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 2005 Forums
 Transact-SQL (2005)
 variable name as tablename

Author  Topic 

sqlalways
Starting Member

3 Posts

Posted - 2007-04-17 : 23:40:40
can we use variable name as table in the below query
actual syntax
select * into table1 from table2

but i want to use variable in place of table1
which is a combination of values
set @table1='abc'+@value and now
select * into @table1 from table2

error is incorrect syntax @table1
plz help

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2007-04-17 : 23:49:21
You would need to use dynamic SQL to accomplish that..

set @table1='abc'+@value
EXEC('select * into ' + @table1 + ' from table2')

There's some pros and cons you would need to understand when it comes to dynamic SQL.
Check out these articles to get a proper understanding.. http://www.sommarskog.se/dynamic_sql.html





************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page
   

- Advertisement -