-- 계약기간이 겹치는 기존계약의 계약종료일 갱신
UPDATE ESPINFO SET APPLY_ED = CASE WHEN ESPINFO.APPLY_ED >= T.APPLY_SD
THEN CONVERT(VARCHAR, DATEADD(DAY, -1, T.APPLY_SD), 112)
ELSE ESPINFO.APPLY_ED
END
FROM (SELECT HD.SYS_ID
, HD.COMP_CD
, HD.PLT_CD
, DT.ITEM_CD
, HD.VD_CD
, HD.OPER_ORG_SN
, HD.APPLY_SD
FROM ESPINHD HD WITH (NOLOCK)
JOIN ESPINDT DT WITH (NOLOCK) ON HD.SYS_ID = DT.SYS_ID
AND HD.PRICE_CNTR_NO = DT.PRICE_CNTR_NO
WHERE HD.SYS_ID = '100'
AND HD.PRICE_CNTR_NO = 'PC121000074'
AND DT.STS <> 'D') T
WHERE T.SYS_ID = ESPINFO.SYS_ID
AND T.COMP_CD = ESPINFO.COMP_CD
AND T.PLT_CD = ESPINFO.PLT_CD
AND T.ITEM_CD = ESPINFO.ITEM_CD
AND T.VD_CD = ESPINFO.VD_CD
AND T.OPER_ORG_SN = ESPINFO.OPER_ORG_SN;
WITH (NOLOCK) 은 dirty read를 하기 때문에 위 옵션은 제거 하는게 맞다.
읽기일관성을 위한 옵션 참조
https://blog.daonelab.com/post/26/1546/