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)
 sp_executesql

Author  Topic 

lane0618
Posting Yak Master

134 Posts

Posted - 2002-10-16 : 12:36:27
The code below is part of a stored procedure. I am trying to dynamically create a #temp table, then execute it so I can use it later in the procedure.


declare @csql nvarchar(2000)
select @csql = 'CREATE TABLE #temp
(ftrdesc varchar(300),'
select @csql = @csql + @create2 + ')'

--print(@csql)
Exec sp_executesql @csql, N'@create2 nvarchar(2000)', @create2 = @create2

select * from #temp
---------------------------------------------

When I run this, I get:
Invalid object name '#temp'.

Why is @csql not executing?

Thanks,
Lane

Page47
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2002-10-16 : 12:41:14
It's executing, its just that your temp table is out of scope. The table only exists for the life of the batch and sp_executesql runs outside the scope of your current proc.

Jay White
{0}
Go to Top of Page

lane0618
Posting Yak Master

134 Posts

Posted - 2002-10-16 : 12:57:07
So is there a way to get the "select * from #temp" to work?

Go to Top of Page

Page47
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2002-10-16 : 13:04:53
add it to your @csql ....

But I'm unclear what your @create2 is? More columns?

What are you trying to do? Maybe there is a better way ...

Jay White
{0}
Go to Top of Page
   

- Advertisement -