April 14, 2005 at 8:45 am
Hi all, I have this problem: I should make a script that open a table whose name is given dinamycally; so I tryed using a simple variable, but if i write a query like this:
declare @tab varchar(20)
set @tab = 'TABLE'
select * from @tab
I get this error:
Server: Msg 137, Level 15, State 2, Line 4
Must declare the variable '@tab'.
Looking in BOL i found that if I write: execute('select * from '+ @tab)
it works, but, cause my query is much more complex, it doesn't seems a great soultion... is there another way to solve this problem?? Thanks for any help
Marco
April 14, 2005 at 9:00 am
Why r u having a dynamic table name??
That's completely besides the point of a stored proc.
April 14, 2005 at 12:59 pm
I'll just add the usual response: http://www.sommarskog.se/dynamic_sql.html
Everything you need can be found there, including an explanation of why this is normally not a good idea, just as Remi points out.
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply