Thursday, January 5, 2017

SQL Server - Merge statement in SQL Server

-- Create Target Table
CREATE TABLE dbo.Sales (Id int,
                    SalesAmount money);
INSERT INTO dbo.Sales VALUES(1,10.99);

-- Create Source Table
CREATE TABLE dbo.NewSalesNAdjustments(Id int,
                                      SalesAmount money);
INSERT INTO dbo.NewSalesNAdjustments VALUES (1, 12.99);
INSERT INTO dbo.NewSalesNAdjustments VALUES (2, 5.99);
go

merge sales t ---target
using newsalesNAdjustments as S ---Source
on t.id=s.id
when matched then ---update
update set t.salesamount=s.salesamount
when not matched then ---insert
insert (id,salesamount) values(s.id,s.salesamount);
---verify upsert operation
select * from sales
go
merge sales t
using NewsalesNadjustments as s
on t.id = s.id
when matched then update set t.salesamount=s.salesamount
when not matched then insert (id,salesamount) values(s.id,s.salesamount);
select * from sales

No comments:

Search This Blog

DAX - Grouping on multiple columns and the count

Please go thorugh the below URL for entire soultion. http://community.powerbi.com/t5/Desktop/DAX-Grouping-on-multiple-columns-and-the-cou...