Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
MERGE with OUTPUT
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
-- MERGE with OUTPUT -- inserted.* returns all the inseted columns create table MyOrders( orderid INT NOT NULL PRIMARY KEY, custid INT NOT NULL, empid INT NOT NULL, orderdate DATE NOT NULL ) MERGE INTO MyOrders AS TGT USING (VALUES(1, 70, 1, '20061218'), (2, 70, 7, '20070429'), (3, 70, 7, '20070820'), (4, 70, 3, '20080114'), (5, 70, 1, '20080226'), (6, 70, 2, '20080410')) AS SRC(orderid, custid, empid, orderdate ) ON SRC.orderid = TGT.orderid WHEN MATCHED AND ( TGT.custid <> SRC.custid OR TGT.empid <> SRC.empid OR TGT.orderdate <> SRC.orderdate) THEN UPDATE SET TGT.custid = SRC.custid, TGT.empid = SRC.empid, TGT.orderdate = SRC.orderdate WHEN NOT MATCHED THEN INSERT VALUES(SRC.orderid, SRC.custid, SRC.empid, SRC.orderdate) WHEN NOT MATCHED BY SOURCE THEN DELETE -- Code for output data in merge statement OUTPUT $action AS the_action, COALESCE(inserted.orderid, deleted.orderid) AS orderid,inserted.*; -- inserted.* returns all the inseted columns
View schema
Execution time: 0.02 sec, rows selected: 0, rows affected: 6, absolute service time: 0.17 sec
edit mode
|
history
|
discussion
the_action
orderid
orderid
custid
empid
orderdate
1
INSERT
1
1
70
1
12/18/2006 12:00:00 AM
2
INSERT
2
2
70
7
4/29/2007 12:00:00 AM
3
INSERT
3
3
70
7
8/20/2007 12:00:00 AM
4
INSERT
4
4
70
3
1/14/2008 12:00:00 AM
5
INSERT
5
5
70
1
2/26/2008 12:00:00 AM
6
INSERT
6
6
70
2
4/10/2008 12:00:00 AM