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)
 Table Variables

Author  Topic 

rlopez
Starting Member

18 Posts

Posted - 2007-05-12 : 16:48:49
I need to create a stored procedure to select a different table based on which parameter it receives. I am having trouble getting this to work here is an example of what I am trying to do. Has anyone had any experience with this?

DECLARE @TableVariable AS table
SET @TableVariable = 'SalesData_MonthEnd'

SELECT *
FROM @TableVariable

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2007-05-12 : 16:53:35
you need to use dynamic sql:
EXEC('SELECT * FROM ' + @table) 

Howver, using dynamic sql has to be done carefully. There are plenty of caveats. do a search on these forums and also look into http://www.sommarskog.se/dynamic_sql.html to get a better udnerstanding of the pros/cons with dynamic sql.

Dinakar Nethi
SQL Server MVP
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page
   

- Advertisement -