Mark Johnston Development Lead - MSDN Microsoft Corporation TL42
Subscriptions MSDN Web Sites MSDN Magazine Offline Help This talk is only about this part of MSDN
Content Providers Online Services MSDN
Content Providers Visual Studio SQL Server Exchange Office Windows KB Articles … SQL Servers IIS/Web Datacenter 1 Datacenter 2 Transactional Replication All Content is Stored in SQL Server
MERGE employee as target USING employee_changes as src ON src.loginid = target.loginid WHEN MATCHED THEN /* UPDATE */ UPDATE SET first = src.first,last = src.last WHEN NOT MATCHED THEN /* INSERT */ INSERT (loginid,first,last) VALUES(src.loginid,src.first,src.last) WHEN NOT MATCHED BY SOURCE THEN DELETE; /* DELETE */
Employee_changes “source” table Applied to loginidfirstlastage JohnAJohnAdams32 SteveCSteveCox25 BillSBillSmith51 loginIdfirstlastage JohnAJohnAdams31 SteveCSteveCox25 BobMBobMartin48 INSERT DELETE loginidfirstlastage JohnAJohnAdams32 SteveCSteveCox25 BillSBillSmith51 BobMBobMartin48 Employee “Target” result after MERGE UPDATE untouched This process is known as Delta Processing Employee “Target“ Table
Mark Johnston Development Lead - MSDN Microsoft Corporation
Mark Johnston Development Lead - MSDN Microsoft Corporation
Source loginidtypephoneNum JohnAcell JohnAhome JohnAWork Target loginidtypephoneNum JohnAcell JohnAhome SteveChome SteveCcell Need to fix the ON clause of the MERGE statement Problem #1: Error: target row matches more than one source row
SourceTarget Need to properly define the scope of the target UPDATE untouched INSERT DELETE Problem #2: Inadvertent rows are being deleted loginidtypephoneNum JohnAcell JohnAhome JohnAWork loginidtypephoneNum JohnAcell JohnAhome JohnAWork SteveChome SteveCcell
;WITH target AS (SELECT ep.* FROM EmployeePhone AS ep JOIN (SELECT distinct loginId from #t) t ON t.loginId = ep.loginId) MERGE target USING (SELECT * FROM #t) AS SRC ON SRC.loginid = target.loginid AND SRC.Type = target.type WHEN MATCHED AND (target.phoneNum <> SRC.phoneNum) THEN UPDATE SET target.phoneNum=SRC.phoneNum WHEN NOT MATCHED THEN INSERT (loginid, type, phoneNum) VALUES (SRC.loginid, SRC.type, SRC.phoneNum) WHEN NOT MATCHED BY SOURCE THEN DELETE; Untouched
InsertDelete Update Subset Insert Update SourceTarget Delete Subset
Mark Johnston Development Lead - MSDN Microsoft Corporation
dbo.EmployeeTableType insert values (N’JohnA’,N’John’, N’Adams’) dbo.EmployeePhoneTableType insert values (N’JohnA’,N’cell’, N’ ’) insert values (N’JohnA’,N’home’, N’ ’) insert values (N’JohnA’,N’work’, N’ ’) exec dbo.mergeEmployeeAndPhoneWithTVP
exec contentInsertUpdate … /*if update content then delete rows in related tables associated with content */ exec contentPrimaryDocInsert … … exec contentImageInsert … … exec contentMetaDataInsert … … /* there are 10 content related tables */
exec /* contentTableType /* primaryDocTableType /* imageTableType /* metaDataTableType */ … /* All data related to the content is passed into one stored procedure */
Mark Johnston Development Lead - MSDN Microsoft Corporation
EmployeeInsert EmployeeUpdate EmployeeDelete Employee EmployeePhoneInsert EmployeePhoneUpdate EmployeePhoneDelete EmployeePhone
EmployeeMerge Employee EmployeePhoneMerge EmployeePhone
EmployeeMerge Employee EmployeePhone EmployeeAddress
contentMerge content contentPrimaryDoc contentMetaData contentImage exec /* contentTableType /* primaryDocTableType /* imageTableType /* metaDataTableType */ …
Mark Johnston Development Lead – MSDN Microsoft Corporation
Please fill out your evaluation for this session at: This session will be available as a recording at:
© 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
Mark Johnston Development Lead – MSDN Microsoft Corporation
IF exists (select * from Product where productNumber = ‘AR-5381’) BEGIN update product set … where productNumber = ‘AR-5381’ END ELSE BEGIN INSERT product (…) SELECT ‘AR-5381’, … END Race condition