SQL 合并(使用 Temp Table)在 SubQuery 上失败返回了超过 1 个值(但我没有在更新中使用 Sub-Query..)
SQL Merge (with Temp Table) failing on SubQuery returned more than 1 value (But I am not using a Sub-Query.. in the update)
我希望有人能帮助我摆脱这种乏味...!?
如标题所示,我有一个 Temp Table(在 select 语句中动态创建):
SELECT *
INTO #results
FROM Table_1
CROSS APPLY ( SELECT TOP 1 *
FROM Table_2
WHERE (Table_1.ItemId = Table_2.ItemId)
ORDER BY CreatedDt DESC
)
... 如您所见,它在交叉连接中使用了 Sub-Query。
接下来我尝试使用此临时 table #results 更新相关的 table 及其值。已尝试使用更新:
UPDATE a
SET a.StatusId = b.StatusId
FROM Table_1 a
INNER JOIN #results b on (a.ItemId = b.ItemId)
并合并:
MERGE INTO Table_1 a
USING #results b
ON (a.ItemId = b.temId)
WHEN MATCHED THEN UPDATE SET a.StatusId = b.StatusId;
但我似乎总能得到回应:
Msg 512, Level 16, State 1, Procedure trg_dbo_PBITree_TreeModel_HierarchicalEscalationHistory_InsertNode,
Line 7 [Batch Start Line 11] Subquery returned more than 1 value. This
is not permitted when the subquery follows =, !=, <, <= , >, >= or
when the subquery is used as an expression.
当我查询有问题的两个 table 时 (#results & Table_1),它们都具有 1 对 1 的关系,根本看不出它可能隐藏某种子查询的位置! ?
任何人都可以快速帮助解决这个问题吗?这似乎是 1-0-1 的东西,正在烤我的汉堡!
-- 编辑--
我查看了错误消息中提到的触发器,因为有人建议它可能正在尝试处理单行更新,而不是我正在做的多行更新。对我来说没什么特别的...?
ALTER TRIGGER [dbo].[trg_dbo_PBITree_TreeModel_HierarchicalEscalationHistory_InsertNode]
ON [dbo].[Table_1]
AFTER UPDATE
AS
BEGIN
-- NodeModelInsertOrUpdateTPH
IF ((select [Item] from inserted) = 'X')
BEGIN
UPDATE tx
SET
tx.LastUpdatedBy = i.LastUpdatedBy,
tx.LastUpdatedAt = i.LastUpdatedAt
FROM
[dbo].[Table_X] tx,
inserted i
WHERE
tx.OtherItemId = i.OtherItemId
END
END
有人有什么想法吗?
你的触发器是这里的问题。您的 IF 语句有一个查询 return 多于 1 行,结果就是确切的消息。您应该使触发器能够容忍多行操作。这是相同的逻辑,但它可以处理任意数量的正在更新的行。
ALTER TRIGGER [dbo].[trg_dbo_PBITree_TreeModel_HierarchicalEscalationHistory_InsertNode]
ON [dbo].[Table_1]
AFTER UPDATE
AS
BEGIN
UPDATE tx
SET
tx.LastUpdatedBy = i.LastUpdatedBy,
tx.LastUpdatedAt = i.LastUpdatedAt
FROM
[dbo].[Table_X] tx
join inserted i ON tx.OtherItemId = i.OtherItemId
where i.Item = 'X'
END
我希望有人能帮助我摆脱这种乏味...!?
如标题所示,我有一个 Temp Table(在 select 语句中动态创建):
SELECT *
INTO #results
FROM Table_1
CROSS APPLY ( SELECT TOP 1 *
FROM Table_2
WHERE (Table_1.ItemId = Table_2.ItemId)
ORDER BY CreatedDt DESC
)
... 如您所见,它在交叉连接中使用了 Sub-Query。
接下来我尝试使用此临时 table #results 更新相关的 table 及其值。已尝试使用更新:
UPDATE a
SET a.StatusId = b.StatusId
FROM Table_1 a
INNER JOIN #results b on (a.ItemId = b.ItemId)
并合并:
MERGE INTO Table_1 a
USING #results b
ON (a.ItemId = b.temId)
WHEN MATCHED THEN UPDATE SET a.StatusId = b.StatusId;
但我似乎总能得到回应:
Msg 512, Level 16, State 1, Procedure trg_dbo_PBITree_TreeModel_HierarchicalEscalationHistory_InsertNode, Line 7 [Batch Start Line 11] Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
当我查询有问题的两个 table 时 (#results & Table_1),它们都具有 1 对 1 的关系,根本看不出它可能隐藏某种子查询的位置! ?
任何人都可以快速帮助解决这个问题吗?这似乎是 1-0-1 的东西,正在烤我的汉堡!
-- 编辑--
我查看了错误消息中提到的触发器,因为有人建议它可能正在尝试处理单行更新,而不是我正在做的多行更新。对我来说没什么特别的...?
ALTER TRIGGER [dbo].[trg_dbo_PBITree_TreeModel_HierarchicalEscalationHistory_InsertNode]
ON [dbo].[Table_1]
AFTER UPDATE
AS
BEGIN
-- NodeModelInsertOrUpdateTPH
IF ((select [Item] from inserted) = 'X')
BEGIN
UPDATE tx
SET
tx.LastUpdatedBy = i.LastUpdatedBy,
tx.LastUpdatedAt = i.LastUpdatedAt
FROM
[dbo].[Table_X] tx,
inserted i
WHERE
tx.OtherItemId = i.OtherItemId
END
END
有人有什么想法吗?
你的触发器是这里的问题。您的 IF 语句有一个查询 return 多于 1 行,结果就是确切的消息。您应该使触发器能够容忍多行操作。这是相同的逻辑,但它可以处理任意数量的正在更新的行。
ALTER TRIGGER [dbo].[trg_dbo_PBITree_TreeModel_HierarchicalEscalationHistory_InsertNode]
ON [dbo].[Table_1]
AFTER UPDATE
AS
BEGIN
UPDATE tx
SET
tx.LastUpdatedBy = i.LastUpdatedBy,
tx.LastUpdatedAt = i.LastUpdatedAt
FROM
[dbo].[Table_X] tx
join inserted i ON tx.OtherItemId = i.OtherItemId
where i.Item = 'X'
END