A Sample To use Update, InnerJoin, When/Case, Exists in SQL Server
DECLARE @SINKKNO varchar(11),
@KYOKAYMD varchar(8),
@LASTUPDATEID varchar(5),
@LASTUPDPGID varchar(7),
@SYANAISEQNO varchar(8),
@TUKAN_TOKUISAKI varchar(17),
@TUKAN_TOKUISAKI_INP varchar(12),
@NINUSIKIJI varchar(10),
@NINUSISECCD varchar(11),
@NINUSIREFNO varchar(15)
;
SET
@SINKKNO = '43935442710'
SET
@KYOKAYMD = '20170920'
SET
@LASTUPDATEID = 'batch'
SET
@LASTUPDPGID = 'BC02501'
SET
@SYANAISEQNO = '1199S002'
SET
@TUKAN_TOKUISAKI = '11200010358669000'
SET
@TUKAN_TOKUISAKI_INP = '1000071J0000'
SET
@NINUSIKIJI = 'NINUSIKIJI'
SET
@NINUSISECCD = 'NINUSISECCD'
SET
@NINUSIREFNO = 'NINUSIReference';
UPDATE
C_T_EXP
SET
[SINKKNO] = @SINKKNO,
[KYOKAYMD] = @KYOKAYMD,
[LASTUPDATEYMD] = GETDATE(),
[LASTUPDATEID] = @LASTUPDATEID,
[LASTUPDPGID] = @LASTUPDPGID,
[TUKAN_TOKUISAKI] = @TUKAN_TOKUISAKI,
[TUKAN_TOKUISAKI_INP] = @TUKAN_TOKUISAKI_INP,
[NINUSIKIJI] = CASE
WHEN(
P.[NINUSIKIJI] IS NULL
OR P.[NINUSIKIJI] = ''
) THEN @NINUSIKIJI
ELSE P.[NINUSIKIJI]
END,
[NINUSISECCD] = (CASE
WHEN(
P.[NINUSISECCD] IS NULL
OR P.[NINUSISECCD] = ''
) THEN @NINUSISECCD
ELSE P.[NINUSISECCD]
END),
[NINUSIREFNO] =( CASE
WHEN(
P.[NINUSIREFNO] IS NULL
OR P.[NINUSIREFNO] = ''
) THEN @NINUSIREFNO
ELSE P.[NINUSIREFNO]
END)
FROM C_T_EXP
INNER JOIN C_T_EXP P
ON C_T_EXP.USYUTDAINO = P.USYUTDAINO
WHERE
C_T_EXP.SYANAISEQNO = @SYANAISEQNO
AND C_T_EXP.DELSGN = 0
AND 1 = (
SELECT
COUNT(T1.USYUTDAINO)
FROM
[C_T_EXP] T1
WHERE
T1.SYANAISEQNO = @SYANAISEQNO
AND T1.DELSGN = 0
)
Love it, and you live without it