Presentation is loading. Please wait.

Presentation is loading. Please wait.

Stored Procedure, UDF and Triggers Pertemuan 9 Matakuliah: T0413 Tahun: 2009.

Similar presentations


Presentation on theme: "Stored Procedure, UDF and Triggers Pertemuan 9 Matakuliah: T0413 Tahun: 2009."— Presentation transcript:

1

2 Stored Procedure, UDF and Triggers Pertemuan 9 Matakuliah: T0413 Tahun: 2009

3 Bina Nusantara University 3 Stored Procedures Client Application Server SQL #1 SQL #2 SQL #3 Network SQL #1 SQL #2 SQL #3 myproc CALL myproc

4 Bina Nusantara University 4 Stored Procedures Database objects that usually contain one or more SQL statements as well as procedural (business) logic Executed and managed by DB2 (server-side objects) Can be written using SQL PL, C/C++, Java, Cobol, CLR supported languages, and OLE Benefits for using stored procedures include: –Centralized business logic that promotes code re-use –Improved security –Improved performance In this workshop, we focus on SQL PL procedures because of their popularity, good performance and simplicity

5 Bina Nusantara University 5 IBM Data Studio Replaces the DB2 Developer Workbench of DB2 9 Installed separately from DB2 9.5 Based on Eclipse  Using the Command Editor: connect to sample create procedure p1 begin end  Using the IBM Data Studio Creating your first SQL PL Stored Procedure

6 Bina Nusantara University 6 CREATE PROCEDURE proc_name [( {optional parameters} )] [optional procedure attributes] Basic stored procedure structure LANGUAGE SQL  RESULT SETS (required if returning result sets)  SPECIFIC my_unique_name ƒcan be same as procedure name ƒhighly recommended for manageability: – GRANT EXECUTE ON SPECIFIC PROCEDURE... – DROP SPECIFIC PROCEDURE... Optional stored procedure attributes is a single statement, or a set of statements grouped by BEGIN [ATOMIC]... END

7 Bina Nusantara University 7 CREATE PROCEDURE proc(IN p1 INT, OUT p2 INT, INOUT p3 INT)...  IN - Input parameter  OUT - Output parameter  INOUT - Input and Output parameter  All params must be provided in CALL statement Parameters  -- This is an SQL-style comment  /* This is a C-style coment */ (valid within SQL procedures ) Comments in SQL PL Stored Procedures

8 Bina Nusantara University 8 BEGIN [ATOMIC] END Declarations Logic - Can contain other compound stmts Compound Statement Optionally atomic Compound statements

9 Bina Nusantara University 9 DECLARE var_name [ DEFAULT value]; Note: Default value is NULL  Examples: DECLARE temp1 SMALLINT DEFAULT 0; DECLARE temp2 INTEGER DEFAULT 10; DECLARE temp3 DECIMAL(10,2) DEFAULT ; DECLARE temp4 REAL DEFAULT 10.1; DECLARE temp5 DOUBLE DEFAULT ; DECLARE temp6 BIGINT DEFAULT 10000; DECLARE temp7 CHAR(10) DEFAULT 'yes'; DECLARE temp8 VARCHAR(10) DEFAULT 'hello'; DECLARE temp9 DATE DEFAULT ' '; DECLARE temp10 TIME DEFAULT '1:50 PM'; DECLARE temp11 TIMESTAMP DEFAULT ' '; DECLARE temp12 CLOB(2G); DECLARE temp13 BLOB(2G); Variable declaration Assignment statements  SET total = 100; ƒSame as VALUES(100) INTO total;  SET total = NULL; ƒany variable can be set to NULL  SET total = (select sum(c1) from T1); ƒCondition is raised if more than one row  SET first_val = (select c1 from T1 fetch first 1 row only) ƒfetch only the first row from a table  SET sch = CURRENT SCHEMA;

10 Bina Nusantara University 10 CREATE PROCEDURE P2 ( IN v_p1 INT, INOUT v_p2 INT, OUT v_p3 INT) LANGUAGE SQL SPECIFIC myP2 BEGIN -- my second SQL procedure SET v_p2 = v_p2 + v_p1; SET v_p3 = v_p1; END Example: Stored procedure with parameters To call the procedure from the Command Editor: call P2 (3, 4, ?)

