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 |
Axxib
Starting Member
6 Posts |
Posted - 2009-10-07 : 13:09:17
|
HiI need to use a query to form part of the table name in a select statement, something like:select * from [Transactions_M(select Month_No from Month_Counter)](which obviously does not work)so if the result of the sub query: "select Month_No from Month_Counter"is 25, then the end query should be "select * from [Transactions_M25]"Any Ideas would be appreciated.Thanks |
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2009-10-07 : 13:21:47
|
You need dynamic SQL for that...try this...declare @sql varchar(500)select @sql = Month_No from Month_Counterset @sql = 'select * from [Transactions_M' + @sql + ']'exec(@sql) |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-10-08 : 04:35:26
|
declare @sql varchar(500),@month_no intSELECT @month_no=Month_No from Month_Counterselect @sql = 'select * from [Transactions_M' + cast(@month_no as varchar(10))+ ']'exec(@sql)MadhivananFailing to plan is Planning to fail |
|
|
|
|
|