Best Practices TransactSQL u TransactSQL Syntax Elements n

Best Practices Transact-SQL

u Transact-SQL Syntax Elements n Batch Directives n System Functions n Comments n Operators n Identifiers n Expressions n Types of Data n n Variables Control-of-Flow Language Elements n Reserved Keywords

Batch Directives n n GO l Delineates batches of Transact-SQL statements to tools and utilities l Is not an actual Transact-SQL statement EXEC l Executes a user-defined function, system procedure, user-defined stored procedure, or an extended stored procedure l Controls the execution of a character string within a Transact-SQL batch

Comments n In-line Comments Example 1 SELECT productname , (unitsinstock - unitsonorder) -- Calculates inventory , supplier. ID FROM products GO n Block Comments Example 3 /* This code retrieves all rows of the products table and displays the unit price, the unit price increased by 10 percent, and the name of the product. */ USE northwind SELECT unitprice, (unitprice * 1. 1), productname FROM products GO

u Identifiers n n Standard Identifiers l First character must be alphabetic l Other characters can include letters, numerals, or symbols l Identifiers starting with symbols have special uses Delimited Identifiers l Use when names contain embedded spaces l Use when reserved words are portions of names l Enclose in brackets ([ ]) or quotation marks (" ")

Naming Guidelines for Identifiers n Keep Names Short n Use Meaningful Names Where Possible n Use Clear and Simple Naming Conventions n Use an Identifier That Distinguishes Types of Object n l Views l Stored procedures Keep Object Names and User Names Unique l Sales table and sales role

Types of Data n Numbers n SQL Variants n Dates n Image and Text n Characters n Table n Binary n Cursor n Unique Identifiers (GUID) n User-defined

Variables n User-defined with DECLARE @ Statement n Assigned Values with SET or SELECT @ Statement n Variables Have Local Scope USE northwind DECLARE @Emp. ID varchar(11) , @vl. Name char(20) SET @vlname = 'Dodsworth' SELECT @Emp. ID = employeeid FROM employees WHERE Last. Name = @vlname SELECT @Emp. ID AS Employee. ID GO

u System Functions n Aggregate Functions USE northwind SELECT AVG (unitprice) AS Avg. Price FROM products GO n Scalar Functions USE northwind SELECT DB_NAME() AS 'database‘ GO n Rowset Functions SELECT * FROM OPENQUERY (Oracle. Svr, 'SELECT name, id FROM owner. titles')

System Function Examples Example 1 SELECT 'ANSI: ', CONVERT(varchar(30), GETDATE(), 102) AS Style UNION SELECT 'Japanese: ', CONVERT(varchar(30), GETDATE(), 111) UNION SELECT 'European: ', CONVERT(varchar(30), GETDATE(), 113) GO Result Style ANSI: 1998. 03. 19 Japanese: 1998/03/19 European: 19 Mar 1998 16: 34: 40: 616

Operators n n Types of Operators l Arithmetic l Comparison l String concatenation l Logical Operator Precedence Levels

Expressions n Combination of Symbols and Operators n Evaluation to Single Scalar Value n Result Data Type Dependent on the Elements Within the Expression USE SELECT northwind Order. ID, Product. ID , (Unit. Price * Quantity) as Extended. Amount FROM [Order Details] WHERE (Unit. Price * Quantity) > 10000 GO

Control-of-Flow Language Elements Example 2 n n Statement Level l BEGIN … END block l IF … ELSE block l WHILE constructs Row Level l CASE function DECLARE @n tinyint SET @n = 5 IF (@n BETWEEN 4 and 6) BEGIN WHILE (@n > 0) BEGIN SELECT @n AS 'Number' , CASE WHEN (@n % 2) = 1 THEN 'EVEN' ELSE 'ODD' END AS 'Type' SET @n = @n - 1 END ELSE PRINT 'NO ANALYSIS‘ GO

Reserved Keywords n n Identifier Names That Have Special Meaning l Transact-SQL keywords l ANSI SQL-92 keywords l ODBC reserved keywords Do Not Use Reserved Keywords for Identifier Names

u Retrieving Data by Using the SELECT Statement n Specifying Columns n Using the WHERE Clause to Specify Rows

Using the SELECT Statement n Select List Specifies the Columns n WHERE Clause Specifies the Condition Restricting the Query n FROM Clause Specifies the Table Partial Syntax SELECT [ALL | DISTINCT] <select_list> FROM {<table_source>} [, …n] WHERE <search_condition>

Specifying Columns USE northwind SELECT employeeid, lastname, firstname, title FROM employees GO employeeid lastname firstname title 1 2 3 4 5 Davolio Fuller Leverling Peacock Buchanan Nancy Andrew Janet Margaret Steven Sales Representative Vice President, Sales Representative Sales Manager 6 7 8 Suyama King Callahan Michael Robert Laura Sales Representative Inside Sales Coordinator 9 Dodsworth Anne Sales Representative

