[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2014-03-15
2
Medium Priority
?
353 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 27

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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
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 and will be exposed to the many uses the SELECT statement has.

650 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