SQL Server Specialist Certificate Program
Maintaining SQL Server 2005
Week 5 – Working with TSQL and Transactions
Performance Tuning
Steve Stedman - Instructor
This Weeks Overview
● Review from Last Week + Homework● TSQL – Transact SQL● Transactions● Performance Tuning● Class Project● Review and Homework
Topics from last week
● Stored Procedures● Functions● Triggers
Homework
● Practice● Assignment● Reading
Preparation
We will be using the AdventureWorks database.
1.TSQL
● Overview● SELECT statements● JOINS (INNER AND OUTER)● Flow Control● Complex Criteria
○ Aggregate Functions, PIVOT / UNPIVOT, Full Text Search
Overview
● What is TSQL○ Transact-SQL○ Microsoft’s Proprietary extension to SQL
● Why use TSQL
SELECT statements
● Used to retrieve data from the database.
SELECT *FROM HumanResources.Employee E
INNER JOINS
● Return only rows that match from both tables
SELECT *FROM HumanResources.Employee EINNER JOIN HumanResources.EmployeeAddress as EA ON E.EmployeeId = EA.EmployeeId
OUTER JOINS
● Returns rows with matching data, as well as rows with nonmatching data
● LEFT OUTER JOIN○ All the rows from the LEFT table
● RIGHT OUTER JOIN○ All the rows from the RIGHT table
● FULL OUTER JOIN○ All the rows from both the LEFT and
RIGHT
Example: LEFT OUTER JOIN
SELECT E.EmployeeID, E.LoginID, EA.* FROM HumanResources.Employee ELEFT OUTER JOIN HumanResources.EmployeeAddress as EA ON E.EmployeeId = EA.EmployeeId
Example: RIGHT OUTER JOIN
SELECT E.EmployeeID, E.LoginID, EA.* FROM HumanResources.Employee ERIGHT OUTER JOIN HumanResources.EmployeeAddress as EA ON E.EmployeeId = EA.EmployeeId
Flow Control
● BEGIN and END● WHILE● BREAK and CONTINUE● GOTO● IF and ELSE● RETURN● WAITFOR
Example: IF and ELSE
IF DATEPART(dw, GETDATE()) = 7 OR DATEPART(dw, GETDATE()) = 1PRINT 'It is the weekend.'ELSEPRINT 'It is a weekday.'
Example: BEGIN and END
IF DATEPART(dw, GETDATE()) = 7 OR DATEPART(dw, GETDATE()) = 1BEGINPRINT 'It is the weekend.'PRINT 'Get some rest!'ENDELSEBEGINPRINT 'It is a weekday.'PRINT 'Get to work!'END
Example: WHILE, BREAK, CONTINUEDECLARE @Counter INTSET @Counter = 10WHILE @Counter > 0BEGINPRINT 'The count is ' + CONVERT(VARCHAR(10), @Counter)SET @Counter = @Counter - 1END
Grouping and Sorting
● ORDER BY - Example SELECT E.LoginID, E.Title, E.ManagerID FROM HumanResources.Employee E ORDER BY E.ManagerID
● GROUP BY - Example SELECT E.ManagerID FROM HumanResources.Employee E GROUP BY E.ManagerID
Aggregate Functions
● AVG● COUNT● MAX● MIN● SUM● STDEV ● VAR
PIVOT / UNPIVOT
● Cross-Tabulation SELECT [0], [1]FROM(SELECT E.SalariedFlag, E.VacationHoursFROM HumanResources.Employee E) AS HPIVOT(AVG(VacationHours)FOR SalariedFlag IN ([0], [1])) AS Pvt
Lab Project – TSQL
● Using the AdventureWorks database, start with the following Query
SELECT * FROM HumanResources.Employee as E1. Modify the output to group by Title, and count the
number of people with each title2. Modify the query to group by the MaritalStatus and
Gender, and count the number of people in each group.
● End of this section. Any Questions?
● 10 Minute Break
1.Transactions
● BEGIN TRANSACTION● COMMIT TRANSACTION● ROLLBACK TRANSACTION● Transaction Sample
BEGIN TRANSACTION
● Starts the transaction● Tracks data changes until COMMIT or
ROLLBACK● Ends only when you COMMIT or
ROLLBACK
Ending a Transaction
● COMMIT TRANSACTION○ Saves Changes
● ROLLBACK TRANSACTION
○ Returns the data to the original state
Transaction Sample
BEGIN TRANSACTION;UPDATE [HumanResources].[Employee] SET [Title] = @Title, [HireDate] = @HireDate WHERE [EmployeeID] = @EmployeeID;
INSERT INTO [HumanResources].[EmployeePayHistory] ([EmployeeID],[RateChangeDate],[Rate])VALUES (@EmployeeID, @RateChangeDate, @Rate);COMMIT TRANSACTION;
See: [HumanResources].[uspUpdateEmployeeHireInfo]
● End of this section. Any Questions?
1.Performance Tuning
● See Other Powerpoint presentation.
● End of this section. Any Questions?
Homework
● Homework for next week○ Work on your class projects
Class Project
● Status Update● Review of research and
experimentation required for the group project.
● Presentations will be week 9
Questions?
Top Related