View Full Version : declare tablename


accessman2
11-23-2006, 11:20 PM
declare @tbl as varchar(100)

-- setup the temporary table
@tbl = '## New Orders'

select Students, Sum(Orders) into @tbl
from members
where id = 1
or id = 2
Group By Students

when I run it, it has an error. @tbl is invalid in the statement.

What's wrong with it?

Thanks.

SQL_Hell
11-24-2006, 06:08 AM
Hi there,

its a restriction of t-sql I'm afraid you cannot use a parameter as the value for your 'from' clause.

You have to do it this way...


SET @strSQL = ' SELECT ' + @myColumn + ' FROM ' + @myTable
EXEC (@strSQL)