Friday, March 23, 2012

MERGE

Hi all,
What would be SQL Server's version of this Oracle code:
****************************************
*********************
MERGE INTO destination_table dest
USING (SELECT col1, col2, col3 FROM source_table) source
ON (dest.col1 = source.col1)
WHEN MATCHED THEN
UPDATE SET dest.col2 = source.col2,
dest.col3 = source.col3
WHEN NOT MATCHED THEN
INSERT (dest.col1, dest.col2, dest.col3)
VALUES (source.col1, source.col2, source.col3)
****************************************
**************************
Thanks in advance
GoranGoran Djuranovic (goran.djuranovic@.newsgroups.nospam) writes:
> What would be SQL Server's version of this Oracle code:
> ****************************************
*********************
> MERGE INTO destination_table dest
> USING (SELECT col1, col2, col3 FROM source_table) source
> ON (dest.col1 = source.col1)
> WHEN MATCHED THEN
> UPDATE SET dest.col2 = source.col2,
> dest.col3 = source.col3
> WHEN NOT MATCHED THEN
> INSERT (dest.col1, dest.col2, dest.col3)
> VALUES (source.col1, source.col2, source.col3)
> ****************************************
**************************
Alas, MERGE is implemented in SQL Server, so you will need to do:
UPDATE ...
INSERT ...
SELECT ...
WHERE NOT EXISTS
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||Thanks Erland.
"Erland Sommarskog" <esquel@.sommarskog.se> wrote in message
news:Xns97DC4CB7551Yazorman@.127.0.0.1...
> Goran Djuranovic (goran.djuranovic@.newsgroups.nospam) writes:
>
> Alas, MERGE is implemented in SQL Server, so you will need to do:
> UPDATE ...
> INSERT ...
> SELECT ...
> WHERE NOT EXISTS
> --
> Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
> Books Online for SQL Server 2005 at
> http://www.microsoft.com/technet/pr...oads/books.mspx
> Books Online for SQL Server 2000 at
> http://www.microsoft.com/sql/prodin...ions/books.mspxsql

No comments:

Post a Comment