T-SQL Scripts Advanced Database Dr. AlaaEddin Almabhouh.

Slides:



Advertisements
Similar presentations
Transact-SQL. 1. Declare float = 10 select * from customers where discnt
Advertisements

PL/SQL.
7 Copyright © Oracle Corporation, All rights reserved. Producing Readable Output with i SQL*Plus.
Lecture-5 Though SQL is the natural language of the DBA, it suffers from various inherent disadvantages, when used as a conventional programming language.
Databases Tutorial 2 Further Select Statements. Objectives for Week Data types Sort retrieved data Formatting output.
Data Warehousing ISYS 650. What is a data warehouse? A data warehouse is a subject-oriented, integrated, nonvolatile, time-variant collection of data.
Structured Query Language SQL: An Introduction. SQL (Pronounced S.Q.L) The standard user and application program interface to a relational database is.
Copying, Managing, and Transforming Data With DTS.
Module 2: Using Transact-SQL Querying Tools. Overview SQL Query Analyzer Using the Object Browser Tool in SQL Query Analyzer Using Templates in SQL Query.
Simple Programming Part 1 SECTION 7 Procedures and Functions.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Module 3: Changes to Transact-SQL. Overview Accessing Object Information New Transact-SQL Syntax Changes to Objects Distributed Queries.
1 SQL/PSM Procedures Stored in the Database General-Purpose Programming.
15 Structured Query Language (SQL). 2 Objectives After completing this section, you should be able to: Understand Structured Query Language (SQL) and.
Java Programming Constructs 1 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.
Introduction to Databases Chapter 7: Data Access and Manipulation.
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.
Dexterity | CONFIDENTIAL 2009 MRO | Analytics | Insights 1 Stored Procedures.
Stored Procedures, Transactions, and Error-Handling
Module 1: Introduction to Transact-SQL
T-SQL Transact-SQL is microsoft implementation of SQL. It contains additional programming constracts T-SQL enables you to write programs that contain SQL.
15/10/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
Program with PL/SQL Lesson 5. Working with Composite Data Types.
PL/SQL vs. Transact-SQL. Transact-SQL Central to the use of Microsoft® SQL Server™. All applications that communicate with SQL Server do so by sending.
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
IMS 4212: Intro to SQL 1 Dr. Lawrence West, Management Dept., University of Central Florida Introduction to SQL—Topics Introduction to.
PL / SQL By Mohammed Baihan. What is PL/SQL? PL/SQL stands for Procedural Language extension of SQL. PL/SQL is a combination of SQL along with the procedural.
Programmatic SQL Shaista Khan CS 157B. Topic Embedded SQL statements in high-level programming languages.
Trapping Oracle Server Exceptions. 2 home back first prev next last What Will I Learn? Describe and provide an example of an error defined by the Oracle.
10 | Programming with Transact-SQL Graeme Malcolm | Senior Content Developer, Microsoft Geoff Allix | Principal Technologist, Content Master.
Recognizing PL/SQL Lexical Units. 2 home back first prev next last What Will I Learn? List and define the different types of lexical units available in.
Ms. Reham Alhaweal Practice 3 Ms. Reham Alhaweal
In this session, you will learn to: Use functions to customize the result set Summarize and group data Objectives.
SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 1.
A Guide to SQL, Eighth Edition Chapter Eight SQL Functions and Procedures.
Retrieving Data in PL/SQL. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –Recognize the SQL statements that can.
Python Let’s get started!.
Using Functions in SQL Statements. 2 home back first prev next last What Will I Learn? List the advantages of user-defined functions in SQL statements.
Best Practices Transact-SQL.  Transact-SQL Syntax Elements Batch Directives Comments Identifiers Types of Data Variables System Functions Operators Expressions.
Slide 1 Chapter 7 – Part 3 Stored Procedure, Function &Trigger.
IS6146 Databases for Management Information Systems Lecture 4: SQL IV – SQL Functions and Procedures Rob Gleasure robgleasure.com.
Stored Procedures / Session 4/ 1 of 41 Session 4 Module 7: Introducing stored procedures Module 8: More about stored procedures.
IMS 4212: Application Architecture and Intro to Stored Procedures 1 Dr. Lawrence West, Management Dept., University of Central Florida
IMS 4212: Constraints & Triggers 1 Dr. Lawrence West, Management Dept., University of Central Florida Stored Procedures in SQL Server.
Lab 2 Writing PL/SQL Blocks CISB514 Advanced Database Systems.
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 9: Using Advanced Techniques. Considerations for Querying Data Working with Data Types Cursors and Set-Based Queries Dynamic SQL Maintaining Query.
Module 2: Querying and Filtering Data. Using the SELECT Statement Filtering Data Working with NULL Values Formatting Result Sets Performance Considerations.
PL/SQL Writing Executable Statements. PL/SQL Block Syntax and Guidelines Statement can be split across lines, but keywords must not be split Lexical units.
1 Stored Procedure, Function and Trigger. 2Objectives 1. Database Programming 2. Stored Procedure 3. Function 4. Trigger.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
In this session, you will learn to: Create and manage views Implement a full-text search Implement batches Objectives.
Module 9: Implementing Functions. Overview Creating and Using Functions Working with Functions Controlling Execution Context.
Implementing Functions Advanced Database Dr. AlaaEddin Almabhouh.
1. Advanced SQL Functions Procedural Constructs Triggers.
1 c6212 Advanced Database and Client Server MS SQL Server 2000 Stored Procedures and Parameters What ? Why ? How ?
3 Copyright © 2004, Oracle. All rights reserved. Writing Executable Statements.
Transact SQL (T-SQL) Creating Stored Procedures, Functions and Triggers SoftUni Team Technical Trainers Software University
Task oriented processing
ATS Application Programming: Java Programming
10 | Programming with Transact-SQL
Programmability by Adrienne Watt.
Implementing Triggers
Stored Procedure, Function and Trigger
Chapter 3 The DATA DIVISION.
Tutorial 10: Programming with javascript
SQL Transactions 4/23/2019 See scm-intranet.
Chap 2. Identifiers, Keywords, and Types
CS4540 Special Topics in Web Development SQL and MS SQL
Presentation transcript:

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