在SQL Server中删除之前,我可以检查约束吗? [英] Can I check for constraints before a delete in SQL Server?

查看:154
本文介绍了在SQL Server中删除之前,我可以检查约束吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况。主表和许多其他表与外键链接在一起。现在,当我想删除主表中的一行时,会发生ConstraintsViolation,这是意图和好的。

现在我想要能够检查ConstraintsViolation将在我触发删除行事件之前发生。



这是否可能?

解决方案

  If Exists(Select * From OtherTable 
其中OtherTableFKColumn = MainTablePrimaryKey)
Begin
回滚事务
RaisError('表中的违反FK约束[OtherTable]',16,1 )
End


I have following situation. A main table and many other tables linked together with foreign keys. Now when I would like to delete a row in the main table a ConstraintsViolation will occur, which is intended and good.

Now I want to be able to check if the ConstraintsViolation will occur before I trigger the delete row event.

Is this possible?

解决方案

If Exists ( Select * From OtherTable
            Where OtherTableFKColumn = MainTablePrimaryKey) 
   Begin
       Rollback Transaction
       RaisError('Violating FK Constraint in Table [OtherTable]', 16, 1)
   End

这篇关于在SQL Server中删除之前,我可以检查约束吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