Merge in MSSQL

less than 1 minute read

7년 동안 외웠다 까먹기를 반복하다 지쳐버린 나…

DECLARE @INHAID  NVARCHAR(50) = '217255'
DECLARE @ACCOUNT NVARCHAR(50) = 'what@mail.com'

MERGE hpDEPTDB.dbo.OFFICE_ACCOUNT A
USING (SELECT @INHAID as INHAID) B
ON A.INHAID = B.INHAID
WHEN MATCHED THEN
  UPDATE
  SET A.OFFICE_ACCOUNT = @ACCOUNT,
      A.LDATE          = GETDATE()
WHEN NOT MATCHED THEN
  INSERT (INHAID, OFFICE_ACCOUNT, LDATE, CDATE)
  VALUES (@INHAID, @ACCOUNT, GETDATE(), GETDATE())
;

Tags:

Categories:

Updated:

Leave a comment