11 Bina Nusantara University 11  Cursor declaration and usage: DECLARE CURSOR [WITH RETURN ] FOR ; OPEN ; FETCH INTO CLOSE ;  The value can be to “CLIENT or CALLER” ƒCLIENT: result set will be returned to the client application ƒCALLER: result set will be returned to the client or stored procedure that made the call Cursors

12 Bina Nusantara University 12 CREATE PROCEDURE set () DYNAMIC RESULT SETS 1 LANGUAGE SQL BEGIN DECLARE cur CURSOR WITH RETURN TO CLIENT FOR SELECT name, dept, job FROM staff WHERE salary > 20000; OPEN cur; END Example: Stored procedure returning result sets

13 Bina Nusantara University 13 CREATE PROCEDURE sum_salaries(OUT sum INTEGER) LANGUAGE SQL BEGIN DECLARE p_sum INTEGER; DECLARE p_sal INTEGER; DECLARE c CURSOR FOR SELECT SALARY FROM EMPLOYEE; DECLARE SQLSTATE CHAR(5) DEFAULT '00000'; SET p_sum = 0; OPEN c; FETCH FROM c INTO p_sal; WHILE(SQLSTATE = '00000') DO SET p_sum = p_sum + p_sal; FETCH FROM c INTO p_sal; END WHILE; CLOSE c; SET sum = p_sum; END Example: Processing a cursor

14 Bina Nusantara University 14  Access requires explicit declaration:  DECLARE SQLSTATE CHAR(5);  DECLARE SQLCODE INT;  Can be declared ONLY at outermost scope and automatically set by DB2 after each operation SQLCODE and SQLSTATE SQLSTATE  Success: SQLSTATE '00000'  Not found: SQLSTATE '02000'  Warning: SQLSTATE '01XXX'  Exception: Everything else SQLCODE = 0, successful. > 0, successful with warning < 0, unsuccessful = 100, no data was found. i.e. FETCH statement returned no data

15 Bina Nusantara University 15  A condition represents a given SQLSTATE  It can be raised by any SQL statement  You can provide names to a condition to make your code more readable  Built-in names for general conditions:  SQLWARNING, SQLEXCEPTION, NOT FOUND  These map to the SQLSTATEs mentioned in the previous slide  Specific conditions:  SQLSTATE '01004‘  To assign a user-defined name to a condition: DECLARE truncation CONDITION FOR SQLSTATE '01004' Conditions

16 Bina Nusantara University 16  A condition handler must specify handled conditions where to resume execution (CONTINUE, EXIT or UNDO) action to perform to handle the condition  Action can be any statement (including control structures)  Upon SQLEXCEPTION condition,if no handler exists, the procedure terminates and returns to the client with error CONTINUE point BEGIN [ATOMIC] DECLARE HANDLER FOR statement_1; statement_2; statement_3; END raises exception UNDO or EXIT point Condition Handling

17 Bina Nusantara University 17  CASE (selects an execution path (simple / searched))  IF  FOR (executes body for each row of table)  WHILE  ITERATE (forces next iteration. Similar to CONTINUE in C)  LEAVE (leaves a block or loop. "Structured Goto")  LOOP (infinite loop)  REPEAT  GOTO  RETURN  CALL (procedure call) Flow control statements

18 Bina Nusantara University 18  Useful when the final form of SQL is known only at RUN TIME  Example: if col1 and tabname are variables: 'SELECT ' || col1 || ' FROM ' || tabname;  Also recommended for DDL to avoid dependency problems and package invalidation, and to implement recursion.  Keywords: ƒEXECUTE IMMEDATE - ideal for single execution SQL ƒPREPARE + EXECUTE - ideal for multiple execution SQL Dynamic SQL

