CursoSQL 07 Tecnicas Avanzadas TSQL
CursoSQL 07 Tecnicas Avanzadas TSQL
9-1
Module 9
Using Advanced Techniques
Contents:
Lesson 1: Considerations for Querying Data Lesson 2: Working with Data Types Lesson 3: Cursors and Set-Based Queries Lesson 4: Dynamic SQL Lesson 5: Maintaining Query Files Lab: Using Advanced Techniques 9-3 9-13 9-23 9-33 9-39 9-42
9-2
Module Overview
This module provides you with the skills and knowledge about advanced querying techniques in Microsoft SQL Server 2008. This course provides information about the best practices you should follow when querying complex data that involves date/time data, and how to work with the hierarchyid data type. You will learn how to use cursors and set-based queries, as well as dynamic SQL. In addition, you will examine how to use Microsoft Team Foundation Server for managing query source files.
9-3
Lesson 1
Because there are often many ways to query for the same data, you need to understand how SQL Server processes queries using execution plans to help identify the most efficient query methods for a given situation. Also, when querying data, you need to understand how data type conversion works, as well as. the order of precedence of data types.
9-4
Execution Plans
Key Points
Execution plans graphically display the data retrieval methods chosen by the SQL Server query optimizer. Execution plans represent the execution cost of specific statements and queries in SQL Server using icons. To use the graphical execution plan feature in Management Studio, and to use the Showplan Transact-SQL SET statement options, users must have sufficient permissions to execute the Transact-SQL statements and queries. Users must also be granted the SHOWPLAN permission for all databases containing referenced objects. The graphical execution plan output in SQL Server Management Studio is read from right to left and from top to bottom. Each query in the batch that is analyzed is displayed, including the cost of each query as a percentage of the total cost of the batch.
9-5
Note: For more information about the icons used to display execution plans in Management Studio, see the topic Graphical Execution Plan Icons (SQL Server Management Studio) in SQL Server Books Online.
Question: Why would you want to view the Estimated Execution Plan?
9-6
Question: What do you need to do in order to be able to view the Actual Execution Plan?
9-7
Key Points
Data types can be converted in the following scenarios: When data from one object is moved to, compared with, or combined with data from another object, the data may have to be converted from the data type of one object to the data type of the other. When data from a Transact-SQL result column, return code, or output parameter is moved into a program variable, the data must be converted from the SQL Server system data type to the data type of the variable.
Implicit conversions are not visible to the user. SQL Server automatically converts the data from one data type to another. For example, when a smallint is compared to an int, the smallint is implicitly converted to int before the comparison proceeds.
9-8
Explicit conversions use the CAST or CONVERT functions. The CAST and CONVERT functions convert a value (a local variable, a column, or another expression) from one data type to another. For example, the following CAST function converts the numeric value of $157.27 into a character string of '157.27':
CAST ( $157.27 AS VARCHAR(10) )
Note: Not all data type conversions are allowed. For a chart of allowed implicit and explicit conversions, see the topic CAST and CONVERT (Transact-SQL) in SQL Server Books Online.
Question: What are some examples of operations that can result in data conversion?
9-9
Implicit Conversions
Key Points
Implicit conversions occur whenever two different data types are operated upon together without the use of CAST or CONVERT. In the code sample above, the variable @firstname is a char data type. The FirstName field in the Person.Person table is an nvarchar data type. In this case, char has a lower precedence than nvarchar, so the @firstname variable is converted to nvarchar for comparison, as the execution plan shows. Data type precedence is covered shortly. Question: When would you want to avoid implicit conversions?
9-10
Key Points
CAST and CONVERT explicitly convert one data type to another, with slightly different syntaxes. CONVERT is SQL Server specific and includes additional styles for converting date/time data. Use CAST instead of CONVERT if you want Transact-SQL program code to comply with ISO. Use CONVERT instead of CAST to take advantage of the style functionality in CONVERT.
9-11
Key Points
When an operator combines two expressions of different data types, the rules for data type precedence specify that the data type with the lower precedence is converted to the data type with the higher precedence. If the conversion is not a supported implicit conversion, an error is returned. When both operand expressions have the same data type, the result of the operation has that data type. In the code sample above, the first statement fails because int has higher precedence than varchar. The string 'Page Number' cannot be converted to int, so an explicit conversion of the @pageno variable to varchar is required for the statement to succeed and produce the correct output.
Note: For a complete list of data type precedence, see the topic Data Type Precedence (Transact-SQL) in SQL Server Books Online.
Question: Can you think of other scenarios where data type precedence might cause problems for implicit conversions?
9-12
Question: How can implicit data type conversions cause unexpected results on queries that run successfully?
9-13
Lesson 2
With the introduction of new date/time data types in SQL Server 2008, you need to know how to work with these data types in order to make sure your time/date data is correct in the database and in reports. Another new data type, hierachyid, can help organize hierarchical data in your databases.
9-14
Key Points
In SQL Server, there are six built-in data types to store date and time data: time, date, smalldatetime, datetime, datetime2, and datetimeoffset. The smalldatetime, datetime and datetime2 data types store both date and time data. The datetimeoffset data type also stores both date and time data, but is timezone aware for specifying the offset from a time or datetime value. The time and date data types store only time and only date data, respectively. If you specify only the date part when inserting data to smalldatetime, datetime, or datetime2 column, then SQL Server stores a zero value in the time part. If you specify only the time part, then SQL Server stores 1900-01-01 in the date part. Because the date and time values are stored together in those data types, some unexpected problems might occur.
9-15
The following table summarizes the key differences between the different date and time data types:
Userdefined Storage fractional Time size second zone (bytes) precision offset Yes No
Format
Range
Accuracy
hh:mm:ss[.nnnnnnn] 00:00:00.0000000 100 3 to 5 through nanoseconds 23:59:59.9999999 YYYY-MM-DD 0001-01-01 through 999912-31 1900-01-01 through 207906-06 1753-01-01 through 999912-31 1 day 3
date
No
No
smalldatetime
YYYY-MM-DD hh:mm:ss
1 minute
No
No
datetime
YYYY-MM-DD hh:mm:ss[.nnn]
0.00333 second
No
No
datetime2
Yes
No
datetimeoffset YYYY-MM-DD 0001-01-01 100 8 to 10 hh:mm:ss[.nnnnnnn] 00:00:00.0000000 nanoseconds [+|-]hh:mm through 999912-31 23:59:59.9999999 (in UTC)
Yes
Yes
9-16
Key Points
When inserting date/time values into tables or views, you need to ensure that the format and the language settings of the date/time data types are correct. It is recommended that you use language-independent formats, rather than languagedependent formats because they are portable across languages. It is not recommended that you use language-dependent formats even with SET statements. You can also set SQL Server to use the ISOdatetime format. In the ISO format, the datetime values are represented as yyyymmdd [hh:mm:ss] or yyyy-mmddThh:mm:ss. The ISO dateformat is always guaranteed to work irrespective of the LANGUAGE or DATEFORMAT setting.
9-17
Question: What happens when you insert data containing only a date or only a time into a column that holds date and time data?
9-18
Key Points
Hierarchical data is defined as a set of data items that are related to each other by hierarchical relationships. Hierarchical relationships are where one item of data is the parent of another item. Hierarchical data is common in databases. Examples include the following: An organizational structure A file system A set of tasks in a project A taxonomy of language terms A graph of links between Web pages
9-19
The hierarchyid data type makes it easier to store and query hierarchical data. hierarchyid is optimized for representing trees, which are the most common type of hierarchical data. The hierarchyid data type can be used to replace recursive common table expressions (CTEs) which are frequently used to generate hierarchical representations of database data. The code sample shows how to create a table that implements the hierarchyid data type on the EmployeeID column for organizing employees in the database in a hierarchy. Question: Can you think of a scenario where using hierarchical data might be useful?
9-20
Key Points
A hierarchy needs an indexing strategy. There are two indexing strategies supported: Depth-first: A depth-first index, rows in a sub-tree are stored near each other. For example, all employees that report through a manager are stored near their managers record. This is represented in the code sample on the left. Breadth-first: A breadth-first stores the rows each level of the hierarchy together. For example, the records of employees who directly report to the same manager are stored near each other. This is represented by the code sample on the right.
9-21
You can perform a number of operations on nodes within the hierarchy. You can query to get nodes above or below a point on the tree, move nodes from one parent to another, and add and remove nodes using the methods supplied with the hierarchyid data type.
Note: For a complete list and description of methods available for working with hierarchies, see the topic hierarchyid Data Type Method Reference in SQL Server Books Online.
9-22
Question: What is the difference between the GetDescendant and GetAncestor methods?
9-23
Lesson 3
When querying data, the set of rows returned from a query is called the result set. Cursors allow you to perform operations on the result set on a row-by-row basis. Set-based logic, or a set-based approach, deals with the result set as a whole or parts instead of individual rows of data.
9-24
Understanding Cursors
Key Points
Operations in a relational database act on a complete set of rows. The set of rows returned by a SELECT statement consists of all the rows that satisfy the conditions in the WHERE clause of the statement. This complete set of rows returned by the statement is known as the result set. Applications, especially interactive online applications, cannot always work effectively with the entire result set as a unit. These applications need a mechanism to work with one row or a small block of rows at a time. Cursors are an extension to result sets that provide that mechanism. Cursors extend result processing by: Allowing positioning at specific rows of the result set. Retrieving one row or block of rows from the current position in the result set. Supporting data modifications to the rows at the current position in the result set.
9-25
Supporting different levels of visibility to changes made by other users to the database data that is presented in the result set. Providing Transact-SQL statements in scripts, stored procedures, and triggers access to the data in a result set.
Question: Can you think of a scenario where a cursor might prove useful?
9-26
Cursor Implementations
Key Points
SQL Server supports three cursor implementations. Transact-SQL cursors Are based on the DECLARE CURSOR syntax and are used mainly in TransactSQL scripts, stored procedures, and triggers. Transact-SQL cursors are implemented on the server and are managed by Transact-SQL statements sent from the client to the server. They may also be contained in batches, stored procedures, or triggers. Application programming interface (API) server cursors Support the API cursor functions in OLE DB and ODBC. API server cursors are implemented on the server. Each time a client application calls an API cursor function, the SQL Server Native Client OLE DB provider or ODBC driver transmits the request to the server for action against the API server cursor.
9-27
Client cursors Are implemented internally by the SQL Server Native Client ODBC driver and by the DLL that implements the ADO API. Client cursors are implemented by caching all the result set rows on the client. Each time a client application calls an API cursor function, the SQL Server Native Client ODBC driver or the ADO DLL performs the cursor operation on the result set rows cached on the client.
Note: Because Transact-SQL cursors and API server cursors are implemented on the server, they are referred to collectively as server cursors.
9-28
Using Cursors
Key Points
Transact-SQL cursors and API cursors have different syntax, but the following general process is used with all SQL Server cursors: Associate a cursor with the result set of a Transact-SQL statement, and define characteristics of the cursor, such as whether the rows in the cursor can be updated. Execute the Transact-SQL statement to populate the cursor. Retrieve the rows in the cursor you want to see. The operation to retrieve one row or one block of rows from a cursor is called a fetch. Performing a series of fetches to retrieve rows in either a forward or backward direction is called scrolling. Optionally, perform modification operations (update or delete) on the row at the current position in the cursor. Close the cursor.
9-29
When you are completely finished with a cursor, you must also deallocate the resources using the DEALLOCATE command. Closing a cursor releases locks, but SQL Server resources will be held until a cursor is deallocated.
9-30
9-31
Key Points
While cursors allow a procedural approach to working with data by handling the data row-by-row, set-based logic, or a set-based approach to handling SQL Server data is inherent to SQL Server. Using a set-based approach to querying data, you are able to manipulate the entire result set as a whole. When using set-based logic, you allow SQL Server to determine the most efficient way to retrieve and manipulate the result set.
9-32
9-33
Lesson 4
Dynamic SQL
Although static SQL works well in many situations, there are some applications in which the data access cannot be determined in advance. To solve this problem, an application can use a form of embedded SQL called dynamic SQL. Unlike static SQL statements, which are hard-coded in the program, dynamic SQL statements can be built at run time and placed in a string host variable. They are then sent to SQL Server for processing.
9-34
Key Points
Dynamic SQL allows a T-SQL query to be built using variables and strings, then executed as a string using two available methods, which you will learn about shortly. Dynamic SQL can be useful in applications where forms with optional fields may be filled out and submitted. You can use the contents of the optional fields that have been filled out to create WHERE clauses to help return more relevant results. In the code sample above, a variable called @SQLString is populated with a SELECT statement. The @SQLString variable can then be passed to one of two commands for execution. Question: Can you think of a scenario where dynamic SQL might be useful?
9-35
Key Points
There are two methods for executing dynamic SQL statements, sp_executesql and EXECUTE. sp_executesql is a system stored procedure that takes the dynamic SQL statement as a parameter and executes it. It can take additional parameters as well. EXECUTE can also execute dynamic SQL strings, and has a different syntax than sp_executesql.
Question: Why choose one method of dynamic SQL execution over the other?
9-36
Key Points
There are some important things to consider when using dynamic SQL: SQL injection is an attack in which malicious code is inserted into strings that are later passed to an instance of SQL Server for parsing and execution. Dynamic SQL can be prone to SQL injection attacks if strings are not validated before they are executed. Security is checked for every object involved in a dynamic SQL statement. This means that you may have to give more permissions on underlying database objects than you would with a stored procedure. A user with permission to execute a stored procedure does not need permissions on the underlying database objects, but a user who executed a dynamic SQL statement against those same tables must have permissions on them. Because of the nature of dynamic SQL, it is less likely to generate execution plans that can be cached and reused than static SQL. This can have an impact on query performance using dynamic SQL.
9-37
Using sp_executesql can help SQL Server reuse execution plans when the only variation is in the parameter values supplied to the Transact-SQL statement. Because the Transact-SQL statements themselves remain constant and only the parameter values change, the SQL Server query optimizer is likely to reuse the execution plan it generates for the first execution. Because dynamic SQL statements are generally unlikely to generate reusable execution plans, their performance is related directly to the complexity of the query you build. When possible, try to reduce the complexity of any dynamic SQL queries you use.
9-38
9-39
Lesson 5
When you have many developers working on a SQL Server project, you want to have a historical record of versions of projects and files, or you want to ensure that you have a central repository for all your SQL Server projects, you may want to implement a versioning and source control system.
9-40
Key Points
When you work in teams, you need to enable the parallel development of projects by team members. However, when team members work with the same file, they need to reconcile conflicts between the different versions of the file and avoid the potential loss of valuable changes. In such situations, you require software such as Microsoft Visual SourceSafe or Team Foundation Server for source control and version management.
9-41
Key Points
Team Foundation Server (TFS) 2008 provides source control and versioning capabilities. TFS will supports a Microsoft SQL Server 2005 back end, and will support SQL Server 2008 for a back end when SQL Server 2008 and Team Foundation Server SP1 reach RTM.
9-42
9-43
9-44
Review the Results pane. SQL Server automatically converted "False" to a data type of bit to compare it to the SalariedFlag column. Look up the data types for the ListPrice and StandardCost columns in the Production.Product table. Create a new query on the AdventureWorks2008 database with the following code:
DECLARE @NewListPrice nvarchar(10) = '39.99' DECLARE @NewStandardCost float = 12.45 UPDATE Production.Product SET ListPrice = @NewListPrice, StandardCost = @NewStandardCost WHERE ProductID = '2'
9-45
Create a new query on the AdventureWorks2008 database with the following code:
SELECT Name, StandardCost, ListPrice from Production.Product WHERE ProductID = '2'
Run the query. In the results pane, notice that the ListPrice and StandardCost columns have been updated. SQL Server automatically converted the nvarchar and float types in the update query to the money data type used by the columns. Open the file E:\Mod09\Labfiles\Starter\Exercise02\UpdateDemographics.sql. Notice that the variable @NewDemographics is an nvarchar data type and contains a valid XML string. Run the query. Create a new query on the AdventureWorks2008 database with the following code:
SELECT FirstName, MiddleName, LastName, Demographics FROM Person.Person WHERE BusinessEntityID = 15
Run the query. In the Results pane, click the link in the Demographics column. SQL Server converted the nvarchar string to the xml data type automatically because the XML was valid.
9-46
Run the query. In the Results pane, there is a single column with the employee name and pay rate combined. Open the file E:\Mod09\Labfiles\Starter\Exercise02\PayRateLastChange.sql. Run the query. Review the results of the query. Notice that the last column does not show both the pay rate and date. In the query pane, modify the SELECT statement so that it begins as follows:
SELECT p.FirstName, p.LastName, (CAST(s.Rate AS nvarchar) + ' ' + s.RateChangeDate)
Run the query, and take note of the error message. Modify the SELECT statement again, so that it reads as follows:
SELECT p.FirstName, p.LastName, (CAST(s.Rate AS nvarchar) + ' ' + CAST(s.RateChangeDate as nvarchar))
Run the query. Notice that the query now runs and the last column displays the correct information.
Results: After this exercise, you should have verified that SQL Server automatically performs implicit conversions, and you should have also successfully run queries that performed explicit data type conversions.
9-47
9-48
9-49
Review the StandardCost and ListPrice columns in the Results pane. Keep this query pane open. Open the E:\Mod09\Labfiles\Starter\Exercise04\UpdateListPriceCursor.sql file. Review and execute the query. When the update is complete, run the SELECT query again. Review the ListPrice column in the Results pane. The ListPrice column has been updated.
9-50
Run the query. When the update is complete, run the SELECT query again. Review the StandardCost column in the Results pane. The StandardCost column has been updated. Close SQL Server Management Studio without saving changes. Turn off the virtual machine and discard changes.
Results: After this exercise, you should have used a cursor to update the ListPrice column and used a set-based query to update the StandardCost column of the Production.Product table.
9-51
Review Questions
1. 2. 3. How do cursors handle result sets? What functions are used to perform explicit data type conversions? What methods can be used to execute dynamic SQL queries?
9-52
Test the size and data type of input and enforce appropriate limits. This can help prevent deliberate buffer overruns. Never build Transact-SQL statements directly from user input. Use stored procedures to validate user input. Never concatenate user input that is not validated. String concatenation is the primary point of entry for script injection. Do not accept the following strings in fields from which file names can be constructed: AUX, CLOCK$, COM1 through COM8, CON, CONFIG$, LPT1 through LPT8, NUL, and PRN.