Skip to main content
tada
Source Link
cmsjr
  • 59.6k
  • 11
  • 76
  • 63

There is not a built in procedure to accomplish this, but you can build your own using the information in the information_schema views.

Table based example

Create Proc dropFK(@TableName sysname) as Begin Declare @FK sysname Declare @SQL nvarchar(4000) Declare crsFK cursor for select *tu.Constraint_Name from information_schema.constraint_table_usage TU LEFT JOIN SYSOBJECTS SO ON TU.Constraint_NAME = SO.NAME where xtype = 'F' and Table_Name = @TableName open crsFK selectfetch *next from crsFK into @FK information_schema.constraint_column_usageWhile TU(@@Fetch_Status = 0) LEFTBegin  JOIN SYSOBJECTS SO  ONSet TU.Constraint_NAME@SQL = SO.NAME'Alter table ' + @TableName + ' Drop Constraint ' + @FK where xtype = 'F' Print 'Dropping ' + @FK exec sp_executesql @SQL fetch next from crsFK into @FK End Close crsFK Deallocate crsFK End 

There is not a built in procedure to accomplish this, but you build your own using the information in the information_schema views.

select * from information_schema.constraint_table_usage TU LEFT JOIN SYSOBJECTS SO ON TU.Constraint_NAME = SO.NAME where xtype = 'F' select * from information_schema.constraint_column_usage TU LEFT JOIN SYSOBJECTS SO  ON TU.Constraint_NAME = SO.NAME where xtype = 'F' 

There is not a built in procedure to accomplish this, but you can build your own using the information in the information_schema views.

Table based example

Create Proc dropFK(@TableName sysname) as Begin Declare @FK sysname Declare @SQL nvarchar(4000) Declare crsFK cursor for select tu.Constraint_Name from information_schema.constraint_table_usage TU LEFT JOIN SYSOBJECTS SO ON TU.Constraint_NAME = SO.NAME where xtype = 'F' and Table_Name = @TableName open crsFK fetch next from crsFK into @FK While (@@Fetch_Status = 0) Begin  Set @SQL = 'Alter table ' + @TableName + ' Drop Constraint ' + @FK  Print 'Dropping ' + @FK exec sp_executesql @SQL fetch next from crsFK into @FK End Close crsFK Deallocate crsFK End 
Source Link
cmsjr
  • 59.6k
  • 11
  • 76
  • 63

There is not a built in procedure to accomplish this, but you build your own using the information in the information_schema views.

select * from information_schema.constraint_table_usage TU LEFT JOIN SYSOBJECTS SO ON TU.Constraint_NAME = SO.NAME where xtype = 'F' select * from information_schema.constraint_column_usage TU LEFT JOIN SYSOBJECTS SO ON TU.Constraint_NAME = SO.NAME where xtype = 'F'