SQL Server 2008 delete all tables under special schema

后端 未结 13 1525
广开言路
广开言路 2021-01-30 01:02

Hello I would like to know is is possible to drop all tables in database what was created under custom schema for example DBO1...with one sql query or special script.

T

13条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-30 01:33

    Modification of acepted answer that works just copy pasted.

    Change db to your database and set @dbSchema to your schema.

    USE db -- CHANGE TO YOUR DB
    GO
    
    DECLARE @dbSchema NVARCHAR(200);
    SET @dbSchema = 'dbo'  -- CHANGE TO YOUR SCHEMA
    
    DECLARE @SqlStatement NVARCHAR(MAX)
    SELECT @SqlStatement =
        COALESCE(@SqlStatement, N'') + N'DROP TABLE ' +'[' + @dbSchema +']' + '.' + QUOTENAME(TABLE_NAME) + N';' + CHAR(13)
    FROM INFORMATION_SCHEMA.TABLES
    WHERE TABLE_SCHEMA = @dbSchema and TABLE_TYPE = 'BASE TABLE'
    
    EXEC sp_executesql @SqlStatement
    

提交回复
热议问题