Solved

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

Posted on 2014-03-15
2
341 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
2 Comments
 
LVL 69

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 500 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now