DB2 合并更新插入出现 'Row not found for MERGE' 错误
DB2 merge upsert gets 'Row not found for MERGE' error
我正在尝试使用 MERGE 语句在 iSeries db2 上执行基本更新插入,类似于 Does DB2 have an "insert or update" statement? and http://db2performance.blogspot.com/2011/12/merge-make-your-upserts-quick.html 中所述。执行时,它给我 Row not found for MERGE. SQLSTATE=02000
而不是插入行。既然我在语句中有when not matched then insert
,为什么它会return错误而不是插入?我找遍了 SO 并没有看到这个特定问题。
这是我使用的语句:
merge into UFDFTRN as T using (
select * from UFDFTRN
where DFCNO = 354 and DFINV = 1179 and DFLC = 1 and DFDATE = '2017-01-31'
and DFSPLT = 0 and DFSEQ = 100
) as S on (
T.DFCNO = S.DFCNO and T.DFINV = S.DFINV and T.DFDATE = S.DFDATE and
T.DFSPLT = S.DFSPLT and T.DFSEQ = S.DFSEQ
) when matched then
update set DFSEQ = 1000, DFTRAN = 0, DFITEM = 'F224', DFRITM = '0',
DFDESC = 'DAIRY VTM PREMIX', DFQTY = 3, DFUM = '',DESIQU = 0, DFRTQU = 3,
DFUPR = 0, DFCTUP = 0, DFUCST = 0, DFOUCST = 0, DFAMT = 0, DFOAMT = 0, DFCODE = '',
DFURAT = '', DFCGCD = '0', DFCTNO = 0, DFADJITM = '', DFADJPCT = 0, DFMNFITM = '',
DFMNFRAT = '', DFMNFQTY = '0', DFMNFTQTY = '0'
when not matched then
insert (DFCNO, DFINV, DFLC, DFDATE, DFSPLT, DFSEQ, DFTRAN, DFITEM, DFRITM, DFDESC,
DFQTY, DFUM, DFSIQU, DFRTQU, DFUPR, DFCTUP, DFUCST, DFOUCST, DFAMT, DFOAMT, DFCODE,
DFURAT, DFCGCD, DFCTNO, DFADJITM, DFADJPCT, DFMNFITM, DFMNFRAT, DFMNFQTY, DFMNFTQTY
) values (
354, 1179, 1, '2017-01-31', 0, 1000, 0, 'F224', '0', 'DAIRY VTM PREMIX', 3, '', 0,
3, 0, 0, 0, 0, 0, 0, '', '', '0', 0, '', 0, '', '', '0', '0'
)
它可能看起来更像这样:
merge into UFDFTRN as T using (
select 354 DFCNO, 1179 DFINV, 1 DFLC, '2017-01-31' DFDATE, 0 DFSPLT, 100 DFSEQ
, 'DAIRY VTM PREMIX' f1 -- all other columns you might need
from sysibm.sysdummy1
) as S
on (
T.DFCNO = S.DFCNO and T.DFINV = S.DFINV and T.DFDATE = S.DFDATE and
T.DFSPLT = S.DFSPLT and T.DFSEQ = S.DFSEQ
)
when matched then
update set T.DFSEQ = S.DFSEQ, T.DFTRAN = S.DFTRAN, -- etc. etc.
when not matched then
insert (DFCNO, DFINV, ... -- etc. etc.
) values (
S.DFSNO, S.DFINV, ..., S.F1, ...-- etc. etc.
)
PS。未测试。
Mustaccio 具有正确的合并格式...
但正如我评论的那样,这是一种非常有趣的使用合并的方式。
就我个人而言,如果有 1 次,我会
update UFDFTRN
set (DFCNO, DFINV, DFLC, DFDATE, DFSPLT, DFSEQ, DFTRAN, DFITEM, DFRITM, DFDESC,
DFQTY, DFUM, DFSIQU, DFRTQU, DFUPR, DFCTUP, DFUCST, DFOUCST, DFAMT, DFOAMT, DFCODE,
DFURAT, DFCGCD, DFCTNO, DFADJITM, DFADJPCT, DFMNFITM, DFMNFRAT, DFMNFQTY, DFMNFTQTY
) = (
354, 1179, 1, '2017-01-31', 0, 1000, 0, 'F224', '0', 'DAIRY VTM PREMIX', 3, '', 0,
3, 0, 0, 0, 0, 0, 0, '', '', '0', 0, '', 0, '', '', '0', '0'
)
where DFCNO = 354 and DFINV = 1179 and DFLC = 1 and DFDATE = '2017-01-31'
and DFSPLT = 0 and DFSEQ = 100
如果因找不到记录而失败,只需将更新更改为插入
insert into UFDFTRN
(DFCNO, DFINV, DFLC, DFDATE, DFSPLT, DFSEQ, DFTRAN, DFITEM, DFRITM, DFDESC,
DFQTY, DFUM, DFSIQU, DFRTQU, DFUPR, DFCTUP, DFUCST, DFOUCST, DFAMT, DFOAMT, DFCODE,
DFURAT, DFCGCD, DFCTNO, DFADJITM, DFADJPCT, DFMNFITM, DFMNFRAT, DFMNFQTY, DFMNFTQTY
) values (
354, 1179, 1, '2017-01-31', 0, 1000, 0, 'F224', '0', 'DAIRY VTM PREMIX', 3, '', 0,
3, 0, 0, 0, 0, 0, 0, '', '', '0', 0, '', 0, '', '', '0', '0'
)
我正在尝试使用 MERGE 语句在 iSeries db2 上执行基本更新插入,类似于 Does DB2 have an "insert or update" statement? and http://db2performance.blogspot.com/2011/12/merge-make-your-upserts-quick.html 中所述。执行时,它给我 Row not found for MERGE. SQLSTATE=02000
而不是插入行。既然我在语句中有when not matched then insert
,为什么它会return错误而不是插入?我找遍了 SO 并没有看到这个特定问题。
这是我使用的语句:
merge into UFDFTRN as T using (
select * from UFDFTRN
where DFCNO = 354 and DFINV = 1179 and DFLC = 1 and DFDATE = '2017-01-31'
and DFSPLT = 0 and DFSEQ = 100
) as S on (
T.DFCNO = S.DFCNO and T.DFINV = S.DFINV and T.DFDATE = S.DFDATE and
T.DFSPLT = S.DFSPLT and T.DFSEQ = S.DFSEQ
) when matched then
update set DFSEQ = 1000, DFTRAN = 0, DFITEM = 'F224', DFRITM = '0',
DFDESC = 'DAIRY VTM PREMIX', DFQTY = 3, DFUM = '',DESIQU = 0, DFRTQU = 3,
DFUPR = 0, DFCTUP = 0, DFUCST = 0, DFOUCST = 0, DFAMT = 0, DFOAMT = 0, DFCODE = '',
DFURAT = '', DFCGCD = '0', DFCTNO = 0, DFADJITM = '', DFADJPCT = 0, DFMNFITM = '',
DFMNFRAT = '', DFMNFQTY = '0', DFMNFTQTY = '0'
when not matched then
insert (DFCNO, DFINV, DFLC, DFDATE, DFSPLT, DFSEQ, DFTRAN, DFITEM, DFRITM, DFDESC,
DFQTY, DFUM, DFSIQU, DFRTQU, DFUPR, DFCTUP, DFUCST, DFOUCST, DFAMT, DFOAMT, DFCODE,
DFURAT, DFCGCD, DFCTNO, DFADJITM, DFADJPCT, DFMNFITM, DFMNFRAT, DFMNFQTY, DFMNFTQTY
) values (
354, 1179, 1, '2017-01-31', 0, 1000, 0, 'F224', '0', 'DAIRY VTM PREMIX', 3, '', 0,
3, 0, 0, 0, 0, 0, 0, '', '', '0', 0, '', 0, '', '', '0', '0'
)
它可能看起来更像这样:
merge into UFDFTRN as T using (
select 354 DFCNO, 1179 DFINV, 1 DFLC, '2017-01-31' DFDATE, 0 DFSPLT, 100 DFSEQ
, 'DAIRY VTM PREMIX' f1 -- all other columns you might need
from sysibm.sysdummy1
) as S
on (
T.DFCNO = S.DFCNO and T.DFINV = S.DFINV and T.DFDATE = S.DFDATE and
T.DFSPLT = S.DFSPLT and T.DFSEQ = S.DFSEQ
)
when matched then
update set T.DFSEQ = S.DFSEQ, T.DFTRAN = S.DFTRAN, -- etc. etc.
when not matched then
insert (DFCNO, DFINV, ... -- etc. etc.
) values (
S.DFSNO, S.DFINV, ..., S.F1, ...-- etc. etc.
)
PS。未测试。
Mustaccio 具有正确的合并格式...
但正如我评论的那样,这是一种非常有趣的使用合并的方式。
就我个人而言,如果有 1 次,我会
update UFDFTRN
set (DFCNO, DFINV, DFLC, DFDATE, DFSPLT, DFSEQ, DFTRAN, DFITEM, DFRITM, DFDESC,
DFQTY, DFUM, DFSIQU, DFRTQU, DFUPR, DFCTUP, DFUCST, DFOUCST, DFAMT, DFOAMT, DFCODE,
DFURAT, DFCGCD, DFCTNO, DFADJITM, DFADJPCT, DFMNFITM, DFMNFRAT, DFMNFQTY, DFMNFTQTY
) = (
354, 1179, 1, '2017-01-31', 0, 1000, 0, 'F224', '0', 'DAIRY VTM PREMIX', 3, '', 0,
3, 0, 0, 0, 0, 0, 0, '', '', '0', 0, '', 0, '', '', '0', '0'
)
where DFCNO = 354 and DFINV = 1179 and DFLC = 1 and DFDATE = '2017-01-31'
and DFSPLT = 0 and DFSEQ = 100
如果因找不到记录而失败,只需将更新更改为插入
insert into UFDFTRN
(DFCNO, DFINV, DFLC, DFDATE, DFSPLT, DFSEQ, DFTRAN, DFITEM, DFRITM, DFDESC,
DFQTY, DFUM, DFSIQU, DFRTQU, DFUPR, DFCTUP, DFUCST, DFOUCST, DFAMT, DFOAMT, DFCODE,
DFURAT, DFCGCD, DFCTNO, DFADJITM, DFADJPCT, DFMNFITM, DFMNFRAT, DFMNFQTY, DFMNFTQTY
) values (
354, 1179, 1, '2017-01-31', 0, 1000, 0, 'F224', '0', 'DAIRY VTM PREMIX', 3, '', 0,
3, 0, 0, 0, 0, 0, 0, '', '', '0', 0, '', 0, '', '', '0', '0'
)