MERGE INTO and CROSS JOIN
metalrayFeb 17 2012 — edited Feb 22 2012Hello Guys,
I have a bit of trouble understanding the MERGE I have written here.
All the code executes without errors. As far as I see,
the below MERGE SELECT is a Cartesian product/cross join and I remember that is not
good. The ON (join condition) seems to be only for the merge into table (T_CLAIMS_BB) and
the table that comes from the select (d).
merge into T_CLAIMS_BB t
using
(
SELECT sch_vt.SCH#,vert.CNO,vers.SubCNo,regv.ReturnNo,regv.SndReturnNo,sch_vt.MainProd,sch_vt.Prod
FROM mainschema.TCONTRACT vert, mainschema.TVERSCONTRACT vers, commonschema.VC_REGVEREINB regv, myschema.T_CLAIMS_VT sch_vt
) d
ON (
t.sch# = d.sch# AND
SUBSTR(UPPER(t.BOOKINGTEXT),1,2) = d.SndReturnNo AND d.SubCNo IS NULL
) -- Join
when matched then
update
set t.CNO = 'a',
t.ReturnNo = 'a',
t.MainProd = 'b',
t.Prod = 'c';
I wonder now, what is the advantage of using MERGE in my case - if there is one.
Finally I want to demonstrate how the whole thing would look without a MERGE:
SELECT vers.* FROM TVERSCONTRACT vers
LEFT OUTER JOIN myschema.T_CLAIMS_BB sch ON sch.SubCNo = vers.SubCNo
LEFT OUTER JOIN commonschema.VC_REGVEREINB regv ON sch.SCH# = regv.SCH# AND SUBSTR(UPPER(sch.BUCHTEXT),1,2) = regv.SndReturnNo AND sch.SubCNo IS NULL
LEFT OUTER JOIN myschema.T_CLAIMS_VT sch_vt ON sch.SCH# = sch_vt.SCH#