On 2024-09-09 14:02:50 +0100, Philip Hazelden wrote: > The MERGE docs[1] give this warning: > > > Only columns from the target table that attempt to match > > `data_source` rows should appear in `join_condition`. > > `join_condition` subexpressions that only reference the target > > table's columns can affect which action is taken, often in > > surprising ways. > > (The docs for upcoming v17 have the same line.) > > But when I tested this, it seems to work fine. For example, consider a > two-level primary key, where the source table implicitly has a fixed > value for one level: [...] The warning is a bit misleading, IMHO. I think what this is trying to say is that this is effectively data_source LEFT JOIN target ON condition, and any row from data_source not matched by condition will end up in the "NOT MATCHED" set. So you might insert rows from data_source which you thought you had excluded in the condition. So it's important to get the match right, and then decide what to do in the WHEN clauses. > merge into t1 using t2 > on t1.k2 = t2.k2 and t1.k1 = 1 > when matched then update set v = t2.v > when not matched then insert values (1, t2.k2, t2.v); I think that's ok. The t1.k1 = 1 is used to match rows from the target to the data source (for each row in the data source, find the rows in the target which have the same k2 and k1 = 1). But "columns from the target table that attempt to match data_source` rows" for me sort of sounds like those columns have to have a counterpart in the data_source, which k1 hasn't. Also maybe the order is the wrong way around? "Match rows in the target to rows in the data_source" would fit my mental model better. hp -- _ | Peter J. Holzer | Story must make more sense than reality. |_|_) | | | | | hjp@xxxxxx | -- Charles Stross, "Creative writing __/ | http://www.hjp.at/ | challenge!"
Attachment:
signature.asc
Description: PGP signature