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
 Dynamic Selection of table and field to be use??

Author  Topic 

wormz666
Posting Yak Master

110 Posts

Posted - 2008-10-30 : 00:46:44
Declare @dummy sysname
Declare @dummy1 sysname
Declare @columnd sysname
Set @dummy = 'itemmas'
Set @dummy1 = '1-001'
Set @columnd = 'itemid'

Select * from @dummy where @columnd=@dummy1

please help.... i having a problem
selecting the table and field iam going to use dynamically.........

cvraghu
Posting Yak Master

187 Posts

Posted - 2008-10-30 : 00:53:52
Did you read Books Online or google to find out how to frame dynamic sqls and execute them?

sp_executesql('Select * from ' + @dummy + ' where ' + @columnd + ' = ' + @dummy1)

If the column is varchar make sure to include quotes for the value.



Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-10-30 : 03:03:33
Make sure you read this fully
www.sommarskog.se/dynamic_sql.html


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -