Stored Procedure Language Stored Procedure Overview Stored Procedure is a function in a shared library accessible to the database server can also write.

Slides:



Advertisements
Similar presentations
Owners Roles DB Objects Grant Revoke Grant Revoke Grant Revoke (*) with admin option Il ruolo …. È un set di system & object privileges Non appartiene.
Advertisements

PL/SQL.
Chapter 8 Advanced SQL Pearson Education © Chapter 8 - Objectives u How to use the SQL programming language u How to use SQL cursors u How to create.
BD05/06 PL/SQL  Introduction  Structure of a block  Variables and types  Accessing the database  Control flow  Cursors  Exceptions  Procedures.
® IBM Software Group © IBM Corporation QUY Thai Duy – ITFac DLU Lesson 12: SQL PL Stored Procedures.
AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
PL/SQL. Introduction to PL/SQL PL/SQL is the procedure extension to Oracle SQL. It is used to access an Oracle database from various environments (e.g.
SQL*PLUS, PLSQL and SQLLDR Ali Obaidi. SQL Advantages High level – Builds on relational algebra and calculus – Powerful operations – Enables automatic.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Introduction to SQL Programming Techniques.
Introduction to PL/SQL Lecture 0 – Self Study Akhtar Ali.
Copyright  Oracle Corporation, All rights reserved. 4 Creating Functions.
Dr. James Dullea, CSC8490 Introduction to PL/SQLSlide 1 of 36 7From Prof. Dullea CSC8490 Introduction to PL/SQL Module 01-9 Revised: June 12, 2005 Dr.
Advanced Databases Advanced PL/SQL Programming: Procedure, Function and Package.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
Copyright  Oracle Corporation, All rights reserved. 3 Creating Procedures.
Lecture 4 PL/SQL language. PL/SQL – procedural SQL Allows combining procedural and SQL code PL/SQL code is compiled, including SQL commands PL/SQL code.
Stored Procedures, Transactions, and Error-Handling
Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 5.1 © Copyright IBM Corporation 2008 DB2 9 Fundamentals.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Cursors These slides are licensed under.
PL/SQL A BRIEF OVERVIEW DAVID WILSON. PL/SQL User’s Guide and Reference PL/SQL User’s Guide and Reference.
Triggers and Stored Procedures in DB 1. Objectives Learn what triggers and stored procedures are Learn the benefits of using them Learn how DB2 implements.
1. 1. Which type of argument passes a value from a procedure to the calling program? A. VARCHAR2 B. BOOLEAN C. OUT D. IN 2.
PL SQL Block Structures. What is PL SQL A good way to get acquainted with PL/SQL is to look at a sample program. PL/SQL combines the data manipulating.
Advanced SQL: Cursors & Stored Procedures
F15 Philip K. Gunning, DGI Anaheim, CA Sept , 2002 Fundamentals of DB2 SQL Procedures.
Program with PL/SQL Lesson 4. Writing Control Structure.
CpSc 462/662: Database Management Systems (DBMS) (TEXNH Approach) Stored Procedure James Wang.
Stored Procedures Week 9. Test Details Stored Procedures SQL can call code written in iSeries High Level Languages –Called stored procedures SQL has.
CS178 Database Management PL/SQL session 8 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman.
ICS 321 Fall 2009 DBMS Application Programming Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 10/06/20091Lipyeow.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
ICS 321 Fall 2010 SQL in a Server Environment (i) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 11/1/20101Lipyeow.
PL/SQL Block Structure DECLARE - Optional Variables, cursors, user-defined exceptions BEGIN - Mandatory SQL Statements PL/SQL Statements EXCEPTIONS - Optional.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
School of Computing and Management Sciences © Sheffield Hallam University SQL is non-procedural –designed to be relatively approachable to non- programmers.
CMPE 226 Database Systems October 7 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak
Introduction to Explicit Cursors. 2 home back first prev next last What Will I Learn? Distinguish between an implicit and an explicit cursor Describe.
implicit and an explicit cursor
PL/SQL programming Procedures and Cursors Lecture 1 [Part 2]
1 Stored Procedures in MySQL Part I. 2 Objectives SQL Vs. MySQL SP MySQL SP Parameters MySQL SP Control Structures.
Program with PL/SQL Lesson 3. Interacting with the Oracle Server.
Chapter 8 Advanced SQL Pearson Education © Chapter 8 - Objectives How to use the SQL programming language How to use SQL cursors How to create stored.
Advanced SQL: Cursors & Stored Procedures Instructor: Mohamed Eltabakh 1.
Last Updated : 27 th April 2004 Center of Excellence Data Warehousing Group Teradata RDBMS Concepts.
Kingdom of Saudi Arabia Ministry of Higher Education Al-Imam Muhammad Ibn Saud Islamic University College of Computer and Information Sciences Overview.
SQL Triggers, Functions & Stored Procedures Programming Operations.
1 Introduction to Database Systems, CS420 SQL Persistent Stored Modules (PSM) – Stored Procedure.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
1. Advanced SQL Functions Procedural Constructs Triggers.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Application Development Chapter 6.
A Guide to SQL, Seventh Edition
Program with PL/SQL Lesson 4.
PL/pgSQL
PL/SQL.
Views, Stored Procedures, Functions, and Triggers
Chapter 5: Advanced SQL Database System concepts,6th Ed.
SQL PL/SQL Presented by: Dr. Samir Tartir
PL/SQL Scripting in Oracle:
CPSC-310 Database Systems
CS122B: Projects in Databases and Web Applications Spring 2017
CS122B: Projects in Databases and Web Applications Winter 2017
CS122B: Projects in Databases and Web Applications Winter 2018
Information Management
CS122B: Projects in Databases and Web Applications Spring 2018
Chapter 8 Advanced SQL Pearson Education © 2009.
Modification to Views Via Triggers
Stored Procedure Language
CS122B: Projects in Databases and Web Applications Winter 2019
Presentation transcript:

Stored Procedure Language Stored Procedure Overview Stored Procedure is a function in a shared library accessible to the database server can also write stored procedures using languages such as C or Java Advantages of stored procedure : Reduced network traffic The more SQL statements that are grouped together for execution, the larger the savings in network traffic

Normal Database

Applications using stored procedures

Writing Stored Procedures Allows local variables, loops, procedures, examinationof one tuple ar a time. Rough Form DECLARE Declarations BEGIN Executable statements  The DECLARE portion is optional  Alternate terminating character in DB2 CLP scripts: needed for ending and running.

Simplest Form : Sequence of Modifications Employee( name, ssn, salary) BEGIN INSERT INTO EMPLOYEE VALUES (‘Sharad’, 123, 234); DELETE FROM EMPLOYEE WHERE ssn = 234; To process the DB2 CLP script from the command line: db2 -tdterm-char -vf script-name Writing Stored Procedures  Tasks performed by the client application  Tasks performed by the stored procedure, when invoked  The CALL statement  Explicit parameter to be defined :  IN: Passes a value to the stored procedure from the client application  OUT: Stores a value that is passed to the client application when the stored procedure terminates.  INOUT : Passes a value to the stored procedure from the client application, and returns a value to the Client application when the stored procedure terminates

CREATE OR REPLACE PROCEDURE ( ) AS BEGIN EXAMPLE: CREATE PROCEDURE UPDATE_SALARY_1 (1) (IN EMPLOYEE_NUMBER CHAR(6), (2) IN RATE INTEGER) (2) LANGUAGE SQL (3) BEGIN UPDATE EMPLOYEE (4) SET SALARY = SALARY * (1.0 * RATE / ) WHERE SSN = EMPLOYEE_NUMBER; END LANGUAGE value of SQL and the BEGIN...END block, which forms the procedure body, are particular to an SQL procedure 1) The stored procedure name is UPDATE_SALARY_1. 2)The two parameters have data types of CHAR(6) and INTEGER. Both are input parameters. 3)LANGUAGE SQL indicates that this is an SQL procedure, so a procedure body follows the other parameters. 4)The procedure body consists of a single SQL UPDATE statement, which updates rows in the employee table.

Some Valid SQL Procedure Body Statements  CASE statement  FOR statement  GOTO statement  IF statement  ITERATE statement  RETURN statement  WHILE statement

Invoking Procedures Can invoke Stored procedure stored at the location of the database by using the SQL CALL statement Nested SQL Procedures: To call a target SQL procedure from within a caller SQL procedure, simply include a CALL statement with the appropriate number and types of parameters in your caller. CREATE PROCEDURE NEST_SALES(OUT budget DECIMAL(11,2)) LANGUAGE SQL BEGIN DECLARE total INTEGER DEFAULT 0; SET total = 6; CALL SALES_TARGET(total); SET budget = total * 10000; END

CONDITIONAL STATEMENTS: IF THEN ELSE END IF; Loops LOOP …… EXIT WHEN …… END LOOP;

EXAMPLE : CREATE PROCEDURE UPDATE_SALARY_IF (IN employee_number CHAR(6), IN rating SMALLINT) LANGUAGE SQL BEGIN SET counter = 10; WHILE (counter > 0) DO IF (rating = 1) THEN UPDATE employee SET salary = salary * 1.10, bonus = 1000 WHERE empno = employee_number; ELSEIF (rating = 2) THEN UPDATE employee SET salary = salary * 1.05, bonus = 500 WHERE empno = employee_number; ELSE UPDATE employee SET salary = salary * 1.03, bonus = 0 WHERE empno = employee_number; END IF; SET counter = counter – 1; END WHILE;

EXAMPLE : The procedure receives a department number as an input parameter. A WHILE statement in the procedure body fetches the salary and bonus for each employee in the department. An IF statement within the WHILE statement updates salaries for each employee depending on number of years of service and current salary. When all employee records in the department have been processed, the FETCH statement that retrieves employee records receives SQLSTATE A not_found condition handler makes the search condition for the WHILE statement false, so execution of the WHILE statement ends. CREATE PROCEDURE BUMP_SALARY_IF (IN deptnumber SMALLINT) LANGUAGE SQL BEGIN DECLARE v_salary DOUBLE; DECLARE v_years SMALLINT; DECLARE v_id SMALLINT; DECLARE at_end INT DEFAULT 0; DECLARE not_found CONDITION FOR SQLSTATE '02000'; -- CAST salary as DOUBLE because SQL procedures do not support DECIMAL DECLARE C1 CURSOR FOR SELECT id, CAST(salary AS DOUBLE), years FROM staff; DECLARE CONTINUE HANDLER FOR not_found SET at_end = 1;

OPEN C1; FETCH C1 INTO v_id, v_salary, v_years; WHILE at_end = 0 DO IF (v_salary < 2000 * v_years) THEN UPDATE staff SET salary = 2150 * v_years WHERE id = v_id; ELSEIF (v_salary < 5000 * v_years) THEN IF (v_salary < 3000 * v_years) THEN UPDATE staff SET salary = 3000 * v_years WHERE id = v_id; ELSE UPDATE staff SET salary = v_salary * 1.10 WHERE id = v_id; END IF; ELSE UPDATE staff SET job = 'PREZ' WHERE id = v_id; END IF; FETCH C1 INTO v_id, v_salary, v_years; END WHILE; CLOSE C1; END