This document provides a cheat sheet of Transact-SQL concepts including CASE expressions, CURSOR declarations, and CREATE FUNCTION syntax. It includes examples of simple and searched CASE expressions, declaring and using a cursor to iterate through rows, and creating a user-defined function that returns an integer.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
27 views
Cheatsheet
This document provides a cheat sheet of Transact-SQL concepts including CASE expressions, CURSOR declarations, and CREATE FUNCTION syntax. It includes examples of simple and searched CASE expressions, declaring and using a cursor to iterate through rows, and creating a user-defined function that returns an integer.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1
THE ABSENTMINDED DBA'S CHEAT SHEET CTES
Intermediate TSQL, by MidnightDBA.com WITH DirectReports ( ManagerID, DeptID )
AS ( SELECT e.ManagerID , edh.DepartmentID SIMPLE CASE FROM HumanResources.Employee AS e CASE input_expression INNER JOIN WHEN when_expression THEN HumanResources.EmployeeDepartmentHist result_expression ory AS edh -- ... ON e.EmployeeID = edh.EmployeeID ELSE else_result_expression AND edh.EndDate IS NULL END WHERE ManagerID IS NULL ) SELECT ManagerID , DeptID SEARCHED CASE FROM DirectReports INNER JOIN HumanResources.Department AS dp CASE ON DirectReports.DeptID = dp.DepartmentID WHEN Boolean_expression THEN WHERE dp.GroupName = N'Research and result_expression Development' -- ... ELSE else_result_expression END CURSOR DECLARE @myID INT, @myName NVARCHAR(50) DECLARE myCursor CURSOR FOR DROP IF EXISTS SELECT myID, [NAME] FROM myTable USE DBName; OPEN myCursor GO IF OBJECT_ID (N'dbo.ISOweek', N'FN') IS NOT NULL FETCH NEXT FROM myCursor INTO @myID, DROP FUNCTION dbo.ISOweek; @myName GO WHILE @@FETCH_STATUS = 0 BEGIN -- do stuff with @myID, @myName UPDATE...FROM FETCH NEXT FROM myCursor INTO @myID, @myName UPDATE Table1 END SET Table1.col1 = Table1.col1 + T2.col1 CLOSE myCursor FROM Table1 AS T2 DEALLOCATE myCursor
CREATE FUNCTION CREATE FUNCTION dbo.fName (@DATE datetime) RETURNS int AS BEGIN RETURN 100 END;