SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Temp table in function


Temp table in function

Author
Message
Zahid Farooq
Zahid Farooq
SSC Veteran
SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)

Group: General Forum Members
Points: 208 Visits: 34
Can i use a temp table in function ?
Mahesh Bote
Mahesh Bote
SSCertifiable
SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)

Group: General Forum Members
Points: 5517 Visits: 1303
Let us know what you are going to do? Coz 1st thing, object can't be create inside UDF.

by the way, the answer for your question is, NO

Mahesh


MH-09-AM-8694
GSquared
GSquared
SSC Guru
SSC Guru (139K reputation)SSC Guru (139K reputation)SSC Guru (139K reputation)SSC Guru (139K reputation)SSC Guru (139K reputation)SSC Guru (139K reputation)SSC Guru (139K reputation)SSC Guru (139K reputation)

Group: General Forum Members
Points: 139405 Visits: 9731
No temp tables in functions. However, you can use table variables. That might do what you need.

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
pduplessis-723389
pduplessis-723389
SSCertifiable
SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)

Group: General Forum Members
Points: 6631 Visits: 400
You could create a table valued function, which stores the values in a resultset.
Temp tables wont be stored in here, but its almost the same...

Sample Code:

CREATE FUNCTION YourFunctionName (@VariablesYouNeedHere )
RETURNS @RESULT_SET TABLE (YourFieldNamesAndTypesHere)
AS

BEGIN
Insert into @RESULT_SET
Select YourFieldNames
from tablex
where fieldy = @VariablesYouPassed
RETURN
END
tony.sawyer
tony.sawyer
SSC-Addicted
SSC-Addicted (439 reputation)SSC-Addicted (439 reputation)SSC-Addicted (439 reputation)SSC-Addicted (439 reputation)SSC-Addicted (439 reputation)SSC-Addicted (439 reputation)SSC-Addicted (439 reputation)SSC-Addicted (439 reputation)

Group: General Forum Members
Points: 439 Visits: 146
You can't use the CREATE TABLE #TEMPTABLE command to create a table for the duration of the function

You can create a table for the duration of the function by declaring it as a variable. We use the following code within a function to determine what date the last occurrence of a certain Cycle was.


declare @TempCycles table ( CycleID CHAR(3), ID INT)
INSERT INTO @TempCycles VALUES ('EOD',1)
INSERT INTO @TempCycles VALUES ('EOW',2)
INSERT INTO @TempCycles VALUES ('EOF',3)
INSERT INTO @TempCycles VALUES ('EOM',4)
INSERT INTO @TempCycles VALUES ('EOQ',5)
INSERT INTO @TempCycles VALUES ('EOH',6)
INSERT INTO @TempCycles VALUES ('EOY',7)

...
JOIN @TempCycles TC
ON TC.CycleID = BD.SYSTEM_CYCLE_ID
...
WHERE
...
AND TC.ID >= @ID
...



Hope this helps

Tony
milad.ahmadi2008
milad.ahmadi2008
SSC Rookie
SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)

Group: General Forum Members
Points: 45 Visits: 0
thanks very much
Evil Kraig F
Evil Kraig F
SSC Guru
SSC Guru (52K reputation)SSC Guru (52K reputation)SSC Guru (52K reputation)SSC Guru (52K reputation)SSC Guru (52K reputation)SSC Guru (52K reputation)SSC Guru (52K reputation)SSC Guru (52K reputation)

Group: General Forum Members
Points: 52579 Visits: 7660
I realize this is 2 years old but since it got necro'd anyway, what is this:

INSERT INTO @TempCycles VALUES ('EOF',3)

End Of Fortnight?


- Craig Farrell

Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

For better assistance in answering your questions | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search