How can I find out what FOREIGN KEY constraint references a table in SQL Server?

前端 未结 15 752
再見小時候
再見小時候 2020-12-04 05:12

I am trying to drop a table but getting the following message:

Msg 3726, Level 16, State 1, Line 3
Could not drop object \'dbo.UserProfile\' bec

15条回答
  •  情深已故
    2020-12-04 05:43

    I found this answer quite simple and did the trick for what I needed: https://stackoverflow.com/a/12956348/652519

    A summary from the link, use this query:

    EXEC sp_fkeys 'TableName'
    

    Quick and simple. I was able to locate all the foreign key tables, respective columns and foreign key names of 15 tables pretty quickly.

    As @mdisibio noted below, here's a link to the documentation that details the different parameters that can be used: https://docs.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-fkeys-transact-sql

提交回复
热议问题