Date post: | 24-Jun-2015 |
Category: |
Data & Analytics |
Upload: | sql-server-sri-lanka-user-group |
View: | 47 times |
Download: | 3 times |
www.cammanagementsolutions.com
19-Oct-2011
Presenters – Dinesh Priyankarahttp://dinesql.blogspot.com/
ELIMINATING ROUNDTRIPS:
TABLE DATA TYPE AND MERGE
www.cammanagementsolutions.com
ROUNDTRIPS
Discussion
www.cammanagementsolutions.com
TABLE DATA TYPE
• SQL 2000– TABLE variable type
• SQL 2008– Ability to create user-defined TABLE data type
– Can be used for variables
– Can be used for parameters
www.cammanagementsolutions.com
TABLE DATA TYPE SUPPORTS
• Can use Row Constructor for inserting
• Can use with both INSERT and MERGE
• Multi-Row inserts is atomic
• Can use as a parameter for Stored Procedure– Cannot be used as an Output Parameter
– Must use READONLY with the definition
www.cammanagementsolutions.com
TABLE DATA TYPE
DEMO
www.cammanagementsolutions.com
MERGE STATEMENT
• Perform UPSERT operation– INSERT, UPDATE and DELETE
• Performs against Target using a source– Target: Table or View
– Source: Table, View, Derived Table, CTE, Table Function
• Atomic– Triggers will be fired only once
www.cammanagementsolutions.com
WHEN MATCHED THEN CLAUSE
• Allows to performs an action when the row is the source is found in the target
• Supported actions are: UPDATE and DELETE
• Supports two WHEN MATCHED clauses– One with AND predicate
– Second with or without AND predicate
– One must be UPDATE and other must be DELETE
www.cammanagementsolutions.com
WHEN NOT MATCHED BY TARGET AND BY SOURCE THEN CLAUSE
• BY TARGET – Default– Allows to performs an action when the row in the source is
NOT found in the target
– Only supported action is INSERT
• BY SOURCE– Allows to performs an action against rows in the target that
are not supplied with the source
– DELETE and UPDATE are supported
www.cammanagementsolutions.com
OUTPUT CLAUSE AND $ACTION
• OUTPUT Clause is available with MERGE
• $Action returns the operation performed
• Useful for logging/auditing
www.cammanagementsolutions.com
MERGE
DEMO
www.cammanagementsolutions.com
RESOURCES
• http://dinesql.blogspot.com/2012/01/table-type-and-merge-statement.html
www.cammanagementsolutions.com
Q & A