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 |
|
vreezman
Starting Member
2 Posts |
Posted - 2004-12-15 : 21:38:14
|
| i have a tables with sequence namelike journal_01_04 journal_02_04 ..... journal_12_04is that possible for me to make stored procedure that access certain tablejust by passing table name as parameter, ex : if i pass parameter "journal_01_04"then it process table journal_01_04. i know i can solve this problem with "select case"but the code is too looong..... |
|
|
Hippi
Yak Posting Veteran
63 Posts |
Posted - 2004-12-15 : 21:54:11
|
| Yeap, u can use dynamic sql. Try to search for dynamic sql in this forum |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
|
|
clarkbaker1964
Constraint Violating Yak Guru
428 Posts |
Posted - 2004-12-15 : 22:37:47
|
Declare @sql as NVarchar(500), @TableName as Varchar(100)Set NoCount OnSelect [Name] as TableName into #MyTables From sysobjects where xtype ='U' and Name like 'journal%'While Exists(Select * from #MyTables)BEGIN Select Top 1 @TableName = TableName From #MyTables Delete From #MyTables Where TableName = @TableName Set @sql = 'Select * from ' + @TableName Print @sql exec sp_executesql @sqlEND --WHILE TABLEDrop Table #MyTablesHope that helps and you don't even have to use cursors. |
 |
|
|
vreezman
Starting Member
2 Posts |
Posted - 2004-12-16 : 21:44:16
|
clarkbaker1964 :sorry i dunno understand u code :(, i'm a newbee in t sql, could u write a code toloop the recordset of dynamic sql :like i want to loop an recordset of "select * from @tablename"thx |
 |
|
|
|
|
|