SQLSERVER批量清除临时表
金蝶云社区-云社区用户S5fN2127
云社区用户S5fN2127
0人赞赏了该文章 1,325次浏览 未经作者许可,禁止转载编辑于2016年09月08日 10:43:36

--定义游标删除VT表
declare c1 cursor for select name from sysobjects where name like 'VT%'; *定义游标c1 存放VT开头的表的表明
open c1; *打开游标
declare @table_name varchar(100); *定义table_name变量
DECLARE @SQLString NVARCHAR(500) *定义SQLString变量
fetch c1 into @table_name*移动游标至第一条记录,并把第一条记录的值放到变量table_name中
while(@@fetch_status=0)*如果上一个操作成功,就进行下一步
begin
SET @SQLString = N'drop table ' + @table_name *定义SQLString变量并赋值
EXEC(@SQLString) *执行变量SQLString
fetch next from c1 into @table_name;*移动游标至下一行并赋值于table_name中
end
close c1;*关闭游标
deallocate c1; *删除游标