我正在尝试创建一个存储过程,该存储过程将更新某个表并使用一个int列表。相同的逻辑用于单个更新,但对于此表,需要更新多个行。
但是,出现此错误: “子查询返回的值超过1个。当子查询遵循=,!=,<,<=,>,> =或将子查询用作表达式时,不允许这样做。”
是否有任何方法可以使此合并适用于多行?还是我需要完全改变逻辑?
ALTER PROCEDURE [dbo].[P_ClientBalanceDetails_ExecuteComplex_RefreshFromClawbacks]
@ID AS dbo.IntList READONLY,
@WorkflowInstanceID int null,
@AdjustmentFileID int null,
@PeriodID int null
AS
BEGIN
SET NOCOUNT ON;
--AGENTS
MERGE INTO ClientBalanceDetails AS TARGET
USING (
SELECT
Clawbacks.ID,
Clawbacks.ClientBalanceID,
Clawbacks.AgentClientBalanceDetailID,
Clawbacks.ValueDate,
Clawbacks.ClientID,
Clawbacks.Amount,
Clawbacks.Reason,
Clawbacks.ConfirmationStatus,
Clawbacks.UnderProrationReview,
Clawbacks.CreatedBy,
Clawbacks.CreatedDate,
Clawbacks.UpdatedBy,
Clawbacks.UpdatedDate,
Clawbacks.DeletedBy,
Clawbacks.DeletedDate,
Clawbacks.PostBillingDisputeID,
T05_Periods.FirstDay FirstDay
FROM
Clawbacks WITH (NOLOCK)
INNER JOIN ClientBalances WITH (NOLOCK) ON Clawbacks.ClientBalanceID = ClientBalances.ID
INNER JOIN T05_Periods WITH (NOLOCK) ON ClientBalances.PeriodID = T05_Periods.ID
WHERE
ClientBalances.DeletedDate IS NULL
AND (@WorkflowInstanceID IS NULL OR Clawbacks.WorkflowInstanceID = @WorkflowInstanceID)
AND (@AdjustmentFileID IS NULL OR Clawbacks.AdjustmentFileID = @AdjustmentFileID)
AND (@PeriodID IS NULL OR T05_Periods.ID = @PeriodID)
AND (Clawbacks.ID IN (SELECT ID FROM @ID))
AND (Clawbacks.ConfirmationStatus IN (@ConfirmedStatus, @UnConfirmedStatus, @RejectedStatus) )
AND (Clawbacks.UnderProrationReview IS NULL OR Clawbacks.UnderProrationReview = 0)
) AS SOURCE
--Insert new compensations
ON SOURCE.AgentClientBalanceDetailID = TARGET.ID
答案 0 :(得分:0)
您的select语句返回具有相同SOURCE.AgentClientBalanceDetailID的多个记录。如果源记录重复,则添加DISTINCT关键字将解决此问题。
如果具有相同AgentClientBalanceDetailID的记录甚至只改变一列,则说明您的逻辑存在缺陷,数据库将不允许合并。他们这样做是因为SQL必须始终保持一致,而不管记录的顺序如何。同一键(AgentClientBalanceDetailID)的列值各不相同,这意味着您将根据选择结果集中记录的顺序获得不同的更新结果。