Using the WHERE Clause to Specify Rows USE northwind SELECT employeeid, lastname, firstname, title FROM employees WHERE employeeid = 5 GO employeeid 5 lastname Buchanan firstname Steven title Sales Manager

u Filtering Data n Using Comparison Operators n Using String Comparisons n Using Logical Operators n Retrieving a Range of Values n Using a List of Values as Search Criteria n Retrieving Unknown Values

Using Comparison Operators Example 1 USE northwind SELECT lastname, city FROM employees WHERE country = 'USA' GO lastname city Davolio Fuller Seattle Tacoma Leverling Peacock Kirkland Redmond Callahan Seattle

Using String Comparisons USE northwind SELECT companyname FROM customers WHERE companyname LIKE '%Restaurant%' GO companyname GROSELLA-Restaurante Lonesome Pine Restaurant Tortuga Restaurante

Using Logical Operators Example 1 USE northwind SELECT productid, productname, supplierid, unitprice FROM products WHERE (productname LIKE 'T%' OR productid = 46) AND (unitprice > 16. 00) GO productid 14 29 62 productname Tofu Thüringer Rostbratwurst Tarte au sucre supplierid unitprice 6 23. 25 12 123. 79 29 49. 3

Retrieving a Range of Values USE northwind SELECT productname, unitprice FROM products WHERE unitprice BETWEEN 10 AND 20 GO productname unitprice Chai Chang 18 19 Aniseed Syrup Genen Shouyu 10 15. 5 Pavlova Sir Rodney’s Scones 17. 45 10 … … Example 1

Using a List of Values as Search Criteria Example 1 USE northwind SELECT companyname, country FROM suppliers WHERE country IN ('Japan', 'Italy') GO companyname country Tokyo Traders Mayumi’s Japan Formaggi Fortini s. r. l. Pasta Buttini s. r. l. Italy

Retrieving Unknown Values USE northwind SELECT companyname, fax FROM suppliers WHERE fax IS NULL GO companyname Exotic Liquids New Orleans Cajun Delights Tokyo Traders Cooperativa de Quesos ‘Las Cabras’ … fax NULL …

u Formatting Result Sets n Sorting Data n Eliminating Duplicate Rows n Changing Column Names n Using Literals

Sorting Data Example 1 USE northwind SELECT productid, productname, categoryid, unitprice FROM products ORDER BY categoryid, unitprice DESC GO productid 38 43 2 … 63 8 61 … productname Cote de Blaye Ipoh Coffee Chang … Vegie-spread Northwoods Cranberry Sauce Sirop d'érable … categoryid 1 1 1 … 2 2 2 … unitprice 263. 5000 46. 0000 19. 0000 … 43. 9000 40. 0000 28. 5000 …

Eliminating Duplicate Rows Example 1 USE northwind SELECT DISTINCT country FROM suppliers ORDER BY country GO country Australia Brazil Canada Denmark Finland France Germany Italy Japan Netherlands Norway Singapore Spain Sweden UK USA

Changing Column Names USE northwind SELECT firstname AS First, lastname AS Last , employeeid AS 'Employee ID: ' FROM employees GO First Nancy Andrew Janet Margaret Steven Michael Robert Laura Anne Last Davolio Fuller Leverling Peacock Buchanan Suyama King Callahan Dodsworth Employee ID: 1 2 3 4 5 6 7 8 9

Using Literals USE northwind SELECT firstname, lastname , 'Identification number: ', employeeid FROM employees GO First Nancy Andrew Janet Margaret Steven Michael Robert Laura Anne Last Davolio Fuller Leverling Peacock Buchanan Suyama King Callahan Dodsworth Employee ID: Identification Number: 1 Identification Number: 2 Identification Number: 3 Identification Number: 4 Identification Number: 5 Identification Number: 6 Identification Number: 7 Identification Number: 8 Identification Number: 9

u How Queries Are Processed Uncached Queries (Ad Hoc) Parse Resolve Optimize Compile Execute Procedure Cache Execute Cached Queries First Execution Parse Resolve Subsequent Execution

Performance Considerations n Not Search Conditions May Slow Data Retrieval n LIKE Search Conditions Slow Data Retrieval n Exact Matches or Ranges May Speed Data Retrieval n ORDER BY Clause May Slow Data Retrieval

Listing the TOP n Values n Lists Only the Firstn Rows of a Result Set n Specifies the Range of Values in the ORDER BY Clause n Returns Ties if WITH TIES Is Used Example 1 USE northwind SELECT TOP 5 orderid, productid, quantity FROM [order details] ORDER BY quantity DESC GO Example 2 USE northwind SELECT TOP 5 WITH TIES orderid, productid, quantity FROM [order details] ORDER BY quantity DESC GO

