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
 Transact-SQL (2000)
 table variable in dynamic query

Author  Topic 

rubs_65
Posting Yak Master

144 Posts

Posted - 2004-10-18 : 11:54:40
Hi,

I am trying to use table variable in dynamic sql but always getting
error:
Server: Msg 137, Level 15, State 2, Line 3 Must declare the variable '@dd'.

Following is the code:

declare @dd table(dd int)
declare @str1 nvarchar(4000)
set @str1 = 'create view dd_view as select * from ' + @dd + ')'
exec (@str1)

OR

declare @dd table(dd int)
declare @str1 nvarchar(4000)
set @str1 = 'create view dd_view as select * from @dd'
exec (@str1)

Is table variable allowed inside the dynamic query and how to modify the above code?

Thanks
--Harvinder

nr
SQLTeam MVY

12543 Posts

Posted - 2004-10-18 : 12:03:03
>> Is table variable allowed inside the dynamic query and how to modify the above code?
nope
Use a temp table

Actually what are you trying to do? Create a view on a temporary spid dependent object?

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2004-10-18 : 12:07:51
no you can't at least not on the table variable in this way.
that's because the Create view must be the first statment in a batch thus removing the possibility to use table variable.


Go with the flow & have fun! Else fight the flow
Go to Top of Page
   

- Advertisement -