SQL Server - 通过父表触发器添加的行在子表触发器中不可用
我有 3 个表
ParentTable
ChildTable
(列ParentId
引用ParentTable
)Recording
(ParentId
列引用自己的表)
父表有一个触发器,可将一行添加到 Recording
表中。
子表再次具有在 Recording
表中追加行的触发器
现在我收到一条错误,指出在 Recording
表中找不到该行
INSERT 语句与 FOREIGN KEY SAME TABLE 冲突 约束
I have 3 tables
ParentTable
ChildTable
(with columnParentId
refering toParentTable
)Recording
(with columnParentId
refering to own table)
Parent table has a trigger which adds a row into the Recording
table.
Child table again has a trigger that appends row in Recording
table
Now I get an error that the row was not found in Recording
table
The INSERT statement conflicted with the FOREIGN KEY SAME TABLE
constraint
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能会触发将重复的 ParentID 插入到记录表中。如果记录表中的 ParentID 定义了唯一键,则情况如此。
插入到 Parent 表时,您将带有 ParentID 的记录插入到 Recording 中。我认为您在 ChildTable 触发器上插入了重复的 ParentID。
也许您可以提供更多背景信息。
另外,如果可能的话,您可能希望避免触发因素——大多数情况下都是如此。使用 CTE 和 OUTPUT 子句可以帮助您避免触发器问题。
You might be triggering a duplicate ParentID to be inserted into the Recording table. This would be true if your ParentID in the Recording table has a unique key defined on it.
When inserting into the Parent table, you insert a record with a ParentID into Recording. I think your insert a duplicate ParentID on the ChildTable trigger.
Maybe you can provide some more context.
Also, you might want to avoid triggers if possible - which it is most of the time. Using CTE's and OUTPUT clauses this can help you avoid issues with triggers.