【发布时间】:2019-04-01 13:19:49
【问题描述】:
我有一个包含 ID 和 ParentAccountID 的帐户表。这是重现这些步骤的脚本。
如果 ParentAccountID 为 NULL,则将其视为顶级帐户。 每个帐户最终都应以顶级帐户结束,即 ParentAccountID 为 NULL
Declare @Accounts table (ID INT, ParentAccountID INT )
INSERT INTO @Accounts values (1,NULL), (2,1), (3,2) ,(4,3), (5,4), (6,5)
select * from @Accounts
-- Request to update ParentAccountID to 6 for the ID 3
update @Accounts
set ParentAccountID = 6
where ID = 3
-- Now the above update will cause circular reference
select * from @Accounts
当请求更新一个帐户的 ParentAccountID 时,如果这会导致循环引用,那么在更新之前需要确定它。
大家有什么想法!!
【问题讨论】:
-
您使用的是 MySQL 还是 MS SQL Server?
-
@jarlh MS SQL 服务器
-
@YvetteColomb 感谢您的介入。Mark 最终是对的,但我认为应该避免以使所有答案无效的方式更改问题。
-
你有没有考虑过这种建模你的hierarchy的方式是否正确?如果您改用
hierarchyid,您会发现循环性首先是不可能。然而,就其对其他查询的影响而言,这是一种权衡。
标签: sql sql-server sql-server-2008 circular-reference