Sql Server利用游标批量清空数据表

USE [DataBase]
GO
DECLARE @name varchar(50)
DECLARE @count int
set @count=0
DECLARE contact_cursor CURSOR FOR
select name from sysobjects 
where xtype='u' and name like 'tb_xxx_%'--根据条件筛选表名

OPEN contact_cursor


FETCH NEXT FROM contact_cursor
INTO @name--表名

WHILE @@FETCH_STATUS = 0
BEGIN
   print '已经清空表'+@name
   set @count=@count+1
   exec('truncate table '+@name)
   FETCH NEXT FROM contact_cursor
   INTO @name
END
print '处理完成,共清空数据表'+convert(varchar(50),@count)+''
CLOSE contact_cursor
DEALLOCATE contact_cursor
GO

 

posted @ 2018-06-04 15:40  天豪  阅读(449)  评论(0编辑  收藏  举报