T-SQL Scripts Advanced Database Dr. AlaaEddin Almabhouh
Slide 2 (of 33) Topic & Structure of Lesson Declaring variables Setting variables Common System Functions Dynamic SQL The use of If/Else statement The use of CASE...WHEN...THEN The use of While statement
Batch Directives GO Delineates batches of Transact-SQL statements to tools and utilities Is not an actual Transact-SQL statement EXEC Executes a user-defined function, system procedure, user- defined stored procedure, or an extended stored procedure Controls the execution of a character string within a Transact- SQL batch
Comments In-line Comments Block Comments SELECT productname, (unitsinstock - unitsonorder) -- Calculates inventory, supplierID FROM products GO SELECT productname, (unitsinstock - unitsonorder) -- Calculates inventory, supplierID FROM products GO /* 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 /* 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 Example 2 Example 1
Identifiers Standard Identifiers First character must be alphabetic Other characters can include letters, numerals, or symbols Identifiers starting with symbols have special uses Delimited Identifiers Use when names contain embedded spaces Use when reserved words are portions of names Enclose in brackets ([ ]) or quotation marks ("")
Naming Guidelines for Identifiers Keep Names Short Use Meaningful Names Where Possible Use Clear and Simple Naming Conventions Use an Identifier That Distinguishes Types of Object Views Stored procedures Keep Object Names and User Names Unique Sales table and sales role
Variables User-defined with Statement Assigned Values with SET or Statement Variables Have Local Scope USE northwind char(20) = 'Dodsworth' = employeeid FROM employees WHERE LastName AS EmployeeID GO USE northwind char(20) = 'Dodsworth' = employeeid FROM employees WHERE LastName AS EmployeeID GO
Setting Variables Setting variables Using SELECT money = MAX(Salary) FROM Salesperson Setting variables Using SELECT money = MAX(Salary) FROM Salesperson Setting variables Using SET money = (SELECT MAX(Salary) FROM Salesperson) GO Setting variables Using SET money = (SELECT MAX(Salary) FROM Salesperson) GO
Common System Functions ROWCOUNT int SELECT * FROM Products = PRINT 'The number of rows' + ' ' + ROWCOUNT int SELECT * FROM Products = PRINT 'The number of rows' + ' ' + SERVERNAME Use [ABC Company] varchar(20) = SERVERNAME Use [ABC Company] varchar(20) =
Dynamic SQL varchar(20) = ‘Customers’ EXEC('SELECT * FROM ' GO varchar(20) = ‘Customers’ EXEC('SELECT * FROM ' GO
The use of If/Else statement As As DateTime = '1996/10/04' = '2007/04/16' PRINT 'You have the experience required for a new promotion' ELSE PRINT ‘You do not have the experience’ GO As As DateTime = '1996/10/04' = '2007/04/16' PRINT 'You have the experience required for a new promotion' ELSE PRINT ‘You do not have the experience’ GO As As DateTime = '1996/10/04' = '2007/04/11' PRINT 'You have the experience required for a new promotion in this job' GO As As DateTime = '1996/10/04' = '2007/04/11' PRINT 'You have the experience required for a new promotion in this job' GO
If/Else: Examples if exists (select MAX(value) from table1 where id = 2) BEGIN update table2 set code = (select MAX(value) from table1 where id = 2) END if exists (select MAX(value) from table1 where id = 2) BEGIN update table2 set code = (select MAX(value) from table1 where id = 2) END int = = = na_ans from dbo.qrc_maintally where school_id begin = 'Yes' end int = = = na_ans from dbo.qrc_maintally where school_id begin = 'Yes' end
The use of CASE: Syntax CASE input_expression WHEN when_expression THEN result_expression [...n ] ELSE else_result_expression END Searched CASE function: CASE WHEN Boolean_expression THEN result_expression [...n ] ELSE else_result_expression END CASE input_expression WHEN when_expression THEN result_expression [...n ] ELSE else_result_expression END Searched CASE function: CASE WHEN Boolean_expression THEN result_expression [...n ] ELSE else_result_expression END
The use of CASE: Example 1 Varchar(20); = 'F'; = WHEN 'm' THEN 'Male' WHEN 'M' THEN 'Male' WHEN 'f' THEN 'Female' WHEN 'F' THEN 'Female' END SELECT 'Student Gender: ' GO Varchar(20); = 'F'; = WHEN 'm' THEN 'Male' WHEN 'M' THEN 'Male' WHEN 'f' THEN 'Female' WHEN 'F' THEN 'Female' END SELECT 'Student Gender: ' GO
The use of CASE: Example 2 SELECT ProductNumber, Category = CASE ProductLine WHEN 'R' THEN 'Road' WHEN 'M' THEN 'Mountain' WHEN 'T' THEN 'Touring' WHEN 'S' THEN 'Other sale items' ELSE 'Not for sale' END, Name FROM Production.Product ORDER BY ProductNumber; SELECT ProductNumber, Category = CASE ProductLine WHEN 'R' THEN 'Road' WHEN 'M' THEN 'Mountain' WHEN 'T' THEN 'Touring' WHEN 'S' THEN 'Other sale items' ELSE 'Not for sale' END, Name FROM Production.Product ORDER BY ProductNumber;
The use of While: Syntax WHILE Boolean_expression { sql_statement | statement_block } [ BREAK ] { sql_statement | statement_block } [ CONTINUE ] { sql_statement | statement_block } WHILE Boolean_expression { sql_statement | statement_block } [ BREAK ] { sql_statement | statement_block } [ CONTINUE ] { sql_statement | statement_block }
The use of While: Example 1 As int = 1 < 5 AS Number GO As int = 1 < 5 AS Number GO As int = 1 < 5 BEGIN AS Number + 1 END GO As int = 1 < 5 BEGIN AS Number + 1 END GO
The use of While: Example 2 WHILE (SELECT AVG(ListPrice) FROM Production.Product) < $300 BEGIN UPDATE Production.Product SET ListPrice = ListPrice * 2 SELECT MAX(ListPrice) FROM Production.Product IF (SELECT MAX(ListPrice) FROM Production.Product) > $500 BREAK ELSE CONTINUE END PRINT 'Too much for the market to bear'; WHILE (SELECT AVG(ListPrice) FROM Production.Product) < $300 BEGIN UPDATE Production.Product SET ListPrice = ListPrice * 2 SELECT MAX(ListPrice) FROM Production.Product IF (SELECT MAX(ListPrice) FROM Production.Product) > $500 BREAK ELSE CONTINUE END PRINT 'Too much for the market to bear';
Slide 81 (of 82) Q & A