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.
| 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 ASSELECT 'TB1' AS TableName, * FROM Table1UNION ALLSELECT 'TB2' AS TableName, * FROM Table2UNION ALLSELECT 'TB3' AS TableName, * FROM Table3UNION ALLSELECT 'TB4' AS TableName, * FROM Table4And then query the view like this:DECLARE @table varchar(10)SET @table='TB2'SELECT * FROM AllTables WHERE TableName=@tableHowever, 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. |
 |
|
|
|
|
|