module 10 merging data and passing tables
DESCRIPTION
Module 10 Merging Data and Passing Tables. Module Overview. Using the MERGE Statement Implementing Table Types Using Table Types As Parameters. Lesson 1: Using the MERGE Statement. MERGE Statement WHEN MATCHED WHEN NOT MATCHED BY TARGET WHEN NOT MATCHED BY SOURCE - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Module 10 Merging Data and Passing Tables](https://reader036.vdocument.in/reader036/viewer/2022062501/568161bc550346895dd19577/html5/thumbnails/1.jpg)
Module 10Merging Data and
Passing Tables
![Page 2: Module 10 Merging Data and Passing Tables](https://reader036.vdocument.in/reader036/viewer/2022062501/568161bc550346895dd19577/html5/thumbnails/2.jpg)
Module Overview• Using the MERGE Statement• Implementing Table Types• Using Table Types As Parameters
![Page 3: Module 10 Merging Data and Passing Tables](https://reader036.vdocument.in/reader036/viewer/2022062501/568161bc550346895dd19577/html5/thumbnails/3.jpg)
Lesson 1: Using the MERGE Statement• MERGE Statement• WHEN MATCHED• WHEN NOT MATCHED BY TARGET• WHEN NOT MATCHED BY SOURCE• OUTPUT Clause and $action• MERGE Determinism and Performance• Demonstration 1A: MERGE Statement
![Page 4: Module 10 Merging Data and Passing Tables](https://reader036.vdocument.in/reader036/viewer/2022062501/568161bc550346895dd19577/html5/thumbnails/4.jpg)
MERGE Statement
MERGE INTO dbo.Employee AS eUSING dbo.EmployeeUpdate AS euON e.EmployeeID = eu.EmployeeID…
• Modifies data in a target table (or updatable view or CTE) based on the results of a join with a source table
• Commonly used to populate data warehouses INSERT data if not already present UPDATE data if already present
• Target table plus a source rowset• Must specify how the source and target are joined
![Page 5: Module 10 Merging Data and Passing Tables](https://reader036.vdocument.in/reader036/viewer/2022062501/568161bc550346895dd19577/html5/thumbnails/5.jpg)
WHEN MATCHED
MERGE INTO dbo.Employee AS eUSING dbo.EmployeeUpdate AS euON e.EmployeeID = eu.EmployeeIDWHEN MATCHED THEN UPDATE SET e.FullName = eu.FullName, e.EmploymentStatus = eu.EmploymentStatus
• Clause that defines the action to be taken when the row in the source is found in the target
• Specifies the data modifications to take place – can be INSERT, UPDATE or DELETE
• Two WHEN MATCHED clauses can be included – needs an extra predicate on the first WHEN MATCHED AND s.Quantity > 0 One must be an UPDATE, the other a DELETE
![Page 6: Module 10 Merging Data and Passing Tables](https://reader036.vdocument.in/reader036/viewer/2022062501/568161bc550346895dd19577/html5/thumbnails/6.jpg)
WHEN NOT MATCHED BY TARGET
MERGE INTO dbo.Employee AS eUSING dbo.EmployeeUpdate AS euON e.EmployeeID = eu.EmployeeIDWHEN MATCHED THEN UPDATE SET e.FullName = eu.FullName, e.EmploymentStatus = eu.EmploymentStatusWHEN NOT MATCHED THEN INSERT (EmployeeID,FullName,EmploymentStatus) VALUES (eu.EmployeeID,eu.FullName,eu.EmploymentStatus);
• Clause that defines the action to be taken when the row in the source cannot be found in the target
• The words BY TARGET are optional and often omitted
![Page 7: Module 10 Merging Data and Passing Tables](https://reader036.vdocument.in/reader036/viewer/2022062501/568161bc550346895dd19577/html5/thumbnails/7.jpg)
WHEN NOT MATCHED BY SOURCE
MERGE INTO dbo.Employee AS eUSING dbo.EmployeeUpdate AS euON e.EmployeeID = eu.EmployeeIDWHEN MATCHED THEN UPDATE SET e.FullName = eu.FullName, e.EmploymentStatus = eu.EmploymentStatusWHEN NOT MATCHED THEN INSERT (EmployeeID,FullName,EmploymentStatus) VALUES (eu.EmployeeID,eu.FullName,eu.EmploymentStatus)WHEN NOT MATCHED BY SOURCE THEN DELETE;
• Clause that defines the action to be taken for rows in the target that were not supplied in the source
• Not commonly used but typically involve a DELETE
![Page 8: Module 10 Merging Data and Passing Tables](https://reader036.vdocument.in/reader036/viewer/2022062501/568161bc550346895dd19577/html5/thumbnails/8.jpg)
OUTPUT Clause and $action
MERGE INTO dbo.Employee AS eUSING dbo.EmployeeUpdate AS euON e.EmployeeID = eu.EmployeeIDWHEN MATCHED THEN UPDATE SET e.FullName = eu.FullName, e.EmploymentStatus = eu.EmploymentStatusWHEN NOT MATCHED THEN INSERT (EmployeeID,FullName,EmploymentStatus) VALUES (eu.EmployeeID,eu.FullName,eu.EmploymentStatus)OUTPUT $action,inserted.EmployeeID,deleted.EmployeeID;
• OUTPUT clause can also be used with MERGE• $action used to determine the action taken for each row
![Page 9: Module 10 Merging Data and Passing Tables](https://reader036.vdocument.in/reader036/viewer/2022062501/568161bc550346895dd19577/html5/thumbnails/9.jpg)
MERGE Determinism and Performance• Matching source and destination rows using MERGE is not
the same as matching with UPDATE• UPDATE can allow a source row to update multiple target
rows• MERGE joins must be deterministic – an incoming row
must only match a single target row • MERGE performs well as it makes only a single pass
through the data Often better performance than separate INSERT, UPDATE and
DELETE statements
![Page 10: Module 10 Merging Data and Passing Tables](https://reader036.vdocument.in/reader036/viewer/2022062501/568161bc550346895dd19577/html5/thumbnails/10.jpg)
Demonstration 1A: MERGE StatementIn this demonstration you will see:• How to use the MERGE statement• How to use the OUTPUT clause with the MERGE statement• How to perform optional updates with MERGE• How to use MERGE as a composable query• How to use the VALUES clause as a MERGE source
![Page 11: Module 10 Merging Data and Passing Tables](https://reader036.vdocument.in/reader036/viewer/2022062501/568161bc550346895dd19577/html5/thumbnails/11.jpg)
Lesson 2: Implementing Table Types• Reducing Round-Trip Overhead• Options for Passing Lists as Parameters• Demonstration 2A: Passing Delimited Lists• Introduction to the TABLE Type• Populating TABLE Types with Row Constructors• Demonstration 2B: TABLE Types and Row Constructors
![Page 12: Module 10 Merging Data and Passing Tables](https://reader036.vdocument.in/reader036/viewer/2022062501/568161bc550346895dd19577/html5/thumbnails/12.jpg)
Reducing Round-Trip Overhead• Round-trip to from an application to SQL Server and back
can be substantial Common aim to minimize the impact of round trips
• Many operations include many round-trips and cause transactions to last longer than desirable
• Add an order Start Transaction Save Order Header Save Order Detail Line 1 Save Order Detail Line 2 Save Order Detail Line 3 Commit Transaction
![Page 13: Module 10 Merging Data and Passing Tables](https://reader036.vdocument.in/reader036/viewer/2022062501/568161bc550346895dd19577/html5/thumbnails/13.jpg)
Options for Passing Lists as Parameters• Prior to SQL Server 2008, options for passing lists of values
in a single call were limited• Delimited Lists (often comma-delimited)
Required writing custom parsing logic Loose structure Loose data typing
• XML Also requires some complex parsing logic Semi-structured Loose data typing without schemas
![Page 14: Module 10 Merging Data and Passing Tables](https://reader036.vdocument.in/reader036/viewer/2022062501/568161bc550346895dd19577/html5/thumbnails/14.jpg)
Demonstration 2A: Passing Delimited ListsIn this demonstration, you will see• How to query a table-valued function that performs list
parsing• How the function allows for different delimiters• How to use the function in a join• How common errors can occur with delimited lists
![Page 15: Module 10 Merging Data and Passing Tables](https://reader036.vdocument.in/reader036/viewer/2022062501/568161bc550346895dd19577/html5/thumbnails/15.jpg)
Introduction to the TABLE Type
CREATE TYPE dbo.CustomerBalanceAS TABLE ( CustomerID int, CurrentBalance decimal(18,2));GO
• Earlier versions of SQL Server allowed variables to be of type TABLE and to allow definition of the structure of the table
• SQL Server allows the creation of table data type definitions Can be used for variables Can be used for parameters
![Page 16: Module 10 Merging Data and Passing Tables](https://reader036.vdocument.in/reader036/viewer/2022062501/568161bc550346895dd19577/html5/thumbnails/16.jpg)
Populating TABLE Types with Row Constructors
DECLARE @Balance dbo.CustomerBalance;
INSERT INTO @Balance VALUES (12,14.50),(15,13.75),(22,19.50);
• SQL Server allows the population of multiple rows of data into a table variable via row constructors Can be used with both INSERT and MERGE statements
• Multi-row inserts are atomic operations All inserts occur or none occur
![Page 17: Module 10 Merging Data and Passing Tables](https://reader036.vdocument.in/reader036/viewer/2022062501/568161bc550346895dd19577/html5/thumbnails/17.jpg)
Demonstration 2B: TABLE Types and Row ConstructorsIn this demonstration you will see:• How to work with row constructors• How to declare a table data type• How to work with variables with user-defined table data
types
![Page 18: Module 10 Merging Data and Passing Tables](https://reader036.vdocument.in/reader036/viewer/2022062501/568161bc550346895dd19577/html5/thumbnails/18.jpg)
Lesson 3: Using TABLE Types As Parameters• TABLE Input Parameters for Stored Procedures• Using Row Constructors to Populate Parameters• Demonstration 3A: Passing Tables to Stored Procedures
![Page 19: Module 10 Merging Data and Passing Tables](https://reader036.vdocument.in/reader036/viewer/2022062501/568161bc550346895dd19577/html5/thumbnails/19.jpg)
TABLE Input Parameters for Stored Procedures
CREATE TYPE dbo.SalesDetails AS TABLE( Description varchar(50), Price decimal(18,2));GOCREATE PROCEDURE dbo.SalesInsert @CustomerID int, @PurchaseOrderNumber varchar(20), @SalesDetails dbo.SalesDetails READONLY, @SaleID int OUTPUTAS BEGIN
• User-defined table data types can be used as input parameters for stored procedures
• Not able to be used for output parameters• Must include READONLY in the definition
![Page 20: Module 10 Merging Data and Passing Tables](https://reader036.vdocument.in/reader036/viewer/2022062501/568161bc550346895dd19577/html5/thumbnails/20.jpg)
Using Row Constructors to Populate Parameters
DECLARE @SaleID int;DECLARE @SalesDetails dbo.SalesDetails;
INSERT INTO @SalesDetails VALUES('Product 1',12.3), ('Product 2',14.66), ('Product 3',122.35);EXEC dbo.SalesInsert 12,'BigOrder',@SalesDetails,@SaleID OUTPUT;
• Table variables are created to provide the parameters to pass to stored procedures with table-valued parameters
• Row constructors are ideal for populating the table variables prior to making the stored procedure call
![Page 21: Module 10 Merging Data and Passing Tables](https://reader036.vdocument.in/reader036/viewer/2022062501/568161bc550346895dd19577/html5/thumbnails/21.jpg)
Demonstration 3A: Passing Tables to Stored ProceduresIn this demonstration you will see:• How traditional stored procedure calls often involve
multiple round trips to the server• How to declare a table data type• How to use the table data type to avoid round trips• How to view catalog information about the table data
types by querying the sys.types and sys.table_types system views
![Page 22: Module 10 Merging Data and Passing Tables](https://reader036.vdocument.in/reader036/viewer/2022062501/568161bc550346895dd19577/html5/thumbnails/22.jpg)
Lab 10: Passing Tables and Merging Data• Exercise 1: Create a Table Type • Exercise 2: Use a Table Type Parameter• Challenge Exercise 3: Use a Table Type with MERGE (Only if
time permits)
Logon information
Estimated time: 45 minutes
Virtual machine 623XB-MIA-SQLUser name AdventureWorks\AdministratorPassword Pa$$w0rd
![Page 23: Module 10 Merging Data and Passing Tables](https://reader036.vdocument.in/reader036/viewer/2022062501/568161bc550346895dd19577/html5/thumbnails/23.jpg)
Lab ScenarioIn earlier versions of SQL Server, passing lists of values to stored procedures was a challenge. SQL Server 2008 introduced the table type and table-valued parameters. In this lab, you will create a replacement stored procedure Reports.GetProductsByColorList_Test that uses a table-valued parameter to replace an existing stored procedure Reports.GetProductsByColorList that was based on passing a comma-delimited list of values. You will then create a new procedure that processes complete rows of data and performs updates using the MERGE statement.
![Page 24: Module 10 Merging Data and Passing Tables](https://reader036.vdocument.in/reader036/viewer/2022062501/568161bc550346895dd19577/html5/thumbnails/24.jpg)
Lab Review• What is the purpose of the OUTPUT clause?• In the values returned by an OUTPUT clause, how can we
tell if an INSERT, UPDATE or DELETE occurred?
![Page 25: Module 10 Merging Data and Passing Tables](https://reader036.vdocument.in/reader036/viewer/2022062501/568161bc550346895dd19577/html5/thumbnails/25.jpg)
Module Review and Takeaways• Review Questions• Best Practices