Published on

Published in: Technology, Business
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. ICS 184: Introduction to Data Management Lecture Note 13: Stored Procedures and Embedded SQL
  2. 2. <ul><li>A function stored in the DB server </li></ul><ul><li>Can be invoked multiple times </li></ul><ul><li>Advantage: reduce network traffic </li></ul><ul><li>Can also be written using other languages </li></ul><ul><ul><li>e.g., C, Java </li></ul></ul><ul><li>Different vendors could have different implementations </li></ul><ul><ul><li>Check the manual and sample programs </li></ul></ul>Stored Procedures
  3. 3. Architecture Client Application DB Client Normal Database Network DB Client Client Application DB Client Stored Procedures Applications using stored procedures Network DB Client DB Server DB DB Server DB
  4. 4. <ul><li>Allows local variables, loops, procedures, examination of one tuple at a time. </li></ul><ul><li>CREATE PROCEDURE <name> (<arglist>) </li></ul><ul><li>BEGIN </li></ul><ul><ul><ul><li><procedure statements> </li></ul></ul></ul><ul><ul><ul><li>END </li></ul></ul></ul><ul><ul><ul><li>@ </li></ul></ul></ul><ul><li><Procedure Statements> can have multiple SQL statements </li></ul><ul><li>'@' needed for ending and running. </li></ul><ul><li>Each argument can be: </li></ul><ul><ul><li>IN </li></ul></ul><ul><ul><li>OUT </li></ul></ul><ul><ul><li>INOUT </li></ul></ul>Declare Stored Procedures (DB2 syntax)
  5. 5. <ul><li>CREATE TABLE emp(id INTEGER, salary FLOAT); </li></ul><ul><li>CREATE PROCEDURE </li></ul><ul><li>UPDATE_EMP (IN idNum INTEGER, IN rate FLOAT) </li></ul><ul><li>LANGUAGE SQL </li></ul><ul><li>BEGIN </li></ul><ul><li>UPDATE EMP </li></ul><ul><li>SET salary = salary* (1.0 + rate) </li></ul><ul><li>WHERE id = idNum; </li></ul><ul><li>END </li></ul><ul><li>@ </li></ul><ul><li>Name: UPDATE_EMP. </li></ul><ul><li>The two parameters are of data type INTEGER and FLOAT. Both are input parameters. </li></ul><ul><li>LANGUAGE SQL indicates that this is an SQL procedure, so a procedure body follows the other parameters. </li></ul><ul><li>The procedure body consists of a single SQL UPDATE statement, which updates rows in the emp table. </li></ul><ul><li>'@' is used as the terminating character for stored procedures in DB2. </li></ul>Example
  6. 6. <ul><li>CALL UPDATE_EMP (2, 0.1); </li></ul>Call Stored Procedures CALL UPDATE_EMP (3, 0.2);
  7. 7. <ul><li>Call another procedure in a procedure (like calling functions) </li></ul><ul><li>To call a target SQL procedure within a caller SQL procedure, simply include a CALL statement with the appropriate number and types of parameters. </li></ul><ul><li>CREATE PROCEDURE NEST_SALES(OUT budget DECIMAL(11,2)) </li></ul><ul><li>LANGUAGE SQL </li></ul><ul><li>BEGIN </li></ul><ul><li>DECLARE total INTEGER DEFAULT 0; </li></ul><ul><li>SET total = 6; </li></ul><ul><li>CALL SALES_TARGET(total);  call another procedure </li></ul><ul><li>SET budget = total * 10000; </li></ul><ul><li>END </li></ul><ul><li>@ </li></ul><ul><li>“ budget” is an output variable </li></ul>Nested Procedures
  8. 8. <ul><li>CASE statement </li></ul><ul><li>FOR statement </li></ul><ul><li>GOTO statement </li></ul><ul><li>IF statement </li></ul><ul><li>ITERATE statement </li></ul><ul><li>RETURN statement </li></ul><ul><li>WHILE statement </li></ul><ul><li>Similar to other programming languages. </li></ul>Valid Body Statements
  9. 9. <ul><li>Conditional Statements </li></ul><ul><ul><li>IF <condition> THEN </li></ul></ul><ul><ul><li><statement(s)> </li></ul></ul><ul><ul><li>ELSE </li></ul></ul><ul><ul><li><statement(s)> </li></ul></ul><ul><ul><li>END IF; </li></ul></ul><ul><li>Loops </li></ul><ul><ul><li>LOOP </li></ul></ul><ul><ul><li>…… </li></ul></ul><ul><ul><li>EXIT WHEN <condition> </li></ul></ul><ul><ul><li>…… </li></ul></ul><ul><ul><li>END LOOP; </li></ul></ul>Example Statements
  10. 10. <ul><li> CREATE PROCEDURE UPDATE_SALARY_IF </li></ul><ul><li>(IN employee_number INT, IN rating SMALLINT) </li></ul><ul><li>LANGUAGE SQL </li></ul><ul><li>BEGIN </li></ul><ul><li> IF (rating = 1) THEN </li></ul><ul><ul><ul><li>BEGIN </li></ul></ul></ul><ul><ul><ul><li> DECLARE count INT; </li></ul></ul></ul><ul><li> SET counter = 10; </li></ul><ul><li> WHILE (counter > 0) DO </li></ul><ul><li>UPDATE employee </li></ul><ul><li>SET salary = salary * 1.10, bonus = 1000 </li></ul><ul><li>WHERE empno = COUNT; </li></ul><ul><li> SET counter = counter – 1; </li></ul><ul><li>END WHILE; </li></ul><ul><li> END </li></ul><ul><li>ELSEIF (rating = 2) </li></ul><ul><li>THEN UPDATE employee </li></ul><ul><li> SET salary = salary * 1.05, bonus = 500 </li></ul><ul><li> WHERE empno = employee_number; </li></ul><ul><li>ELSE UPDATE employee </li></ul><ul><li>SET salary = salary * 1.03, bonus = 0 </li></ul><ul><li>WHERE empno = employee_number; </li></ul><ul><li>END IF; </li></ul><ul><li>END </li></ul><ul><li>@ </li></ul>Example 1
  11. 11. <ul><li>CREATE PROCEDURE BUMP_SALARY_IF (IN deptnumber SMALLINT) </li></ul><ul><li>LANGUAGE SQL </li></ul><ul><li>BEGIN </li></ul><ul><li>DECLARE v_salary DOUBLE; </li></ul><ul><li>DECLARE v_years SMALLINT; </li></ul><ul><li>DECLARE v_id SMALLINT; </li></ul><ul><li>DECLARE at_end INT DEFAULT 0; </li></ul><ul><li>DECLARE not_found CONDITION FOR SQLSTATE '02000';  “not found” reached </li></ul><ul><li>-- CAST salary as DOUBLE because SQL procedures do not support DECIMAL </li></ul><ul><li>DECLARE C1 CURSOR FOR </li></ul><ul><li>SELECT id, CAST(salary AS DOUBLE), years </li></ul><ul><li>FROM staff; </li></ul><ul><li>DECLARE CONTINUE HANDLER FOR not_found SET at_end = 1; </li></ul>Example 2
  12. 12. <ul><li>OPEN C1; </li></ul><ul><li>FETCH C1 INTO v_id, v_salary, v_years; </li></ul><ul><li>WHILE at_end = 0 DO </li></ul><ul><li>IF (v_salary < 2000 * v_years) </li></ul><ul><li>THEN UPDATE staff </li></ul><ul><li>SET salary = 2150 * v_years </li></ul><ul><li>WHERE id = v_id; </li></ul><ul><li>ELSEIF (v_salary < 5000 * v_years) </li></ul><ul><li>THEN IF (v_salary < 3000 * v_years) </li></ul><ul><li>THEN UPDATE staff </li></ul><ul><li>SET salary = 3000 * v_years </li></ul><ul><li>WHERE id = v_id; </li></ul><ul><li>ELSE UPDATE staff </li></ul><ul><li>SET salary = v_salary * 1.10 </li></ul><ul><li>WHERE id = v_id; </li></ul><ul><li>END IF; </li></ul><ul><li>ELSE UPDATE staff </li></ul><ul><li>SET job = 'PREZ' </li></ul><ul><li>WHERE id = v_id; </li></ul><ul><li>END IF; </li></ul><ul><li>FETCH C1 INTO v_id, v_salary, v_years; </li></ul><ul><li>END WHILE; </li></ul><ul><li>CLOSE C1; </li></ul><ul><li>END </li></ul>
  13. 13. Example 2: description <ul><li>This procedure receives a department number as an input parameter. </li></ul><ul><li>A WHILE statement in the procedure body fetches the salary and bonus for each employee in the department. </li></ul><ul><li>An IF statement within the WHILE statement updates salaries for each employee, depending on number of years of service and current salary. </li></ul><ul><li>When all employee records in the department have been processed, the FETCH statement that retrieves employee records receives SQLSTATE 20000. </li></ul><ul><li>A not_found condition handler makes the search condition for the WHILE statement false, so execution of the WHILE statement ends </li></ul>
  14. 14. Writing your stored procedures <ul><li>Different vendors have implementations of stored procedures </li></ul><ul><li>As always: </li></ul><ul><ul><li>Start from a sample program. Do not start from scratch! </li></ul></ul><ul><ul><li>“ Copy and paste” </li></ul></ul>
  15. 15. Embedded SQL <ul><li>Direct SQL is rarely used </li></ul><ul><li>Usually, SQL is embedded in some application code. </li></ul><ul><li>Need some method to reference SQL statements </li></ul>Host language + Embedded SQL Preprocessor Host Language + function calls Host language compiler Host language program <ul><li>The best way to do programming: </li></ul><ul><ul><li>Start with a template </li></ul></ul><ul><ul><li>Modify the template to write your own program! </li></ul></ul>
  16. 16. The Impedance Mismatch Problem <ul><li>The host language manipulates variables, values, pointers. </li></ul><ul><li>SQL manipulates relations. </li></ul><ul><li>No construct in host language for manipulating relations. </li></ul><ul><li>Why not use only one language? </li></ul><ul><ul><li>Forgetting SQL  definitely not a good idea! </li></ul></ul><ul><ul><li>SQL cannot do everything that the host language can do. </li></ul></ul><ul><li>Interface between SQL and host language: </li></ul><ul><ul><li>Values get passed through shared variables. </li></ul></ul><ul><ul><li>Colons precede shared variables when occurring within SQL statements. </li></ul></ul><ul><ul><li>EXEC SQL: precedes every SQL statement in host language. </li></ul></ul>
  17. 17. Example Void simpleInsert() { EXEC SQL BEGIN DECLARE SECTION; char productName[20], company[30]; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; /* get values for productName and company somehow */ EXEC SQL INSERT INTO Product(name, company) VALUES (:productName, :company); } Variable SQLSTATE provides error messages and status reports (e.g., 00000 means operation completed without problem).
  18. 18. Single-Row Select Statements Void getPrice() { EXEC SQL BEGIN DECLARE SECTION; char productName[20], company[30]; integer price; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; /* read value of product name */ EXEC SQL SELECT price INTO :price FROM Product WHERE Product.name = :productName; /* print out value of price */ }
  19. 19. Cursors Motivation: go through the results of a query EXEC SQL DECLARE cursorName CURSOR FOR SELECT …. FROM …. WHERE …. ; EXEC SQL OPEN cursorName ; while (true) { EXEC SQL FETCH FROM cursorName INTO :variables; if ( NO_MORE_TUPLES ) break; /* do something with values */ } EXEC SQL CLOSE cursorName ;
  20. 20. More on Cursors <ul><li>Cursors can modify a relation as well as read it. </li></ul><ul><li>Order can be determined by ORDER BY keyword in SQL query. </li></ul><ul><li>Can be protected against changes to underlying relations. </li></ul><ul><li>Can scroll: go forward, backward, +n, -n, Abs(n), Abs(-n). </li></ul>