Академический Документы
Профессиональный Документы
Культура Документы
Implicit Conversions
Explicit Conversions with CAST and CONVERT Data Type Precedence
Execution Plans
Shows how the Database Engine navigates tables and indexes View Estimated Execution Plan before query execution View Actual Execution Plan after execution Generate Execution Plans with: SQL Server Management Studio T-SQL SET options SQL Server Profiler
Implicit Conversions
DECLARE @firstname char(10) SET @firstname = 'Kevin' SELECT FirstName, LastName FROM Person.Person WHERE @firstname = FirstName
Using CONVERT
USE AdventureWorks2008; GO SELECT SUBSTRING(Name, 1, 30) AS ProductName, ListPrice FROM Production.Product WHERE CONVERT(int, ListPrice) LIKE '3%'; GO
such as =, >, and < as well as date/time functions. understanding the data type.
DATETIME, DATETIME2, & DATETIMEOFFSET data types Query conditions must include both date and time portions. DATE data type Query conditions must include just the date portion.
TIME data type Query conditions must include just the time portion.
SET DATEFORMAT mdy GO DECLARE @datevar datetime SET @datevar = '12/31/2008' SELECT @datevar
SET DATEFORMAT ydm GO DECLARE @datevar datetime SET @datevar = 2008/31/12' SELECT @datevar
SET DATEFORMAT ymd GO DECLARE @datevar datetime SET @datevar = 2008/12/31' SELECT @datevar
CREATE TABLE Organization ( EmployeeID hierarchyid, OrgLevel as EmployeeID.GetLevel(), EmployeeName nvarchar(50) NOT NULL ) ; GO
Using Cursors
Understanding Set-Based Logic
Understanding Cursors
Cursors extend processing of result sets Allow positioning at specific rows Retrieve one or more rows from the current position Support data modification
Cursor Implementations
Implementation Features
Based on DECLARE CURSOR Implemented on server
Transact-SQL
API Server
ODBC
Implemented on server
Client
and ADO
Using Cursors
Process of using a cursor Associate and define characteristics Populate the cursor Retrieve rows in the cursor Modify data if needed Close and deallocate the cursor
DECLARE vend_cursor CURSOR FOR SELECT * FROM Purchasing.Vendor OPEN vend_cursor FETCH NEXT FROM vend_cursor CLOSE vend_cursor DEALLOCATE vend_cursor
SELECT ProductID, Purchasing.Vendor.VendorID, Name FROM Purchasing.ProductVendor JOIN Purchasing.Vendor ON (Purchasing.ProductVendor.VendorID = Purchasing.Vendor.VendorID) WHERE StandardPrice > $10 AND Name LIKE N'F%' GO
SET @SQLString = N'SELECT @SalesOrderOUT = MAX(SalesOrderNumber) FROM Sales.SalesOrderHeader WHERE CustomerID = @CustomerID';
Using EXECUTE
[ { EXEC | EXECUTE } ] { [ @return_status = ] { module_name [ ;number ] | @module_name_var } [ [ @parameter = ] { value | @variable [ OUTPUT ] | [ DEFAULT ] } ] [ ,...n ] [ WITH RECOMPILE ] } [;]
Performance Considerations:
Query plan not reused frequently Use sp_executesql for best chance at plan reuse
Source Control Enables development teamwork Source files can be checked in/out and merged
Other Benefits
Provides a central location for source file storage Simplifies source file backup procedures
Implementing a Hierarchy
Using Cursors and Set-Based Queries
Logon information
Lab Scenario
You are a Database Administrator at Adventure Works. As part of an effort to analyze database performance, the Senior Database Administrator has asked you to become familiar with query execution plans. In order to do this, you need to know how to view estimated and actual execution plans, as well as save execution plans and view them in XML format. You also need to familiarize yourself with implicit and explicit data type conversions.
Also, you've been asked to create a hierarchy in the database for the Sales department. To do this, you will implement a table using the hierarchyid data type.
You also need to determine whether database updates are more efficient using cursors or set-based queries. To do this, you will use a cursor to update ListPrice data and a set-based query to update StandardCost data in the Production.Product table to see how the two approaches compare.
Lab Review
What is required for a conversion to the XML data type to
succeed?
How do you create the root node of a hierarchy? When using a cursor, how do you retrieve the next row in
Best Practices
Course Evaluation