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 |
|
ProEdge
Yak Posting Veteran
64 Posts |
Posted - 2007-07-19 : 17:56:58
|
I have a list of table names in a table. I'd like to be able to select from one of these tables based on a condition provided. I have my query in a stored procedure which expects a paramater called @Flag. My procedure works similar to this:SELECT m.Column1, m.Column2FROM myTable m INNER JOIN (SELECT TableName FROM TableList WHERE Flag = @Flag) t ON m.ID = t.ID Of course when I run this it fails. How can I create a dynamic query that will let me do this? |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-07-19 : 17:58:56
|
| You'll need to use dynamic SQL.Tara Kizerhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
ProEdge
Yak Posting Veteran
64 Posts |
Posted - 2007-07-19 : 18:04:19
|
| I figured I would but I don't have a clue how to start it. Could you provide a sample or link? |
 |
|
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2007-07-19 : 18:07:09
|
| This will be worth the time spent: http://www.sommarskog.se/dynamic_sql.htmlDinakar Nethi************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/ |
 |
|
|
ProEdge
Yak Posting Veteran
64 Posts |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-07-19 : 18:09:10
|
| http://www.sommarskog.se/dynamic_sql.htmlPeter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-07-19 : 18:09:26
|
Peter LarssonHelsingborg, Sweden |
 |
|
|
|
|
|