+ All Categories
Home > Data & Analytics > Eliminating Roundtrips: TABLE Data Type and MERGE

Eliminating Roundtrips: TABLE Data Type and MERGE

Date post: 24-Jun-2015
Category:
Upload: sql-server-sri-lanka-user-group
View: 47 times
Download: 3 times
Share this document with a friend
Description:
201201 - Session 2 Eliminating Roundtrips: TABLE Data Type and MERGE Dinesh Priyankara (@dinesh_priya)
Popular Tags:
12
www.cammanagementsolutions.com 19-Oct- 2011 Presenters – Dinesh Priyankara http://dinesql.blogspot.com/ ELIMINATING ROUNDTRIPS: TABLE DATA TYPE AND MERGE
Transcript
Page 1: Eliminating Roundtrips: TABLE Data Type and MERGE

www.cammanagementsolutions.com

19-Oct-2011

Presenters – Dinesh Priyankarahttp://dinesql.blogspot.com/

ELIMINATING ROUNDTRIPS:

TABLE DATA TYPE AND MERGE

Page 2: Eliminating Roundtrips: TABLE Data Type and MERGE

www.cammanagementsolutions.com

ROUNDTRIPS

Discussion

Page 3: Eliminating Roundtrips: TABLE Data Type and MERGE

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

Page 4: Eliminating Roundtrips: TABLE Data Type and MERGE

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

Page 5: Eliminating Roundtrips: TABLE Data Type and MERGE

www.cammanagementsolutions.com

TABLE DATA TYPE

DEMO

Page 6: Eliminating Roundtrips: TABLE Data Type and MERGE

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

Page 7: Eliminating Roundtrips: TABLE Data Type and MERGE

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

Page 8: Eliminating Roundtrips: TABLE Data Type and MERGE

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

Page 9: Eliminating Roundtrips: TABLE Data Type and MERGE

www.cammanagementsolutions.com

OUTPUT CLAUSE AND $ACTION

• OUTPUT Clause is available with MERGE

• $Action returns the operation performed

• Useful for logging/auditing

Page 10: Eliminating Roundtrips: TABLE Data Type and MERGE

www.cammanagementsolutions.com

MERGE

DEMO

Page 12: Eliminating Roundtrips: TABLE Data Type and MERGE

www.cammanagementsolutions.com

Q & A


Recommended