Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mark Johnston Development Lead - MSDN Microsoft Corporation TL42.

Similar presentations


Presentation on theme: "Mark Johnston Development Lead - MSDN Microsoft Corporation TL42."— Presentation transcript:

1 Mark Johnston Development Lead - MSDN Microsoft Corporation johnston.mark@microsoft.com TL42

2

3 Subscriptions MSDN Web Sites MSDN Magazine Offline Help This talk is only about this part of MSDN

4 Content Providers Online Services MSDN

5 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

6

7

8

9

10

11

12

13 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 */

14

15 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

16 Mark Johnston Development Lead - MSDN Microsoft Corporation johnston.mark@microsoft.com

17

18 Mark Johnston Development Lead - MSDN Microsoft Corporation johnston.mark@microsoft.com

19 Source loginidtypephoneNum JohnAcell1-555-456-7890 JohnAhome1-555-425-8080 JohnAWork1-555-112-1234 Target loginidtypephoneNum JohnAcell1-555-111-1111 JohnAhome1-555-425-8080 SteveChome1-800-333-3333 SteveCcell1-866-444-4444 Need to fix the ON clause of the MERGE statement Problem #1: Error: target row matches more than one source row

20 SourceTarget Need to properly define the scope of the target UPDATE untouched INSERT DELETE Problem #2: Inadvertent rows are being deleted loginidtypephoneNum JohnAcell1-555-456-7890 JohnAhome1-555-425-8080 JohnAWork1-555-112-1234 loginidtypephoneNum JohnAcell1-555-111-1111 JohnAhome1-555-425-8080 JohnAWork1-555-112-1234 SteveChome1-800-333-3333 SteveCcell1-866-444-4444

21 ;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

22 InsertDelete Update Subset Insert Update SourceTarget Delete Subset

23

24

25

26 Mark Johnston Development Lead - MSDN Microsoft Corporation johnston.mark@microsoft.com

27 declare @p1 dbo.EmployeeTableType insert into @p1 values (N’JohnA’,N’John’, N’Adams’) declare @p2 dbo.EmployeePhoneTableType insert into @p2 values (N’JohnA’,N’cell’, N’1-555-456-7890’) insert into @p2 values (N’JohnA’,N’home’, N’1-555-425-8080’) insert into @p2 values (N’JohnA’,N’work’, N’1-555-112-1234’) exec dbo.mergeEmployeeAndPhoneWithTVP @employee=@p1,@employeePhone=@p2

28

29 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 */

30 exec contentMerge @contentInfo /* contentTableType */,@primaryDoc /* primaryDocTableType */,@images /* imageTableType */,@metaData /* metaDataTableType */ … /* All data related to the content is passed into one stored procedure */

31

32

33

34 Mark Johnston Development Lead - MSDN Microsoft Corporation johnston.mark@microsoft.com

35

36 EmployeeInsert EmployeeUpdate EmployeeDelete Employee EmployeePhoneInsert EmployeePhoneUpdate EmployeePhoneDelete EmployeePhone

37 EmployeeMerge Employee EmployeePhoneMerge EmployeePhone

38 EmployeeMerge Employee EmployeePhone EmployeeAddress

39 contentMerge content contentPrimaryDoc contentMetaData contentImage exec contentMerge @contentInfo /* contentTableType */,@primaryDoc /* primaryDocTableType */,@images /* imageTableType */,@metaData /* metaDataTableType */ …

40

41

42 Mark Johnston Development Lead – MSDN Microsoft Corporation johnston.mark@microsoft.com

43 Please fill out your evaluation for this session at: This session will be available as a recording at: www.microsoftpdc.com

44 © 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.

45 Mark Johnston Development Lead – MSDN Microsoft Corporation johnston.mark@microsoft.com

46 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

47

48


Download ppt "Mark Johnston Development Lead - MSDN Microsoft Corporation TL42."

Similar presentations


Ads by Google