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

后端 未结 26 2652
梦毁少年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:45

    You should also mind the references to other objects.

    If the table was highly referenced by other tables than it’s probably also highly referenced by other objects such as views, stored procedures, functions and more.

    I’d really recommend GUI tool such as ‘view dependencies’ dialog in SSMS or free tool like ApexSQL Search for this because searching for dependencies in other objects can be error prone if you want to do it only with SQL.

    If SQL is the only option you could try doing it like this.

    select O.name as [Object_Name], C.text as [Object_Definition]
    from sys.syscomments C
    inner join sys.all_objects O ON C.id = O.object_id
    where C.text like '%table_name%'
    

提交回复
热议问题