![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I'm trying to declare a cursor that gets a list of tables in a databse and then deletes all the data from them. My problem is that SQL will not recognize the variable as a table name. Is there another way to do this or perhaps some different syntax use? Here are my statements declare @table_name nvarchar(384) DECLARE DEL_WO_DATA CURSOR FOR select [name] As table_name from sysobjects where [name] like 'work_%' OPEN DEL_WO_DATA FETCH NEXT FROM DEL_WO_DATA INTO @table_name WHILE (@@FETCH_STATUS = 0) BEGIN delete from [@table_name] /* <--Does not recognize the @table_name variable at this point Receive this error while debugging: Server: Msg 208, Level 16, State 1, Procedure TEST, Line 33 [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name '@table_Name'. */ FETCH NEXT FROM DEL_WO_DATA INTO @table_name end close DEL_WO_DATA deallocate DEL_WO_DATA |
#3
| |||
| |||
|
|
I'm trying to declare a cursor that gets a list of tables in a databse and then deletes all the data from them. My problem is that SQL will not recognize the variable as a table name. Is there another way to do this or perhaps some different syntax use? Here are my statements declare @table_name nvarchar(384) DECLARE DEL_WO_DATA CURSOR FOR select [name] As table_name from sysobjects where [name] like 'work_%' OPEN DEL_WO_DATA FETCH NEXT FROM DEL_WO_DATA INTO @table_name WHILE (@@FETCH_STATUS = 0) BEGIN delete from [@table_name] /* <--Does not recognize the @table_name variable at this point Receive this error while debugging: Server: Msg 208, Level 16, State 1, Procedure TEST, Line 33 [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name '@table_Name'. */ FETCH NEXT FROM DEL_WO_DATA INTO @table_name end close DEL_WO_DATA deallocate DEL_WO_DATA |
#4
| |||
| |||
|
#5
| |||
| |||
|
|
EXEC('delete from ' + @table_name) HH, Jens Suessmeyer. --- http://sqlserver2005.de --- "Troy Jerkins" <tjerkins (AT) alltel (DOT) net> schrieb im Newsbeitrag news:ei%23SpdQRFHA.1172 (AT) TK2MSFTNGP12 (DOT) phx.gbl... I'm trying to declare a cursor that gets a list of tables in a databse and then deletes all the data from them. My problem is that SQL will not recognize the variable as a table name. Is there another way to do this or perhaps some different syntax use? Here are my statements declare @table_name nvarchar(384) DECLARE DEL_WO_DATA CURSOR FOR select [name] As table_name from sysobjects where [name] like 'work_%' OPEN DEL_WO_DATA FETCH NEXT FROM DEL_WO_DATA INTO @table_name WHILE (@@FETCH_STATUS = 0) BEGIN delete from [@table_name] /* <--Does not recognize the @table_name variable at this point Receive this error while debugging: Server: Msg 208, Level 16, State 1, Procedure TEST, Line 33 [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name '@table_Name'. */ FETCH NEXT FROM DEL_WO_DATA INTO @table_name end close DEL_WO_DATA deallocate DEL_WO_DATA |
![]() |
| Thread Tools | |
| Display Modes | |
| |