T-SQL: DROP Table cascade constraints equivalent?

拥有回忆 提交于 2019-12-05 23:19:43

问题


In oracle, I can issue a DROP TABLE ... cascade constraints and it won't complain about FKs, etc.

Is there an equivalent in T-SQL?


回答1:


NO, IN SSMS right click on the table, and select "script table as" then "drop to", then "new window", "file..." or "clipboard" and it will produce a script that will include all the necessary drops of FKs etc.




回答2:


For those who got here in the hope of a more generally applicable answer

This will find the constraint, drop it, and then the column

Thanks and a vote to Tim Lentine How to find the name of a default constraint for the start.

Declare @sql VarChar(255)
Declare @tableName Varchar(255)
Declare @columnName VarChar(255)
Select @tableName = 'MyTableName'
Select @columnName = 'MyColumnName'
select @sql = o.[name] from sysobjects o 
inner join syscolumns c
on o.id = c.cdefault
inner join sysobjects t
on c.id = t.id
where o.xtype = 'd'
and t.name = @tableName
and c.name = @columnName

if @sql is not null
begin
  select @sql = 'Alter Table ' + @tableName + ' Drop Constraint ' + @sql + ' Alter Table ' + @tablename + ' Drop Column ' + @columnName
  exec(@sql)
end


来源:https://stackoverflow.com/questions/2247268/t-sql-drop-table-cascade-constraints-equivalent

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