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

时间:2022-04-17 12:14:56
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