SQL触发器批量删除数据库中的表

时间:2021-08-30 22:21:27

以下通过触发器批量删除数据库中的表,SQL2008已验证

 1 DECLARE @Table NVARCHAR(300)
 2 DECLARE @Count Int = 0  
 3 DECLARE tmpCur CURSOR FOR  
 4 SELECT name FROM sys.objects WHERE TYPE='U' AND name LIKE N'temp_cost%' order by name
 5 OPEN tmpCur 
 6 FETCH NEXT FROM tmpCur INTO @Table 
 7 WHILE @@FETCH_STATUS= 0   
 8     BEGIN 
 9         DECLARE @sql VARCHAR(100) 
10         SELECT @sql='drop table ['+@Table+']'
11         EXEC(@sql)
12         Print convert(varchar(100),@Count)+':'+@sql
13         SET @Count=@COUNT + 1 
14         FETCH NEXT FROM tmpCur INTO @Table 
15     END 
16 CLOSE tmpCur
17 DEALLOCATE tmpCur