u Using Aggregate Functions Aggregate function Description AVG Average of values in a numeric expression COUNT Number of values in an expression COUNT (*) Number of selected rows MAX Highest value in the expression MIN Lowest value in the expression SUM Total values in a numeric expression STDEV Statistical deviation of all values STDEVP Statistical deviation for the population VAR Statistical variance of all values VARP Statistical variance of all values for the population

Using Aggregate Functions with Null Values n Most Aggregate Functions Ignore Null Values n COUNT(*) Function Counts Rows with Null Values USE northwind SELECT COUNT (*) FROM employees GO Example 1 USE northwind SELECT COUNT(reportsto) FROM employees GO Example 2

u GROUP BY Fundamentals n Using the GROUP BY Clause with the HAVING Clause

Using the GROUP BY Clause USE northwind SELECT productid, orderid , quantity FROM orderhist GO productid USE northwind SELECT productid , SUM(quantity) AS total_quantity FROM orderhist GROUP BY productid GO orderid quantity productid total_quantity 1 1 5 1 1 10 2 35 2 1 10 3 45 2 2 25 3 1 15 productid total_quantity 3 2 30 2 35 Only rows that satisfy the WHERE clause are grouped USE northwind SELECT productid , SUM(quantity) AS total_quantity FROM orderhist WHERE productid = 2 GROUP BY productid GO

Using the GROUP BY Clause with the HAVING Clause USE northwind SELECT productid, orderid , quantity FROM orderhist GO productid orderid quantity USE northwind SELECT productid, SUM(quantity) AS total_quantity FROM orderhist GROUP BY productid HAVING SUM(quantity)>=30 GO 1 1 5 1 1 10 productid total_quantity 2 1 10 2 35 2 2 25 3 45 3 1 15 3 2 30

u Generating Aggregate Values Within Result Sets n Using the GROUP BY Clause with the ROLLUP Operator n Using the GROUP BY Clause with the CUBE Operator n Using the GROUPING Function

Using the GROUP BY Clause with the ROLLUP Operator USE northwind SELECT productid, orderid, SUM(quantity) AS total_quantity FROM orderhist GROUP BY productid, orderid WITH ROLLUP ORDER BY productid, orderid GO productid orderid total_quantity Description NULL 95 1 NULL 15 1 1 5 1 2 10 2 NULL 35 2 1 10 2 2 25 Detail value for productid 2, orderid 1 3 NULL 45 Summarizes only rows for productid 3 3 1 15 Detail value for productid 3, orderid 1 3 2 30 Detail value for productid 3, orderid 2 Grand total Summarizes only rows for productid 1 Detail value for productid 1, orderid 2 Summarizes only rows for productid 2

Using the GROUP BY Clause with the CUBE Operator USE northwind SELECT productid, orderid, SUM(quantity) AS total_quantity FROM orderhist GROUP BY productid, orderid WITH CUBE ORDER BY productid, orderid GO productid orderid total_quantity The CUBE operator produces two more summary values than the ROLLUP operator Description NULL 95 Grand total NULL 1 30 Summarizes all rows for orderid 1 NULL 2 65 Summarizes all rows for orderid 2 1 NULL 15 Summarizes only rows for productid 1 1 1 5 Detail value for productid 1, orderid 1 1 2 10 Detail value for productid 1, orderid 2 2 NULL 35 Summarizes only rows for productid 2 2 1 10 Detail value for productid 2, orderid 1 2 2 25 Detail value for productid 2, orderid 2 3 NULL 45 Summarizes only rows for productid 3 3 1 15 Detail value for productid 3, orderid 1 3 2 30 Detail value for productid 3, orderid 2

Using the GROUPING Function SELECT productid, GROUPING (productid) , orderid, GROUPING (orderid) , SUM(quantity) AS total_quantity FROM orderhist GROUP BY productid, orderid WITH CUBE ORDER BY productid, orderid GO productid 1 represents summary values in the preceding column 0 represents detail values in the preceding column NULL 1 1 1 2 2 2 3 3 3 1 1 1 0 0 0 0 0 orderid NULL 1 2 total_quantity 1 0 0 95 30 65 15 5 10 35 10 25 45 15 30

Using the COMPUTE and COMPUTE BY Clauses COMPUTE BY USE northwind SELECT productid, orderid, quantity FROM orderhist ORDER BY productid, orderid COMPUTE SUM(quantity) BY productid COMPUTE SUM(quantity) GO GO productid orderid quantity 1 1 5 1 2 10 sum 15 1 1 5 1 2 10 2 1 10 2 2 25 3 1 15 sum 35 3 2 30 3 1 15 sum 95 3 2 30 sum 45 sum 95

Using Aliases for Table Names n Example 1 (without an alias name) USE joindb SELECT buyer_name, sales. buyer_id, qty FROM buyers INNER JOIN sales ON buyers. buyer_id = sales. buyer_id GO n Example 2 (with an alias name) USE joindb SELECT buyer_name, s. buyer_id, qty FROM buyers AS b INNER JOIN sales AS s ON b. buyer_id = s. buyer_id GO
- Slides: 44