SQL Server 2008 delete all tables under special schema

后端 未结 13 1518
广开言路
广开言路 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:28

    Building on @Kevo's answer, I added the following for dropping all foreign key constraints before deleting the tables. I've only tested on SQL2008 R2

    select @Sql = COALESCE(@Sql,'') + 'ALTER TABLE %SCHEMA%.' + t.name + ' drop constraint ' + 
    OBJECT_NAME(d.constraint_object_id)  + ';' + CHAR(13)
    from sys.tables t 
        join sys.foreign_key_columns d on d.parent_object_id = t.object_id 
        inner join sys.schemas s on t.schema_id = s.schema_id
    where s.name = @Schema
    ORDER BY t.name;
    
    0 讨论(0)
  • 2021-01-30 01:29

    Just in case it helps someone, I added this as a stored procedure to the master database to allow it to conveniently used on any db / schema.

    It can be called like this:

    EXEC master.dbo.dropTablesInSchema 'my_db', 'dbo
    

    Stored procedure create script:

    CREATE PROC [master].[dbo].[dropTablesInSchema]
        @db nvarchar(max),
        @schema nvarchar(max)
    AS
    BEGIN
        DECLARE @Tables TABLE (name nvarchar(max))
        INSERT INTO @Tables
        EXEC ('SELECT TABLE_NAME FROM [' + @db + '].INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ''' + @schema + ''' and TABLE_TYPE =''BASE TABLE''')
    
        DECLARE @SqlStatement NVARCHAR(MAX)
        SELECT @SqlStatement = 
            COALESCE(@SqlStatement, N'') + N'DROP TABLE [' + @db + '].[' + @schema + '].' + QUOTENAME(NAME) + N';' + CHAR(13)
        FROM @Tables
    
        EXEC(@SqlStatement)
    
    END
    
    0 讨论(0)
  • 2021-01-30 01:30

    This will generate all the DROP TABLE and DROP VIEW with check exists.

    DECLARE @SqlStatement NVARCHAR(MAX)
    
    SELECT @SqlStatement = 
    COALESCE(@SqlStatement, N'') + N'IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'''+'['+TABLE_SCHEMA+'].' + QUOTENAME(TABLE_NAME) +''' )' + CHAR(13)+
     '  DROP '+ TABLE_TYPE +' ['+TABLE_SCHEMA+'].' + QUOTENAME(TABLE_NAME) + N';' + CHAR(13)
    FROM INFORMATION_SCHEMA.TABLES
    WHERE TABLE_SCHEMA in ('SCHEMA1','SCHEMA2','SCHEMA13' )
    ORDER BY TABLE_SCHEMA   
    
    PRINT  REPLACE(@SqlStatement,'DROP BASE TABLE ','DROP TABLE ') 
    GO
    
    0 讨论(0)
  • 2021-01-30 01:31

    I combined the answers from @raider33 and @Kevo to one solutions for direct execution.

    DECLARE @SqlStatement NVARCHAR(MAX)
    DECLARE @schema varchar(30) = 'SCHEMA_NAME';
    
    select @SqlStatement = COALESCE(@SqlStatement,'') + 'ALTER TABLE '+@schema+'.' + t.name + ' drop constraint ' + 
    OBJECT_NAME(d.constraint_object_id)  + ';' + CHAR(13) + CHAR(10)
    from sys.tables t 
        join sys.foreign_key_columns d on d.parent_object_id = t.object_id 
        inner join sys.schemas s on t.schema_id = s.schema_id
    where s.name = @schema
    ORDER BY t.name;
    
    SELECT @SqlStatement += 
        COALESCE(@SqlStatement, '') + 'DROP TABLE ' + @schema +'.'+ QUOTENAME(TABLE_NAME) + ';'  + CHAR(13) + CHAR(10)
    FROM INFORMATION_SCHEMA.TABLES
    WHERE TABLE_SCHEMA = @schema
    
    EXECUTE sp_executesql @SqlStatement
    
    0 讨论(0)
  • 2021-01-30 01:32

    Also building on @Kevo's answer, I added the following while loop for an issue I was having with TSQL Print statement. A message string can be up to 8,000 characters long. If greater than 8,000 the print statement will truncate any remaining characters.

    DECLARE @SqlLength int
          , @SqlPosition int = 1
          , @printMaxLength int = 8000
    
    SET @SqlLength = LEN(@Sql)
    
    WHILE (@SqlLength) > @printMaxLength
    BEGIN
        PRINT SUBSTRING(@Sql, @SqlPosition, @printMaxLength)
        SET @SqlLength = @SqlLength - @printMaxLength
        SET @SqlPosition = @SqlPosition + @printMaxLength
    END
    IF (@SqlLength) < @printMaxLength AND (@SqlLength) > 0
    BEGIN
        PRINT SUBSTRING(@Sql, @SqlPosition, @printMaxLength)
    END
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题