SQL Server: drop table cascade equivalent?

狂风中的少年 提交于 2019-11-27 04:37:13

I don't believe SQL has a similarly elegant solution. You have to drop any related constraints first before you can drop the table.

Fortunately, this is all stored in the information schema and you can access that to get your whack list.

This blog post should be able to get you what you need: http://weblogs.asp.net/jgalloway/archive/2006/04/12/442616.aspx

-- t-sql scriptlet to drop all constraints on a table
DECLARE @database nvarchar(50)
DECLARE @table nvarchar(50)

set @database = 'DatabaseName'
set @table = 'TableName'

DECLARE @sql nvarchar(255)
WHILE EXISTS(select * from INFORMATION_SCHEMA.TABLE_CONSTRAINTS where constraint_catalog = @database and table_name = @table)
BEGIN
    select    @sql = 'ALTER TABLE ' + @table + ' DROP CONSTRAINT ' + CONSTRAINT_NAME 
    from    INFORMATION_SCHEMA.TABLE_CONSTRAINTS 
    where    constraint_catalog = @database and 
            table_name = @table
    exec    sp_executesql @sql
END
missaghi

In SQL Server Management Studio, go to Options / SQL Server Object Explorer / Scripting, and enable 'Generate script for dependent objects'. Then right click the table, script > drop to > new query window and it will generate it for you.

This might be a horrible solution, but I find it's quick. It is similar to Vinnie's answer, but the product of the SQL statement is another series of SQL statements that will delete all constraints and tables.

(
select
  'ALTER TABLE ' + tc.table_name + ' DROP CONSTRAINT ' + tc.constraint_name + ';'
from
  INFORMATION_SCHEMA.TABLES t
  ,INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
where
  t.table_name = tc.table_name
  and tc.constraint_name not like '%_pk'
  and tc.constraint_name not like 'pk_%'
  and t.table_catalog='<schema>'
) UNION (
select
  'DROP TABLE ' + t.table_name + ';'
from
  INFORMATION_SCHEMA.TABLES t
where
  t.table_catalog='<schema>'
)

This is all fun and games until some table references your table...

Then I must alter the code provided like so :

CREATE PROCEDURE _cascadeConstraints @database nvarchar(30) = NULL, @table nvarchar(60) = NULL
as
DECLARE @sql nvarchar(255)
WHILE EXISTS(select * from INFORMATION_SCHEMA.TABLE_CONSTRAINTS where constraint_catalog = @database and table_name = @table)
BEGIN
    select    @sql = 'ALTER TABLE ' + @table + ' DROP CONSTRAINT ' + CONSTRAINT_NAME 
    from    INFORMATION_SCHEMA.TABLE_CONSTRAINTS 
    where    constraint_catalog = @database and 
            table_name = @table
    select @sql = 'ALTER TABLE ' + tc.TABLE_NAME + ' DROP CONSTRAINT ' + tc.CONSTRAINT_NAME
      from INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc join
                  INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc on
                   (rc.CONSTRAINT_CATALOG = tc.CONSTRAINT_CATALOG and
                    rc.CONSTRAINT_NAME = tc.CONSTRAINT_NAME) join
                  INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc_pk on
                   (tc_pk.CONSTRAINT_CATALOG = rc.CONSTRAINT_CATALOG and
                    tc_pk.CONSTRAINT_NAME = rc.UNIQUE_CONSTRAINT_NAME)
     where tc.constraint_catalog = @database
       and tc_pk.TABLE_NAME = @table
    exec    sp_executesql @sql
END
go

Ultimately we are deleting our table. So we can simply run 2 following command:

ALTER TABLE ... DROP CONSTRAINT ...

DROP TABLE ...

1> ALTER TABLE PRJ_DETAILS DROP CONSTRAINT FK_PRJ_TYPE;

-- Table name and Constraint Name are the parameter

2> DROP TABLE .

First drop constraint with its name associated with it table Second you can drop table.

It worked for me and its easy also.

I just need delete the foreign key

DECLARE @database nvarchar(50)
DECLARE @TABLE_NAME nvarchar(250)
DECLARE @CONSTRAINT_NAME nvarchar(250)
DECLARE @sql nvarchar(350)
set @database = 'XXX'


DECLARE db_cursor CURSOR FOR  
select TABLE_NAME, CONSTRAINT_NAME from INFORMATION_SCHEMA.TABLE_CONSTRAINTS where constraint_catalog = @database and CONSTRAINT_TYPE='FOREIGN KEY'

OPEN db_cursor  
FETCH NEXT FROM db_cursor INTO @TABLE_NAME, @CONSTRAINT_NAME  

WHILE @@FETCH_STATUS = 0  
BEGIN  

    select    @sql = 'ALTER TABLE ' + @TABLE_NAME + ' DROP CONSTRAINT ' + @CONSTRAINT_NAME 
    from    INFORMATION_SCHEMA.TABLE_CONSTRAINTS 
    where    constraint_catalog = @database and 
            table_name = @TABLE_NAME
    exec    sp_executesql @sql 

       FETCH NEXT FROM db_cursor INTO  @TABLE_NAME, @CONSTRAINT_NAME 
END  

CLOSE db_cursor  
DEALLOCATE db_cursor 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!