Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Dynamically naming local temp tables
Message
General information
Forum:
Microsoft SQL Server
Category:
Stored procedures, Triggers, UDFs
Miscellaneous
Thread ID:
00961561
Message ID:
00961897
Views:
13
>To all,
>
>I'm writing a series of stored procedures in SQL Server 2000. My intent is for the procedures to create one or more local temp tables. The tables' name will be dynamically created and the dynamically created table name will be passed back through an output parameter.
>
>Issue one: What's the best way to create a unique string to be used as a temp table name?
>
>Issue two: How can I get SQL Server to accept the temp table name in a query?
>
>For example:
>
>DECLARE @cMyTempTable VarChar(25)
>SET @cMyTempTable = [unique string]
>
>SELECT *
>INTO #@cMyTempTable
>FROM dbo.MySourceTable
>

The only way you might be able to do this is via a dynamic SQL statement. (see sp_executesql in the BOL). That's about as close to VFP macro substitution as you can get in SQL. Something like this:
DECLARE @cMyTempTable VarChar(25)
SET @cMyTempTable = [unique string]

DECLARE @String NVARCHAR(1000)
set @String = N!'SELECT * INTO #' + @cMyTempTable + 
'FROM dbo.MySourceTable'

sp_executesql @string
I didn't test this so the usual disclaimers apply.

However - and this is a big warning - local temporary tables only exists as long as the session that creates them is alive. So, if you pass this temp table output parameter to, say, a client application, and sever the connection, you're hosed.
Dan LeClair
www.cyberwombat.com
SET RANT ON - The Wombat Blog

Life isn’t a morality contest and purity makes a poor shield. - J. Peter Mulhern
Disclaimer: The comments made here are only my OPINIONS on various aspects of VFP, SQL Server, VS.NET, systems development, or life in general, and my OPINIONS should not be construed to be the authoritative word on any subject. No warranties or degrees of veracity are expressed or implied. Void where prohibited. Side effects may included dizziness, spontaneous combustion, or unexplainable cravings for dark beer. Wash with like colors only, serve immediately for best flavor.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform