gpt4 book ai didi

sql - 合并查询返回 ORA-30926 : unable to get a stable set of rows in the source tables

转载 作者:行者123 更新时间:2023-12-01 23:17:39 25 4
gpt4 key购买 nike

我有两个合并查询一个接一个地触发

第一次查询

merge into MyTable
using
(
select distinct nullLogSetId.Id as IdToUpdate,
knownLogSetId.LogSetId LogSetIdToUpdateTo
from MyTable knownLogSetId
join MyTable nullLogSetId
on knownLogSetId.IdentifierType = nullLogSetId.IdentifierType
and knownLogSetId.Identifier = nullLogSetId.Identifier
where

knownLogSetId.IdentifierType = 'ABC'
and knownLogSetId.LogSetId >= 0
and nullLogSetId.LogSetId = -1
)
on (Id = IdToUpdate)
when matched then
update set LogSetId = LogSetIdToUpdateTo

第二次查询

merge into MyTable
using
(

select distinct nullLogSetId.Id as IdToUpdate,
knownLogSetId.LogSetId LogSetIdToUpdateTo
from MyTable knownLogSetId
join MyTable nullLogSetId
on knownLogSetId.IdentifierType = nullLogSetId.IdentifierType
and knownLogSetId.Identifier = nullLogSetId.Identifier
where
knownLogSetId.IdentifierType = 'DEF'
and knownLogSetId.LogSetId >= 0
and nullLogSetId.LogSetId = -1
) on (Id = IdToUpdate)
when matched then
update set LogSetId = LogSetIdToUpdateTo

我使用 OracleCommand 一个接一个地从 .NET 调用这些查询

第一个工作正常,但是当第二个被触发时,我得到了错误

ORA-30926: unable to get a stable set of rows in the source tables

我已经阅读了所有相关问题,并从我这边尝试过,如果您发现我在两个查询中都使用了 distinct,那么重复行不是问题。可以请任何人帮助我我做错了什么,这可能是一个基本的东西,因为我是新的查询,请帮助我

最佳答案

if you see i used distinct in both of the queries so duplicating of rows is not an issue.

您可能在数据中有重复项。 DISTINCT 不保证您的 IdToUpdate 与其他列一起使用时是唯一的。 请参阅:

CREATE TABLE #MyTable(IdToUpdate INT, LogSetIdToUpdateTo INT);

INSERT INTO #MyTable VALUES (1,1), (1,2), (2,1),(3,1);

SELECT DISTINCT IdToUpdate, LogSetIdToUpdateTo
FROM #MyTable;

LiveDemo

您将获得 IdToUpdate 两次。检查您的数据:

with cte AS (
select distinct nullLogSetId.Id as IdToUpdate,
knownLogSetId.LogSetId LogSetIdToUpdateTo
from MyTable knownLogSetId
join MyTable nullLogSetId
on knownLogSetId.IdentifierType = nullLogSetId.IdentifierType
and knownLogSetId.Identifier = nullLogSetId.Identifier
where
knownLogSetId.IdentifierType = 'DEF'
and knownLogSetId.LogSetId >= 0
and nullLogSetId.LogSetId = -1
)
SELECT IdToUpdate, COUNT(*) AS c
FROM cte
GROUP BY IdToUpdate
HAVING COUNT(*) > 1;

一种方法是使用聚合函数(MAX/MIN) 而不是DISTINCT:

merge into MyTable
using
(

select nullLogSetId.Id as IdToUpdate,
MAX(knownLogSetId.LogSetId) AS LogSetIdToUpdateTo
from MyTable knownLogSetId
join MyTable nullLogSetId
on knownLogSetId.IdentifierType = nullLogSetId.IdentifierType
and knownLogSetId.Identifier = nullLogSetId.Identifier
where
knownLogSetId.IdentifierType = 'DEF'
and knownLogSetId.LogSetId >= 0
and nullLogSetId.LogSetId = -1
GROUP BY nullLogSetId.Id
) on (Id = IdToUpdate)
when matched then
update set LogSetId = LogSetIdToUpdateTo

关于sql - 合并查询返回 ORA-30926 : unable to get a stable set of rows in the source tables,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33627802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com