Concatenate a table name from a variable in a FROM clause

Posted by idieeasy on Stack Overflow See other posts from Stack Overflow or by idieeasy
Published on 2012-03-22T05:25:45Z Indexed on 2012/03/22 5:29 UTC
Read the original article Hit count: 156

Filed under:

I'm trying to use a dynamically generated fully-qualified table name in sql server 2008. For example, this does not work:

select max([id]) from @dbName+N'.[T1]'

This will give an error like:

Msg 102, Level 15, State 1, Line 73

Incorrect syntax near '+'.

I know that something like this works:

declare @qualifiedTable varchar(200) = @dbName+N'.[T1]'
select max([id]) from @qualifiedTable 

But I have to do this LOTS of times so I would really like to do it in line. Is it possible?

© Stack Overflow or respective owner

Related posts about sql-server-2008