Module 9: Using Advanced Techniques. Considerations for Querying Data Working with Data Types Cursors and Set-Based Queries Dynamic SQL Maintaining Query.

Slides:



Advertisements
Similar presentations
Module 3: Creating and Tuning Indexes. Planning Indexes Creating Indexes Optimizing Indexes.
Advertisements

Module 8 Importing and Exporting Data. Module Overview Transferring Data To/From SQL Server Importing & Exporting Table Data Inserting Data in Bulk.
Module 12: Auditing SQL Server Environments
Module 17 Tracing Access to SQL Server 2008 R2. Module Overview Capturing Activity using SQL Server Profiler Improving Performance with the Database Engine.
Module 7 Designing Queries for Optimal Performance.
Murali Mani Persistent Stored Modules (Stored Procedures) : PSM.
Fundamentals, Design, and Implementation, 9/e Chapter 11 Managing Databases with SQL Server 2000.
Module 9 Designing an XML Strategy. Module 9: Designing an XML Strategy Designing XML Storage Designing a Data Conversion Strategy Designing an XML Query.
Copying, Managing, and Transforming Data With DTS.
Phonegap Bridge – File System CIS 136 Building Mobile Apps 1.
SEMESTER 1, 2013/2014 DB2 APPLICATION DEVELOPMENT OVERVIEW.
Overview SQL Server 2008 Overview Presented by Tarek Ghazali IT Technical Specialist Microsoft SQL Server MVP, MCTS Microsoft Web Development MCP ITIL.
Module 2 Working with Data Types. Module Overview Using Data Types Working with Character Data Converting Data Types Working with Specialized Data Types.
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
Implementing File and Print Services
IMS 4212: Application Architecture and Intro to Stored Procedures 1 Dr. Lawrence West, Management Dept., University of Central Florida
Module 8 Improving Performance through Nonclustered Indexes.
UNIT TESTING FOR SQL Prepared for SUGSA CodeLabs Alain King Paul Johnson.
Module 7: Fundamentals of Administering Windows Server 2008.
DBSQL 14-1 Copyright © Genetic Computer School 2009 Chapter 14 Microsoft SQL Server.
Dinamic SQL & Cursor. Why Dinamic SQL ? Sometimes there is a need to dynamically create a SQL statement on the fly and then run that command. This can.
Module 7 Reading SQL Server® 2008 R2 Execution Plans.
Module 8: Implementing Stored Procedures. Introducing Stored Procedures Creating, Modifying, Dropping, and Executing Stored Procedures Using Parameters.
Module 1: Introduction to Transact-SQL
Module 9 Designing and Implementing Stored Procedures.
T-SQL Transact-SQL is microsoft implementation of SQL. It contains additional programming constracts T-SQL enables you to write programs that contain SQL.
WHAT’S NEW IN SQL SERVER 2008: T-SQL Martin Bell SQL Server MVP.
Module 11: Programming Across Multiple Servers. Overview Introducing Distributed Queries Setting Up a Linked Server Environment Working with Linked Servers.
Module 3 Designing a Physical Database Model. Module Overview Selecting Data Types Designing Database Tables Designing Data Integrity.
Advanced SQL: Cursors & Stored Procedures
Module 18 Querying XML Data in SQL Server® 2008 R2.
Improving Database Performance Derrick Rapley
Module 4 Designing and Implementing Views. Module Overview Introduction to Views Creating and Managing Views Performance Considerations for Views.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 8 Advanced SQL.
10 | Programming with Transact-SQL Graeme Malcolm | Senior Content Developer, Microsoft Geoff Allix | Principal Technologist, Content Master.
Data Types Lesson 4. Skills Matrix Table A table stores your data. Tables are relational in that they are organized as rows and columns (a matrix). Each.
Creating a dynamic search form with database paging Tony Rogerson SQL Server MVP Torver Computer Consultants.
06 | Modifying Data in SQL Server Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program Manager.
Module 4 Planning for Group Policy. Module Overview Planning Group Policy Application Planning Group Policy Processing Planning the Management of Group.
Visual Programing SQL Overview Section 1.
G. Green 1.  Options include:  Script Files  already covered  APIs  last course topic  Database-Stored Code  our focus 2.
Session 1 Module 1: Introduction to Data Integrity
Learningcomputer.com SQL Server 2008 –Views, Functions and Stored Procedures.
Module 3: Using XML. Overview Retrieving XML by Using FOR XML Shredding XML by Using OPENXML Introducing XQuery Using the xml Data Type.
Stored Procedures / Session 4/ 1 of 41 Session 4 Module 7: Introducing stored procedures Module 8: More about stored procedures.
SQL server Section 2&3. What are Data Types Character Data Types Number Data Types Date and Time Data Types CAST and CONVERT functions TRY_PARSE and TRY_CONVERT.
IMS 4212: Application Architecture and Intro to Stored Procedures 1 Dr. Lawrence West, Management Dept., University of Central Florida
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
Module 10 Merging Data and Passing Tables. Module Overview Using the MERGE Statement Implementing Table Types Using Table Types As Parameters.
Course 2778A Module 1: Getting Started with Databases and Transact-SQL in SQL Server 2008 Presentation: 60 minutes Lab: 60 minutes Module 1: Getting Started.
Module 2: Querying and Filtering Data. Using the SELECT Statement Filtering Data Working with NULL Values Formatting Result Sets Performance Considerations.
Module 8: Using Programming Objects for Data Retrieval.
Module 6: Modifying Data in Tables. Inserting Data into Tables Deleting Data from Tables Updating Data in Tables Overview of Transactions.
Dynamic SQL Writing Efficient Queries on the Fly ED POLLACK AUTOTASK CORPORATION DATABASE OPTIMIZATION ENGINEER.
Module 9: Implementing Functions. Overview Creating and Using Functions Working with Functions Controlling Execution Context.
T-SQL Scripts Advanced Database Dr. AlaaEddin Almabhouh.
Views / Session 3/ 1 of 40 Session 3 Module 5: Implementing Views Module 6: Managing Views.
Module 5: Working with Subqueries. Writing Basic Subqueries Writing Correlated Subqueries Comparing Subqueries with Joins and Temporary Tables Using Common.
Querying with Transact-SQL
Creating Database Objects
Dynamic SQL Writing Efficient Queries on the Fly
Using DML to Modify Data
Dynamic SQL: Writing Efficient Queries on the Fly
Dynamic SQL Writing Efficient Queries on the Fly
Dynamic SQL: Writing Efficient Queries on the Fly
Unit I-2.
Chapter 11 Managing Databases with SQL Server 2000
Using DML to Modify Data
Creating Database Objects
Presentation transcript:

