我之前已经看过这个问题,但是无法确定如何更改此查询以更新实际上在WHERE子句中使用的SELECT语句中返回的字段(clientid,program,startdate和enddate) 。如何执行此操作的任何示例都将非常有帮助。谢谢。
UPDATE baycare_homecare
SET
IsApproved = 1
,ProcessDate = GETDATE()
,clientid = c.ClientID
,program = cp1.Description
,startdate = cp.startdate
,enddate = cp.enddate
WHERE
Processdate IS NULL
AND baycare_homecare.patient_ssn IN
(
SELECT
c.socialsecuritynumber
FROM
carescope_live.dbo.client AS c
,carescope_live.dbo.client_program AS cp
LEFT JOIN
carescope_live.dbo.Code_Program AS cp1
ON
cp1.ULink = cp.ProgramULink
WHERE
c.ulink = cp.clientulink
AND baycare_homecare.service_date
BETWEEN
DATEADD(ss, 1, DATEADD(d, -1, cp.startdate))
AND
DATEADD(ss, -1, DATEADD(d, +1, cp.enddate))
);
答案 0 :(得分:0)
我无法测试,但是我相信这就是你所追求的:
UPDATE bh
SET IsApproved=1
,ProcessDate = GETDATE()
,clientid = c.ClientID
,program = cp1.Description
,startdate = cp.startdate
,enddate = cp.enddate
FROM baycare_homecare bh
JOIN carescope_live.dbo.client AS c ON bh.patient_ssn = c.socialsecuritynumber
JOIN carescope_live.dbo.client_program AS cp ON c.ulink = cp.clientulink
AND bh.service_date BETWEEN DATEADD(ss, 1, DATEADD(d, -1, cp.startdate)) AND DATEADD(ss, -1, DATEADD(d, +1, cp.enddate)) --this isn't SARGable
LEFT JOIN carescope_live.dbo.Code_Program AS cp1 ON cp1.ULink = cp.ProgramULink
WHERE Processdate IS NULL;
请注意,您要做的是一个带有您的FROM
和别名的“正常” JOIN
子句。然后,为您的UPDATE
子句引用别名。然后,您可以引用其他对象中具有各自别名的列,就像在SELECT
中一样。
答案 1 :(得分:0)
您可以这样做
UPDATE b
SET IsApproved = 1
, ProcessDate = GETDATE()
, clientid = a.ClientID
, program = a.Description
, startdate = a.startdate
, enddate = a.enddate
FROM baycare_homecare b
JOIN (
SELECT c.socialsecuritynumber, c.ClientID, program = cp1.Description, startdate = cp.startdate, enddate = cp.enddate
FROM carescope_live.dbo.client AS c
JOIN carescope_live.dbo.client_program AS cp ON c.ulink = cp.clientulink
LEFT JOIN carescope_live.dbo.Code_Program AS cp1 ON cp1.ULink = cp.ProgramULink
WHERE baycare_homecare.service_date BETWEEN DATEADD(ss, 1, DATEADD(d, -1, cp.startdate)) AND DATEADD(ss, -1, DATEADD(d, +1, cp.enddate))
) a ON a.socialsecuritynumber = b.patient_ssn
WHERE b.ProcessDate IS NULL
更新的查询:
UPDATE b
SET IsApproved = 1
, ProcessDate = GETDATE()
, clientid = a.ClientID
, program = a.Description
, startdate = a.startdate
, enddate = a.enddate
FROM baycare_homecare b
JOIN (
SELECT c.socialsecuritynumber, c.ClientID, program = cp1.Description, startdate = cp.startdate, enddate = cp.enddate
FROM carescope_live.dbo.client AS c
JOIN carescope_live.dbo.client_program AS cp ON c.ulink = cp.clientulink
LEFT JOIN carescope_live.dbo.Code_Program AS cp1 ON cp1.ULink = cp.ProgramULink
) a ON a.socialsecuritynumber = b.patient_ssn AND b.service_date BETWEEN DATEADD(ss, 1, DATEADD(d, -1, a.startdate)) AND DATEADD(ss, -1, DATEADD(d, +1, a.enddate))
WHERE b.ProcessDate IS NULL