我正在执行查询以避免在从数据库中删除行时出现约束冲突。我尽可能使用ON DELETE CASCADE
个选项,但有时候我不想让删除被执行。
举个例子:
CASE:
下面的查询将列出引用DEPT表的两个表(CUSTOMER和AREA):
SELECT *
FROM sys.foreign_key_columns fkc
JOIN sys.columns col ON
fkc.parent_object_id = col.object_id
AND fkc.parent_column_id = col.column_id
WHERE object_name(referenced_object_id) = 'DEPT'
但是我想在我的查询中仅显示具有外键而不是 ON DELETE CASCADE
选项的表格,在上面的示例中仅显示表CUSTOMER。
有什么想法吗?
答案 0 :(得分:6)
select ao.name,ao.type_desc,delete_referential_action_desc,*
from sys.foreign_keys fk
inner join sys.all_objects ao
on fk.parent_object_id = ao.object_id
where delete_referential_action_desc <> 'CASCADE'
答案 1 :(得分:2)
SELECT *
FROM sys.foreign_key_columns fkc
JOIN sys.columns col ON
fkc.parent_object_id = col.object_id
AND fkc.parent_column_id = col.column_id
WHERE object_name(referenced_object_id) = 'DEPT'
AND objectproperty(fkc.constraint_object_id , 'CnstIsDeleteCascade')=1