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

前端 未结 15 834
再見小時候
再見小時候 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:49

    You can also return all the information about the Foreign Keys by adapating @LittleSweetSeas answer:

    SELECT 
       OBJECT_NAME(f.parent_object_id) ConsTable,
       OBJECT_NAME (f.referenced_object_id) refTable,
       COL_NAME(fc.parent_object_id,fc.parent_column_id) ColName
    FROM 
       sys.foreign_keys AS f
    INNER JOIN 
       sys.foreign_key_columns AS fc 
          ON f.OBJECT_ID = fc.constraint_object_id
    INNER JOIN 
       sys.tables t 
          ON t.OBJECT_ID = fc.referenced_object_id
    order by
    ConsTable
    

提交回复
热议问题