我有一个运行良好的 Select 语句,产生 7636 行:
SELECT c.ClientId
FROM dbo.tblTreatment e
JOIN dbo.tblProgramAssessment pa
ON pa.TreatmentID = e.TreatmentId
JOIN #Client c
ON c.ClientId = e.ClientId
LEFT JOIN dbo.tblCessationOfTreatment ct
ON ct.TreatmentId = e.TreatmentId
LEFT JOIN dbo.tblClientGP m
ON m.ClientId = c.ClientId
WHERE e.IsOpen = 1
AND e.IsDeleted = 0
AND ct.CessationDate is null
AND c.IsDeceased = 0
我正在尝试更新这 7636 行,但它改为更新 7446 行。在我(有限的)理解中,这可能是由于 clientid 的某种原因。这是更新声明:
UPDATE
#Client
SET
ToMigrate = 1
, OpenTreatmentEpisodes = 1
WHERE
clientid in
(SELECT c.ClientId
FROM dbo.tblTreatment e
JOIN dbo.tblProgramAssessment pa
ON pa.TreatmentID = e.TreatmentId
JOIN #Client c
ON c.ClientId = e.ClientId
LEFT JOIN dbo.tblCessationOfTreatment ct
ON ct.TreatmentId = e.TreatmentId
LEFT JOIN dbo.tblClientGP m
ON m.ClientId = c.ClientId
WHERE e.IsOpen = 1
AND e.IsDeleted = 0
AND ct.CessationDate is null
AND c.IsDeceased = 0
)
如果我将“IN”更改为“=”,我会收到一条错误消息:子查询返回超过 1 个值。当子查询跟随 =、!=、<、<=、>、>= 或子查询用作表达式时,这是不允许的。该语句已终止。
1)我真的不明白为什么 7446 和 7636 行有区别。2) 我怎样才能只更新 select 语句中的 7636 行?
先感谢您!