其他分享
首页 > 其他分享> > 批量删除指定前缀的表

批量删除指定前缀的表

作者:互联网

DECLARE @TmpTb TABLE
    (
      TbName NVARCHAR(100) ,
      Flag INT
    );
 
INSERT  INTO @TmpTb
        SELECT  TABLE_NAME ,
                0 Flag
        FROM    INFORMATION_SCHEMA.TABLES
        WHERE   TABLE_NAME LIKE 'UV_%';
 
DECLARE @Total INT ,
    @RowCount INT ,
    @DropSql NVARCHAR(MAX);
 
SELECT  @Total = COUNT(1)
FROM    INFORMATION_SCHEMA.TABLES
WHERE   TABLE_NAME LIKE 'UV_%';

 
SET @RowCount = 1;
SET @DropSql = '';
 
WHILE @RowCount <= @Total
    BEGIN
        DECLARE @TbName VARCHAR(100);
        SELECT TOP 1
                @TbName = TbName
        FROM    @TmpTb
        WHERE   Flag = 0;
        SET @DropSql = @DropSql + 'DROP TABLE ' + @TbName + ';';
        UPDATE  @TmpTb
        SET     Flag = 1
        WHERE   TbName = @TbName;
        SET @RowCount = @RowCount + 1;
    END;
 
EXEC sp_executesql @DropSql;

 

标签:前缀,删除,INT,批量,DropSql,RowCount,TABLE,SELECT,NAME
来源: https://www.cnblogs.com/myloveblogs/p/12410033.html