2012-06-19 50 views
1

我有這樣的代碼,讓我確定TSQL程序標識列:INFORMATION_SCHEMA外鍵

SELECT 
    c.column_name, (columnproperty(object_id(@table_name),c.column_name,'isIdentity')) pk 
from information_schema.columns c 
where c.table_name = @table_name 

有人可以幫我修改此代碼允許識別外鍵?

在此先感謝

回答

2

你可以使用這個T-SQL語句來獲取引用特定表的外鍵:如果你想所有外鍵

;WITH ReferencingFK AS 
(
    SELECT 
     fk.Name AS 'FKName', 
     OBJECT_NAME(fk.parent_object_id) 'ParentTable', 
     cpa.name 'ParentColumnName', 
     OBJECT_NAME(fk.referenced_object_id) 'ReferencedTable', 
     cref.name 'ReferencedColumnName', 
     fk.delete_referential_action, 
     fk.update_referential_action 
    FROM 
     sys.foreign_keys fk 
    INNER JOIN 
     sys.foreign_key_columns fkc ON fkc.constraint_object_id = fk.object_id 
    INNER JOIN 
     sys.columns cpa ON fkc.parent_object_id = cpa.object_id AND fkc.parent_column_id = cpa.column_id 
    INNER JOIN 
     sys.columns cref ON fkc.referenced_object_id = cref.object_id AND fkc.referenced_column_id = cref.column_id 
) 
SELECT 
    FKName, 
    ParentTable, 
    ParentColumnName, 
    ReferencedTable, 
    ReferencedColumnName 
FROM 
    ReferencingFK 
WHERE 
    ReferencedTable = 'YourTableNameHere' 
ORDER BY 
    ParentTable, ReferencedTable, FKName 

,然後就刪除WHERE條款完全。