Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Temporary Table problem

Author  Topic 

suhasckanade
Starting Member

8 Posts

Posted - 2008-06-26 : 05:22:59
We have to write a stored procedure in SQL 2000 in which we need to create dynamically 3-5 tables depending on parameters. But in that we are facing a problem while accessing temporary table. A small & sample of code in stored procedure is as follow..

declare @strTable varchar(50)
declare @strQuery nvarchar(100)
SET @strTable = '#tmp1'

set @strQuery = 'select * into ' + @strTable + ' from user_mgmt'
print @strQuery
exec sp_executesql @strQuery

set @strQuery = 'SELECT * FROM ' + @strTable
print @strQuery
exec sp_executesql @strQuery



after running this script query analyser gives error...

Server: Msg 208, Level 16, State 1, Line 1
Invalid object name '#tmp1'.


Pls suggest the possible causes of this problem or what we need to change in this procedure.
Thanks in advance.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-26 : 06:22:45
try with exec(@sql)
Go to Top of Page

suhasckanade
Starting Member

8 Posts

Posted - 2008-06-26 : 07:39:39
quote:
Originally posted by visakh16

try with exec(@sql)



still it gives same error...
Server: Msg 208, Level 16, State 1, Line 1
Invalid object name '#tmp1'.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-26 : 08:06:23
quote:
Originally posted by suhasckanade

quote:
Originally posted by visakh16

try with exec(@sql)



still it gives same error...
Server: Msg 208, Level 16, State 1, Line 1
Invalid object name '#tmp1'.


working for me anyways.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-06-26 : 09:13:07
the temp table #tmp1 that you created only exists within the scope of the first sp_executesql. It is drop and when the first sp_executesql terminates.

use global temp table ##tmp1 instead


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -