
Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
CTEs, recursive cts, except/intersect, order of select commands in t-sql
Typology: Cheat Sheet
1 / 1
This page cannot be seen from the preview
Don't miss anything!

http://www.MidnightDBA.com/Jen Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 United States License.
1. FROM table 2. ON join condition
; WITH cteName ( columnList ) AS ( SELECT statement ) SELECT columns FROM cteName INNER JOIN table ON condition
; WITH cteName ( columnList ) AS ( -- Anchor statement: SELECT columns FROM table … UNION ALL -- Recursion statement: SELECT columns FROM table … INNER JOIN cteName ON … ) SELECT columns FROM cteName
/* Aggregate functions include COUNT, MIN, MAX, AVG, ROW_COUNT(), etc. */ SELECT agg_func ( col1 ) OVER(), agg_func ( col1 ) OVER(PARTITION BY col 2 ), columns FROM table …
/* Table2 holds detail rows for Table1; e.g., order details to order headers. */ SELECT columns, colname = STUFF( ( SELECT ','
SELECT col1, col2 FROM Table EXCEPT SELECT col3, col4 FROM Table SELECT col1, col2 FROM Table INTERSECT SELECT col3, col4 FROM Table
DECLARE @Changes TABLE(Change VARCHAR(20)) ; MERGE INTO DestTable USING ( SELECT from sourceTable ) AS Source ( columnList ) ON DestTable.ID = Source.ID WHEN MATCHED THEN Action on destination /* E.g., UPDATE SET col1 = 1 / WHEN NOT MATCHED BY TARGET|SOURCE Action on destination / E.g., INSERT (col1) VALUES(1) */ OUTPUT $action INTO @Changes SELECT * FROM @Changes