Sql - tablename as variable

Posted by BhejaFry on Stack Overflow See other posts from Stack Overflow or by BhejaFry
Published on 2010-05-15T01:07:17Z Indexed on 2010/05/15 1:14 UTC
Read the original article Hit count: 305

Filed under:
|
|

Hi folks,

i am trying to execute this query:

declare @tablename varchar(50)

set @tablename = 'test'

select * from @tablename

This produces the following error:

Msg 1087, Level 16, State 1, Line 5
Must declare the table variable "@tablename".

What's the right way to have table name populated dynamically?

TIA

© Stack Overflow or respective owner

Related posts about sql

Related posts about tablename