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 |
vdavid70
Yak Posting Veteran
67 Posts |
Posted - 2006-10-09 : 06:26:44
|
Hello Folks,I got this rather funny sql cursor that was written by our vendors and given to me to run on a monthly basis to delete some tables in the database. I copied and pasted this code into query analyzer and parsed to see if it was ok. It was ok.I then went ahead to create a scheduled agent job for it, when i got to the stage where i had to paste the code, i did that and parsed it. This time it came up with errors saying "Error 170:Line 20, Incorrect syntax near '@table_name'.What is could be wrong here.Please find below a copy of the code.-- remove all old Transfer Desk work tablesdeclare @bu_code char(3)declare @opr_code char(3)declare @table_name varchar(50)declare bu_cursor cursor for select DB_CODE, OPR_CODE from DB_DEFN, OPR where DB_CODE <> ''open bu_cursorfetch next from bu_cursor into @bu_code, @opr_codewhile @@FETCH_STATUS = 0begin if exists (select top 1 'OK' from sysobjects where name like @bu_code+'[_]'+rtrim(@opr_code)+'[0-9]%[_][WE]R[RK]%') begin declare tab_cursor cursor for select name from sysobjects where name like @bu_code+'[_]'+RTRIM(@opr_code)+'[0-9]%[_][WE]R[RK]%' open tab_cursor fetch next from tab_cursor into @table_name while @@FETCH_STATUS = 0 begin print 'DROP TABLE ' +@table_name exec ('DROP TABLE ' +@table_name) fetch next from tab_cursor into @table_name end close tab_cursor deallocate tab_cursor end fetch next from bu_cursor into @bu_code, @opr_codeendclose bu_cursordeallocate bu_cursorgo-- Remove old Control desk tablesdeclare @bu_opr varchar(124)declare @table_name varchar(124)declare @del_table_name varchar(124)declare bu_opr_cursor cursor for select rtrim(DB_CODE+'[_]'+rtrim(OPR_CODE)+'[_]') from DB_DEFN, OPRwhere DB_CODE <> ''open bu_opr_cursorfetch next from bu_opr_cursor into @bu_oprwhile @@FETCH_STATUS >= 0begin if exists (select TOP 1 'OK' from sysobjects where name like @bu_opr +'%' and name not like @bu_opr + 'CMP%' and name not like @bu_opr + 'BMP%') begin declare tmp_table_cursor cursor for select @bu_opr + RTRIM(FILTER_TYPE_CODE)+'[_][0-9]%' from FILTER_TYPE open tmp_table_cursor fetch next from tmp_table_cursor into @table_name while @@FETCH_STATUS >= 0 begin declare table_cursor cursor for select name from sysobjects where name like @table_name open table_cursor fetch next from table_cursor into @del_table_name while @@FETCH_STATUS >= 0 begin print 'DROP TABLE ' + @del_table_name exec ('DROP TABLE ' + @del_table_name) fetch next from table_cursor into @del_table_name end close table_cursor deallocate table_cursor fetch next from tmp_table_cursor into @table_name end close tmp_table_cursor deallocate tmp_table_cursor end fetch next from bu_opr_cursor into @bu_oprendclose bu_opr_cursordeallocate bu_opr_cursorgo-- Remove more old Control desk temporary tablesdeclare @bu_opr varchar(124)declare @table_name varchar(124)declare @del_table_name varchar(124)declare bu_opr_cursor cursor for select rtrim(DB_CODE)+'[_]'+rtrim(OPR_CODE)+'[0-9][0-9][0-9][_][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][_]' from DB_DEFN, OPRwhere DB_CODE <> ''open bu_opr_cursorfetch next from bu_opr_cursor into @bu_oprwhile @@FETCH_STATUS >= 0begin if exists (select TOP 1 'OK' from sysobjects where name like @bu_opr +'%') begin declare tmp_table_cursor cursor for select @bu_opr + RTRIM(FILTER_TYPE_CODE)+'%' from FILTER_TYPE open tmp_table_cursor fetch next from tmp_table_cursor into @table_name while @@FETCH_STATUS >= 0 begin declare table_cursor cursor for select name from sysobjects where name like @table_name open table_cursor fetch next from table_cursor into @del_table_name while @@FETCH_STATUS >= 0 begin print 'DROP TABLE ' + @del_table_name exec ('DROP TABLE ' + @del_table_name) fetch next from table_cursor into @del_table_name end close table_cursor deallocate table_cursor fetch next from tmp_table_cursor into @table_name end close tmp_table_cursor deallocate tmp_table_cursor end fetch next from bu_opr_cursor into @bu_oprendclose bu_opr_cursordeallocate bu_opr_cursorgo-- Remove old Recon Manager tablesdeclare @recon_opr varchar(30)declare @table_name varchar(124)declare @del_table_name varchar(124)declare opr_cursor cursor for select rtrim('RECON[_]MGR[_]'+rtrim(OPR_CODE)+'[_]') from OPR where OPR_CODE <> ''open opr_cursorfetch next from opr_cursor into @recon_oprwhile @@FETCH_STATUS >= 0begin if exists (select TOP 1 'OK' from sysobjects where name like @recon_opr+ '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]') begin declare table_cursor cursor for select name from sysobjects where name like @recon_opr+ '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' open table_cursor fetch next from table_cursor into @del_table_name while @@FETCH_STATUS >= 0 begin print 'DROP TABLE ' + @del_table_name exec ('DROP TABLE ' + @del_table_name) fetch next from table_cursor into @del_table_name end close table_cursor deallocate table_cursor end fetch next from opr_cursor into @recon_oprendclose opr_cursordeallocate opr_cursorgo |
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2006-10-09 : 06:43:30
|
Looks ok (well at leastshouldn't get that error.Suspect it's a paste problem.Try copying what you pasted back into query analyser.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
|
|
|
|