Module 9: Using Advanced Techniques

Considerations for Querying Data Working with Data Types Cursors and Set-Based Queries Dynamic SQL Maintaining Query Files

Lesson 1: Considerations for Querying Data Execution Plans Data Type Conversions 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

Demonstration: Using Execution Plans In this demonstration, you will learn how to: View estimated and actual execution plans

Data Type Conversions Data Type Conversion scenarios Data is moved to, compared, or combined with other data Data is moved from a result column, return code, or output parameter into a program variable Implicit Conversion Transparent to the user Explicit Conversion Uses CAST or CONVERT

Implicit Conversions char(10) = 'Kevin' SELECT FirstName, LastName FROM Person.Person = FirstName char(10) = 'Kevin' SELECT FirstName, LastName FROM Person.Person = FirstName

Explicit Conversions with CAST and CONVERT Using CAST USE AdventureWorks2008; GO SELECT SUBSTRING(Name, 1, 30) AS ProductName, ListPrice FROM Production.Product WHERE CAST(ListPrice AS int) LIKE '3%'; GO Using CONVERT USE AdventureWorks2008; GO SELECT SUBSTRING(Name, 1, 30) AS ProductName, ListPrice FROM Production.Product WHERE CONVERT(int, ListPrice) LIKE '3%'; GO

Data Type Precedence Without explicit conversion, this statement fails int Number ' = 1 With explicit conversion, this statement succeeds int Number ' = 1 +

Demonstration: Understanding Data Type Conversion In this demonstration, you will learn how to: Understand explicit and implicit data type conversions Use CAST and CONVERT to explicitly convert data types

Lesson 2: Working with Data Types Recommendations for Querying Date/Time Data Recommendations for Inserting Date/Time Data Implementing the hierarchyid Data Type Working with Hierarchies

Date/Time values can be queried using numeric operators such as =, >, and < as well as date/time functions. When querying date/time data, care must be taken in understanding the data type. Recommendations for Querying Date/Time Data 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.

Recommendations for Inserting Date/Time Data SET DATEFORMAT mdy GO datetime = '12/31/2008' SET DATEFORMAT mdy GO datetime = '12/31/2008' SET DATEFORMAT ydm GO datetime = ‘2008/31/12' SET DATEFORMAT ydm GO datetime = ‘2008/31/12' SET DATEFORMAT ymd GO datetime = ‘2008/12/31' SET DATEFORMAT ymd GO datetime = ‘2008/12/31' Use correct format and language settings Use language independent formats for portability