19 Bina Nusantara University 19 CREATE PROCEDURE dyn1 (IN value1 INT, IN value2 INT) SPECIFIC dyn1 BEGIN DECLARE stmt varchar(255); DECLARE st STATEMENT; SET stmt = 'insert into T2 values (?, ?)'; PREPARE st FROM stmt; EXECUTE st USING value1, value1; EXECUTE st USING value2, value2; SET stmt = 'insert into T2 values (9,9)'; EXECUTE IMMEDIATE stmt; END Compile stmt once Execute it many times Compile and execute once prerequisite: create table T2 (c1 int, c2 int) Example: Dynamic SQL

20 Bina Nusantara University 20 SQLCHAR *stmt = (SQLCHAR *) "CALL MEDIAN_RESULT_SET( ? )" ; SQLDOUBLE sal = ; /* Bound to parameter marker in stmt */ SQLINTEGER salind = 0; /* Indicator variable for sal */ sqlrc = SQLPrepare(hstmt, stmt, SQL_NTS); sqlrc = SQLBindParameter(hstmt, 1, SQL_PARAM_OUTPUT, SQL_C_DOUBLE, SQL_DOUBLE, 0, 0, &sal, 0, &salind); SQLExecute(hstmt); if (salind == SQL_NULL_DATA) printf("Median Salary = NULL\n"); else printf("Median Salary = %.2f\n\n", sal ); sqlrc = StmtResultPrint(hstmt); /* Get first result set */ sqlrc = SQLMoreResults(hstmt); /* Check for another result set */ if (sqlrc == SQL_SUCCESS) { /* There is another result set */ sqlrc = StmtResultPrint(hstmt); } See sqllib/samples/sqlproc/rsultset.c Calling from CLI

21 Bina Nusantara University 21 Calling Stored Procedures from a VB.NET Application Try ‘ Create a DB2Command to run the stored procedure Dim procName As String = “TRUNC_DEMO” Dim cmd As DB2Command = conn.CreateCommand() Dim parm As DB2Parameter cmd.CommandType = CommandType.StoredProcedure cmd.CommandText = procName ‘ Register the output parameters for the DB2Command parm = cmd.Parameters.Add(“v_lastname”, DB2Type.VarChar) parm.Direction = ParameterDirection.Output parm = cmd.Parameters.Add(“v_msg”, DB2Type.VarChar) parm.Direction = ParameterDirection.Output ‘ Call the stored procedure Dim reader As DB2DataReader = cmd.ExecuteReader Catch myException As DB2Exception DB2ExceptionHandler(myException) Catch UnhandledExceptionHandler() End Try

