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)
 Select statement

Author  Topic 

cbecerra
Starting Member

38 Posts

Posted - 2002-09-25 : 14:15:18
I was wondering if it is posible to make this query

(select * from @table)

work without using strings...


Thanks
.Cesar

robvolk
Most Valuable Yak

15732 Posts

Posted - 2002-09-25 : 14:19:07
If you mean query a dynamic table name without using dynamic SQL, the answer is no.

Well, there is a way. If you have multiple tables with the same structure, you could UNION them all together, and add a column indicating the table name:

CREATE VIEW AllTables AS
SELECT 'TB1' AS TableName, * FROM Table1
UNION ALL
SELECT 'TB2' AS TableName, * FROM Table2
UNION ALL
SELECT 'TB3' AS TableName, * FROM Table3
UNION ALL
SELECT 'TB4' AS TableName, * FROM Table4


And then query the view like this:

DECLARE @table varchar(10)
SET @table='TB2'
SELECT * FROM AllTables WHERE TableName=@table


However, this is an example really poor database design as well as a workaround for dynamic SQL. Dynamic SQL is probably the best way to go for what you need.

Go to Top of Page
   

- Advertisement -