• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 355
  • Last Modified:

Can someone please simplify this merge sql server statement for me please

Can someone please explain this in lay mans terms to me please

MERGE T s
USING (
        SELECT TID, name, address
        FROM T [s]
      ) d on 0 = 1
WHEN NOT MATCHED
THEN INSERT (name, address)
    VALUES (name, address)
OUTPUT d.TID as oID, Inserted.TID as nID
INTO @t;
0
jazz__man
Asked:
jazz__man
1 Solution
 
Éric MoreauSenior .Net ConsultantCommented:
All the rows (name and address fields only) from table T are re-inserted into the same table (because of "on 0 = 1")
0
 
Shaun KlineLead Software EngineerCommented:
The MERGE statement uses the SELECT statement to compare against your target TABLE T, performing a match between source and target on 0 = 1. For any source data rows that do not match a target row, the source row is inserted into the target row. The ID from the source table and the newly created IID from the target table are then output into a variable table @t.

That said, because the match is performed using 0 = 1, this statement will basically insert every row in your source table into your target table. Since the source and target use the same table, you are duplicating every row in T.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now