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-Enthusiastic
SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)

Group: General Forum Members
Points: 146 Visits: 34
Can i use a temp table in function ?
Mahesh Bote
Mahesh Bote
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1494 Visits: 1274
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-Insane
SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)

Group: General Forum Members
Points: 23713 Visits: 9730
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
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1721 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 Veteran
SSC Veteran (243 reputation)SSC Veteran (243 reputation)SSC Veteran (243 reputation)SSC Veteran (243 reputation)SSC Veteran (243 reputation)SSC Veteran (243 reputation)SSC Veteran (243 reputation)SSC Veteran (243 reputation)

Group: General Forum Members
Points: 243 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
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 0
thanks very much
Evil Kraig F
Evil Kraig F
SSCrazy Eights
SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)

Group: General Forum Members
Points: 8629 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