如何在sql server 2008中获取tableName,columnName,constraintsType和cascade类型

时间:2015-05-08 18:07:01

标签: sql-server sql-server-2008

我需要sql server 2008中tableName,columnName,constraintsType和cascade类型的列表,所以我尝试了下面的查询但是cascade类型为null

SELECT TB.name AS "TableName",CL.name AS 
"ColumnName",KC.type,FK.delete_referential_action,FK.update_referential_action  
FROM SYS.key_constraints KC JOIN SYS.tables TB ON   
KC.parent_object_id=TB.object_id JOIN SYS.columns CL ON  
KC.parent_object_id=CL.object_id LEFT JOIN SYS.foreign_keys FK ON FK.name = KC.name

1 个答案:

答案 0 :(得分:0)

我相信这就是你要找的东西

SELECT
o1.name AS FK_table,
c1.name AS FK_column,
fk.name AS FK_name,
fk.type as FK_TYPE,
pk.name AS PK_name,
pk.type as PK_TYPE,
fk.delete_referential_action_desc AS Delete_Action,
fk.update_referential_action_desc AS Update_Action
FROM sys.objects o1
INNER JOIN sys.foreign_keys fk
    ON o1.object_id = fk.parent_object_id
INNER JOIN sys.foreign_key_columns fkc
    ON fk.object_id = fkc.constraint_object_id
INNER JOIN sys.columns c1
    ON fkc.parent_object_id = c1.object_id
    AND fkc.parent_column_id = c1.column_id
INNER JOIN sys.key_constraints pk
    ON fk.referenced_object_id = pk.parent_object_id
    AND fk.key_index_id = pk.unique_index_id
ORDER BY o1.name, fkc.constraint_column_id

我没有尝试你的查询,因为我没有在我面前的SQL服务器,但我相信你错过了一个DMV,这就是为什么你的左连接返回空值。