SQL Server 2008 delete all tables under special schema

后端 未结 13 1521
广开言路
广开言路 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;
    

提交回复
热议问题