Trouble: Using Dynamic Global Temp Table Name

  • I pass to a stored procedure, a global temp table name such as ##temp1234. I store this name in a char or varchar variable such as @temptable_1234. I then want to use this variable within an INSERT statement to populate the global temp table. (ie: SELECT * FROM pubs INTO @temptable_1234) This will allow the client, who passed the dynamic global temp table name to the stored procedure, to access the results when the SP is done executing. However, I'm having trouble, syntax wise, with the @temptable_1234 variable within a stored procedure SQL Insert statement. Any ideas?

  • You must put the sentence into a variable for example:

    DECLARE @cmd AS VARCHAR(100)

    SELECT @cmd = SELECT * INTO ' + @temptable_1234 + ' FROM pubs'

    EXEC (@cmd)

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply