sql游标用法:删除sql server表

发布时间:2020-01-02编辑:脚本学堂
sql server游标的用法,用于删除sql数据库中的表。

代码(学习sql游标的使用):
 

复制代码 代码示例:

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