Download presentation
Presentation is loading. Please wait.
Published byStephanie Flynn Modified over 8 years ago
1
T-SQL Scripts Advanced Database Dr. AlaaEddin Almabhouh
2
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
3
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
4
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
5
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 ("")
6
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
7
Variables User-defined with DECLARE @ Statement Assigned Values with SET or SELECT @ Statement Variables Have Local Scope USE northwind DECLARE @EmpID varchar(11),@vlName char(20) SET @vlname = 'Dodsworth' SELECT @EmpID = employeeid FROM employees WHERE LastName = @vlname SELECT @EmpID AS EmployeeID GO USE northwind DECLARE @EmpID varchar(11),@vlName char(20) SET @vlname = 'Dodsworth' SELECT @EmpID = employeeid FROM employees WHERE LastName = @vlname SELECT @EmpID AS EmployeeID GO
8
Setting Variables Setting variables Using SELECT DECLARE @MaxSalary money SELECT @MaxSalary = MAX(Salary) FROM Salesperson SELECT @MaxSalary Setting variables Using SELECT DECLARE @MaxSalary money SELECT @MaxSalary = MAX(Salary) FROM Salesperson SELECT @MaxSalary Setting variables Using SET DECLARE @MaxSalary money SET @MaxSalary = (SELECT MAX(Salary) FROM Salesperson) SELECT @MaxSalary GO Setting variables Using SET DECLARE @MaxSalary money SET @MaxSalary = (SELECT MAX(Salary) FROM Salesperson) SELECT @MaxSalary GO
9
Common System Functions ROWCOUNT DECLARE @CheckRow int SELECT * FROM Products SET @CheckRow = @@ROWCOUNT PRINT 'The number of rows' + ' ' + CONVERT(varchar(10), @CheckRow) ROWCOUNT DECLARE @CheckRow int SELECT * FROM Products SET @CheckRow = @@ROWCOUNT PRINT 'The number of rows' + ' ' + CONVERT(varchar(10), @CheckRow) SERVERNAME Use [ABC Company] DECLARE @CheckIdentity varchar(20) SET @CheckIdentity = @@SERVERNAME PRINT @CheckIdentity SERVERNAME Use [ABC Company] DECLARE @CheckIdentity varchar(20) SET @CheckIdentity = @@SERVERNAME PRINT @CheckIdentity
10
Dynamic SQL DECLARE @Tablename varchar(20) SELECT @Tablename = ‘Customers’ EXEC('SELECT * FROM ' + @Tablename) GO DECLARE @Tablename varchar(20) SELECT @Tablename = ‘Customers’ EXEC('SELECT * FROM ' + @Tablename) GO
11
The use of If/Else statement DECLARE @DateHired As DateTime, @CurrentDate As DateTime SET @DateHired = '1996/10/04' SET @CurrentDate = '2007/04/16' IF @DateHired > @CurrentDate PRINT 'You have the experience required for a new promotion' ELSE PRINT ‘You do not have the experience’ GO DECLARE @DateHired As DateTime, @CurrentDate As DateTime SET @DateHired = '1996/10/04' SET @CurrentDate = '2007/04/16' IF @DateHired > @CurrentDate PRINT 'You have the experience required for a new promotion' ELSE PRINT ‘You do not have the experience’ GO DECLARE @DateHired As DateTime, @CurrentDate As DateTime SET @DateHired = '1996/10/04' SET @CurrentDate = '2007/04/11' IF @DateHired < @CurrentDate PRINT 'You have the experience required for a new promotion in this job' GO DECLARE @DateHired As DateTime, @CurrentDate As DateTime SET @DateHired = '1996/10/04' SET @CurrentDate = '2007/04/11' IF @DateHired < @CurrentDate PRINT 'You have the experience required for a new promotion in this job' GO
12
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 declare @yes_ans int, @no_ans int, @na_ans int SELECT @yes_ans = yes_ans, @no_ans = no_ans, @na_ans = na_ans from dbo.qrc_maintally where school_id = @SchoolId If @yes_ans > @no_ans and @yes_ans > @na_ans begin Set @Final = 'Yes' end declare @yes_ans int, @no_ans int, @na_ans int SELECT @yes_ans = yes_ans, @no_ans = no_ans, @na_ans = na_ans from dbo.qrc_maintally where school_id = @SchoolId If @yes_ans > @no_ans and @yes_ans > @na_ans begin Set @Final = 'Yes' end
13
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
14
The use of CASE: Example 1 DECLARE @CharGender Char(1), @Gender Varchar(20); SET @CharGender = 'F'; SET @Gender = CASE @CharGender WHEN 'm' THEN 'Male' WHEN 'M' THEN 'Male' WHEN 'f' THEN 'Female' WHEN 'F' THEN 'Female' END SELECT 'Student Gender: ' + @Gender; GO DECLARE @CharGender Char(1), @Gender Varchar(20); SET @CharGender = 'F'; SET @Gender = CASE @CharGender WHEN 'm' THEN 'Male' WHEN 'M' THEN 'Male' WHEN 'f' THEN 'Female' WHEN 'F' THEN 'Female' END SELECT 'Student Gender: ' + @Gender; GO
15
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;
16
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 }
17
The use of While: Example 1 DECLARE @Number As int SET @Number = 1 WHILE @Number < 5 SELECT @Number AS Number GO DECLARE @Number As int SET @Number = 1 WHILE @Number < 5 SELECT @Number AS Number GO DECLARE @Number As int SET @Number = 1 WHILE @Number < 5 BEGIN SELECT @Number AS Number SET @Number = @Number + 1 END GO DECLARE @Number As int SET @Number = 1 WHILE @Number < 5 BEGIN SELECT @Number AS Number SET @Number = @Number + 1 END GO
18
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';
19
Slide 81 (of 82) Q & A
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.