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
 General SQL Server Forums
 New to SQL Server Programming
 Select from parameter table

Author  Topic 

stahorse
Yak Posting Veteran

86 Posts

Posted - 2014-02-07 : 03:11:32
I have this query

declare @TableName varchar(50)
set @TableName = 'Hosea_tblDATA_NOTES'

select * from @TableName

Hosea_tblDATA_NOTES is the name of the table, I'm passing it to the parameter then select from it.

as it is not working, with error "Must declare the table variable "@TableName"."

How can I achive that, to pass a table name as a parameter to a query then use that name of the table inside the query to select from it.

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2014-02-07 : 03:29:43
read this about dynamic sql:
http://www.sommarskog.se/dynamic_sql.html


Too old to Rock'n'Roll too young to die.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-02-10 : 09:06:05
quote:
Originally posted by stahorse

I have this query

declare @TableName varchar(50)
set @TableName = 'Hosea_tblDATA_NOTES'

select * from @TableName

Hosea_tblDATA_NOTES is the name of the table, I'm passing it to the parameter then select from it.

as it is not working, with error "Must declare the table variable "@TableName"."

How can I achive that, to pass a table name as a parameter to a query then use that name of the table inside the query to select from it.


why is table dynamic here? ideally you write separate retrieve procedure for each entity

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -