?
Solved

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

Posted on 2014-03-15
2
Medium Priority
?
351 Views
Last Modified: 2014-03-16
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
Comment
Question by:jazz__man
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 39932463
All the rows (name and address fields only) from table T are re-inserted into the same table (because of "on 0 = 1")
0
 
LVL 26

Accepted Solution

by:
Shaun Kline earned 2000 total points
ID: 39932468
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

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question