22 Bina Nusantara University 22 Calling Stored Procedures from a Java Application try { // Connect to sample database String url = “jdbc:db2:sample”; con = DriverManager.getConnection(url); CallableStatement cs = con.prepareCall(“CALL trunc_demo(?, ?)”); // register the output parameters callStmt.registerOutParameter(1, Types.VARCHAR); callStmt.registerOutParameter(2, Types.VARCHAR); cs.execute(); con.close(); } catch (Exception e) { /* exception handling logic goes here */ }

23 Bina Nusantara University 23  SQL PL = SQL Procedural Language  Inline SQL PL This is a SQL PL subset supported within a dynamic SQL statement, triggers and UDFs  Dynamic compound SQL It means using a BEGIN ATOMIC – END block. The “ATOMIC” part, makes the entire compound SQL be treated as a single UOW (unit of work) Inline SQL PL User Defined Function

24 Bina Nusantara University 24 create sequence create table T1 (id bigint, data char(100), insert_ts begin atomic declare cnt INT default 0; while (cnt < 20000) do insert into t1 values ( nextval for MYSEQ, (select case when (rand() < 0.5) then null else space(int(rand()*100)) end case from sysibm.sysdummy1), current timestamp); set cnt=cnt+1; end while; Example: Randomly populating a table Pre-req: Inline SQL PL:

25 Bina Nusantara University 25 User-Defined Functions Functions always return a value Some built-in functions already exist out-of-the-box –Eg: SUM(), AVG(), DIGITS(), etc. Can create UDFs in: –SQL PL, C/C++, Java, CLR (Common Language Runtime), and OLE (Object Linking and Embedding) –In this workshop, we focus on SQL PL functions because of their simplicity and popularity

26 Bina Nusantara University 26 Types of functions Scalar functions –Return a single value –Cannot change database state (i.e. no INSERT, UPDATE, DELETE statements allowed) – Example: COALESCE( ), SUBSTR( ) Table functions –Return values in a table format –Called in the FROM clause of a query –Can change database state (i.e. allow INSERT, UPDATE, DELETE statements) – Example: SNAPSHOT_DYN_SQL( ), MQREADALL( ) Others type of functions (not covered in this course): –Row functions –Column functions

27 Bina Nusantara University 27 CREATE FUNCTION deptname(p_empid VARCHAR(6)) RETURNS VARCHAR(30) SPECIFIC deptname BEGIN ATOMIC DECLARE v_department_name VARCHAR(30); DECLARE v_err VARCHAR(70); SET v_department_name = ( SELECT d.deptname FROM department d, employee e WHERE e.workdept=d.deptno AND e.empno= p_empid); SET v_err = 'Error: employee ' || p_empid || ' was not found'; IF v_department_name IS NULL THEN SIGNAL SQLSTATE '80000' SET MESSAGE_TEXT=v_err; END IF; RETURN v_department_name; END Scalar function  Scalar functions take input values and return a single value  They cannot be used to modify table data Example:

28 Bina Nusantara University 28 Invoking Scalar UDFs Scalar UDFs can be invoked in SQL statements wherever a scalar value is expected, or in a VALUES clause –SELECT DEPTNAME(‘000010’) FROM SYSIBM.SYSDUMMY1 –VALUES DEPTNAME(‘000010’)

29 Bina Nusantara University 29 Table UDFs Return a table of rows Used in the FROM clause of a query Similar to a view, except more powerful because data modification statements (INSERT/UPDATE/DELETE) can be performed Typically used to return a table and keep an audit record

30 Bina Nusantara University 30 CREATE FUNCTION getEnumEmployee(p_dept VARCHAR(3)) RETURNS TABLE (empno CHAR(6), lastname VARCHAR(15), firstnme VARCHAR(12)) SPECIFIC getEnumEmployee RETURN SELECT e.empno, e.lastname, e.firstnme FROM employee e WHERE e.workdept=p_dept  A function which enumerates a set of employees of a department Table function example

31 Bina Nusantara University 31  Used in the FROM clause of an SQL statement  The TABLE() function must be applied and must be aliased. SELECT * FROM TABLE (getEnumEmployee('E01')) T alias TABLE() function Calling a Table function

32 Bina Nusantara University 32 Triggers A trigger is a database object defined on a table and fired when an INSERT, UPDATE, or DELETE operation is performed. Activate (“fire”) automatically Operations that cause triggers to fire are called triggering SQL statements

33 Bina Nusantara University 33 Types of Triggers BEFORE –activation before row is inserted, updated or deleted –Operations performed by this trigger cannot activate other triggers (i.e. INSERT, UPDATE, and DELETE operations are not permitted) AFTER –Activated after the triggering SQL statement has executed to successful completion –May activate other triggers (cascading permitted up to 16 levels) INSTEAD OF –Defined on views –Logic defined in the trigger is executed instead of the triggering SQL statement

34 Bina Nusantara University 34 A Simple BEFORE Trigger CREATE TRIGGER default_class_end NO CASCADE BEFORE INSERT ON cl_sched REFERENCING NEW AS n FOR EACH ROW MODE DB2SQL WHEN (n.ending IS NULL) SET n.ending = n.starting + 1 HOUR if no value provided on insert, column is NULL optional WHEN define qualifier for new values

35 Bina Nusantara University 35 A Simple INSTEAD OF Trigger CREATE TRIGGER update_view1 INSTEAD OF UPDATE ON view1 REFERENCING OLD AS o NEW AS n FOR EACH ROW MODE DB2SQL BEGIN ATOMIC UPDATE countries SET region = n.region WHERE region = o.region; END It is activated when performing changes to a view Prereq: CREATE TABLE countries (id int, country varchar(50), region varchar (50), average_temp int) CREATE VIEW view1 (id, continent, temperature) as SELECT id, region, average_temp from countries


Download ppt "Stored Procedure, UDF and Triggers Pertemuan 9 Matakuliah: T0413 Tahun: 2009."

Similar presentations


Ads by Google