Demonstration: Working with Date/Time Data In this demonstration, you will learn how to: Use the correct methods for working with various date/time data types

Implementing the hierarchyid Data Type CREATE TABLE Organization ( EmployeeID hierarchyid, OrgLevel as EmployeeID.GetLevel(), EmployeeName nvarchar(50) NOT NULL ) ; GO CREATE TABLE Organization ( EmployeeID hierarchyid, OrgLevel as EmployeeID.GetLevel(), EmployeeName nvarchar(50) NOT NULL ) ; GO

Working with Hierarchies CREATE CLUSTERED INDEX Org_Breadth_First ON Organization(OrgLevel, EmployeeID); GO CREATE CLUSTERED INDEX Org_Breadth_First ON Organization(OrgLevel, EmployeeID); GO CREATE UNIQUE INDEX Org_Depth_First ON Organization(EmployeeID); GO CREATE UNIQUE INDEX Org_Depth_First ON Organization(EmployeeID); GO

Demonstration: Using the hierachyid Data Type In this demonstration you will learn how to: Work with the hierarchyid data type

Lesson 3: Cursors and Set-Based Queries Understanding Cursors Cursor Implementations Using Cursors Understanding Set-Based Logic

Cursors extend processing of result sets Understanding Cursors Allow positioning at specific rows Retrieve one or more rows from the current position Support data modification Support different levels of visibility Provide T-SQL statements access to data

Cursor Implementations ImplementationFeatures Transact-SQL Based on DECLARE CURSOR Implemented on server API Server Based on API functions in OLE DB and ODBC Implemented on server Client Implemented by Native Client ODBC and ADO Caches all result set rows on the client

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 DECLARE vend_cursor CURSOR FOR SELECT * FROM Purchasing.Vendor OPEN vend_cursor FETCH NEXT FROM vend_cursor CLOSE vend_cursor DEALLOCATE vend_cursor

Demonstration: Working with Cursors In this demonstration, you will learn how to: Use cursors to work with result sets

Understanding Set-Based Logic 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 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-based logic SQL Server iterates through data Deals with results as a set instead of row-by-row

Demonstration: Using Set-Based Queries In this demonstration, you will learn how to: Use a set-based query to replace a cursor

Lesson 4: Dynamic SQL Introducing Dynamic SQL Using Dynamic SQL Considerations for Using Dynamic SQL

Introducing Dynamic SQL Dynamic SQL Allows query to be built using variables Places query into variable = = MAX(SalesOrderNumber) FROM Sales.SalesOrderHeader WHERE CustomerID = = MAX(SalesOrderNumber) FROM Sales.SalesOrderHeader WHERE CustomerID

Using Dynamic SQL Using sp_executesql sp_executesql = ] stmt [ {, data_type [ OUT | OUTPUT ][,...n]' } {, = ] 'value1' [,...n ] } ] sp_executesql = ] stmt [ {, data_type [ OUT | OUTPUT ][,...n]' } {, = ] 'value1' [,...n ] } ] Using EXECUTE [ { EXEC | EXECUTE } ] { = ] { module_name [ ;number ] } [ = ] { value [ OUTPUT ] | [ DEFAULT ] } ] [,...n ] [ WITH RECOMPILE ] } [;] [ { EXEC | EXECUTE } ] { = ] { module_name [ ;number ] } [ = ] { value [ OUTPUT ] | [ DEFAULT ] } ] [,...n ] [ WITH RECOMPILE ] } [;]

Considerations for Using Dynamic SQL Security Considerations: SQL Injection Security is checked for every object Performance Considerations: Query plan not reused frequently Use sp_executesql for best chance at plan reuse Performance related to complexity of query

Demonstration: Using Dynamic SQL In this demonstration, you will learn how to: Build and execute a query that uses dynamic SQL

Lesson 5: Maintaining Query Files What Are Versioning and Source Control? Features of Team Foundation Server 2008

What Are Versioning and Source Control? Versioning Provides a record of changes made Enables retrieval of previous versions 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

Features of Team Foundation Server 2008 Enabled support for Reporting Services on any server and any port Support for SQL Server 2008 New role for many operations activities

Lab: Using Advanced Querying Techniques Using Execution Plans Converting Data Types Implementing a Hierarchy Using Cursors and Set-Based Queries Logon information Virtual machineNY-SQL-01 User nameAdministrator Password Pa$$w0rd Estimated time: 60 minutes

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 the result set?

Module Review and Takeaways Review Questions Common Issues and Troubleshooting Tips Best Practices

Course Evaluation