Sunday, 21 February 2021

To Know Foreign Keys on Table in SQL SERVER

Msg 3726, Level 16, State 1, Line 26

Could not drop object 'Table_name' because it is referenced by a FOREIGN KEY constraint.


To get the list of Foreign Keys

 SELECT * 

FROM sys.foreign_keys

WHERE referenced_object_id = object_id('Table_name')


To Remove the constraints, the given output to be executed to remove Foreign Keys

SELECT 

    'ALTER TABLE [' +  OBJECT_SCHEMA_NAME(parent_object_id) +

    '].[' + OBJECT_NAME(parent_object_id) + 

    '] DROP CONSTRAINT [' + name + ']'

FROM sys.foreign_keys

WHERE referenced_object_id = object_id('Table_name')