How can I list all foreign keys referencing a given table in SQL Server?

后端 未结 26 2663
梦毁少年i
梦毁少年i 2020-11-22 07:13

I need to remove a highly referenced table in a SQL Server database. How can I get a list of all the foreign key constraints I will need to remove in order to drop the tabl

26条回答
  •  渐次进展
    2020-11-22 07:52

    Not sure why no one suggested but I use sp_fkeys to query foreign keys for a given table:

    EXEC sp_fkeys 'TableName'
    

    You can also specify the schema:

    EXEC sp_fkeys @pktable_name = 'TableName', @pktable_owner = 'dbo'
    

    Without specifying the schema, the docs state the following:

    If pktable_owner is not specified, the default table visibility rules of the underlying DBMS apply.

    In SQL Server, if the current user owns a table with the specified name, that table's columns are returned. If pktable_owner is not specified and the current user does not own a table with the specified pktable_name, the procedure looks for a table with the specified pktable_name owned by the database owner. If one exists, that table's columns are returned.

提交回复
热议问题