SQL UPDATE statement with WHERE EXISTS

I believe this will give you same result.

UPDATE ea
  SET GTL_UW_APPRV_DT = DNTL_UW_APPRV_DT
  FROM EMPLOYER_ADDL AS ea
  INNER JOIN EMP_PLAN_LINE_INFO AS ep
      ON ep.GR_NBR = ea.GR_NBR
      AND ep.LINE_CD = 50

UPDATE ea
  SET GTL_UW_APPRV_DT = DNTL_UW_APPRV_DT
  FROM EMPLOYER_ADDL AS ea
  WHERE EXISTS
  (
    SELECT 1
      FROM EMP_PLAN_LINE_INFO AS ep
      WHERE ep.GR_NBR = ea.GR_NBR
      AND ep.LINE_CD = 50
  );

However, if you can derive this information from a query, why update the table? Seems like this will have to be run constantly else risk being out of date.


Assuming that GR_NBR is a PK and distinct in EMP_Plan_line_Info:

UPDATE  EA
SET     GTL_UW_APPRV_DT = DNTL_UW_APPRV_DT
FROM    EMPLOYER_ADDL EA
        INNER JOIN EMP_PLAN_LINE_INFO EP
            ON EP.GR_NBR = EA.GR_NBR
               AND EP.LINE_CD = 50

What about this?

UPDATE ea  
SET ea.GTL_UW_APPRV_DT = ea.DNTL_UW_APPRV_DT 
FROM EMPLOYER_ADDL ea
    INNER JOIN EMP_PLAN_LINE_INFO ei ON(ei.GR_NBR = ea.GR_NBR)
WHERE 
ei.LINE_CD = 50

Tags:

Sql

Sql Server