SQL Server: drop table cascade equivalent?

前端 未结 6 1475
难免孤独
难免孤独 2020-11-29 06:14

In oracle, to drop all tables and constraints you would type something like

DROP TABLE myTable CASCADE CONSTRAINTS PURGE;

and this would co

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-29 06:17

    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=''
    ) UNION (
    select
      'DROP TABLE ' + t.table_name + ';'
    from
      INFORMATION_SCHEMA.TABLES t
    where
      t.table_catalog=''
    )
    

提交回复
热议问题