一个简单的游标删除SQL SERVER表

时间:2021-11-04 02:35:20

use databaseName

declare @tblname char(100)

declare @sql char(5000)

declare table_cursor cursor for select name  from sysobjects where  name like 'tb_card[_]%'  and name<>'Tb_card_regist' and name<>'Tb_card_item' and name<>'Tb_card_discrule' and name<>'tb_card_packagesetdetail' and name<>'tb_card_packagedetail' and name<>'tb_card_exchangeitem'

and  name<>'tb_card_package'

open table_cursor

fetch next from table_cursor into @tblname

WHILE @@FETCH_STATUS = 0

BEGIN

 set @sql='delete from '+@tblname

 exec(@sql)

 print 'delete ' +@tblname + 'successful'

 fetch next from table_cursor into @tblname

END

close table_cursor

deallocate table_cursor