批量删除数据库表

时间:2021-06-05 22:20:18

由于工作的原因,有时候会对数据库表进行批量的处理,这时候我们可以用游标对数据库进行批量删除表操作

DECLARE @Table NVARCHAR(30)
DECLARE tmpCur CURSOR FOR 
SELECT name FROM sys.objects WHERE TYPE='U' AND name LIKE N'HSUPA%'
OPEN tmpCur 
FETCH NEXT FROM tmpCur INTO @Table

WHILE @@FETCH_STATUS = 0 
BEGIN
    DECLARE @sql VARCHAR(100)
    SELECT @sql = 'drop table ' + @Table
    EXEC(@sql)
    FETCH NEXT FROM tmpCur INTO @Table
END
CLOSE tmpCur 
DEALLOCATE tmpCur