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 2000 Forums
 Transact-SQL (2000)
 Can this be done? Assigning a table name to a var

Author  Topic 

mdelgado
Posting Yak Master

141 Posts

Posted - 2002-08-02 : 16:31:12
I would like to declare a Table Name to a varible and then run a simple select...
For example...

Declare @table varchar(100)
set @table = tblMyTable

then run a simple select:

Select * from @table


I can't get this to work.

Any ideas?

nr
SQLTeam MVY

12543 Posts

Posted - 2002-08-02 : 16:37:21
try searching for dynamic sql on this site

declare @sql varchar(1000)
select @sql = 'Select * from ' + @table
exec (@sql)



==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

jasper_smith
SQL Server MVP & SQLTeam MVY

846 Posts

Posted - 2002-08-02 : 16:40:42
If you plan to use dynamic sql read
[url]http://www.algonet.se/~sommar/dynamic_sql.html[/url]


HTH
Jasper Smith
Go to Top of Page
   

- Advertisement -