Sql Server利用游标批量清空数据表
时间:2021-07-01 10:21:17
帮助过:13人阅读
USE [DataBase]
2 GO
3 DECLARE @name varchar(
50)
4 DECLARE @count int
5 set @count=0
6 DECLARE contact_cursor
CURSOR FOR
7 select name
from sysobjects
8 where xtype
=‘u‘ and name
like ‘tb_xxx_%‘--根据条件筛选表名
9
10 OPEN contact_cursor
11
12
13 FETCH NEXT FROM contact_cursor
14 INTO @name--表名
15
16 WHILE @@FETCH_STATUS = 0
17 BEGIN
18 print ‘已经清空表‘+@name
19 set @count=@count+1
20 exec(
‘truncate table ‘+@name)
21 FETCH NEXT FROM contact_cursor
22 INTO @name
23 END
24 print ‘处理完成,共清空数据表‘+convert(
varchar(
50),
@count)
+‘个‘
25 CLOSE contact_cursor
26 DEALLOCATE contact_cursor
27 GO
Sql Server利用游标批量清空数据表
标签: