Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Peter Wages EDS peter.wages@eds.com Bob Carts Senior Data Engineer, SAIC robert.carts@saic.com Warren Donovan Senior DBA, SAIC warren.donovan@saic.com
6/5/2002
Slide 2
Introduction
Certified Informix DBAs WAIUG Board of Directors Work for EDS and SAIC NT and UNIX (Solaris, IBM AIX, HP-UX) IDS 7.31, 9.21, 9.3 and XPS 8.3 Data Warehouse and OLTP Applications
6/5/2002
Slide 3
Introduction
General Comments Stored Procedure Basics Beyond the Basics Summary
6/5/2002
Slide 4
Use them for Systems Administration functions, such as changing passwords Use them for common database code that will be executed often, making development and tuning easier Use them for security to hide the code Use them to place processing on the database server versus a client machine Use them with Java, 4GL, ESQL/c or other tools
6/5/2002
Slide 5
Stored procedures for Informix, Oracle and Sybase are proprietary and cannot be migrated easily Does anyone know about DB2 procedure and Informix procedure compatibility/migratability? With queries that are frequently changed and updated
6/5/2002
Slide 6
What is a Stored Procedure? Creating a Stored Procedure using DBAccess Parts of the Stored Procedure Compile Time Errors and Warnings Running a procedure in DBAccess Returning multiple sets of values Permissions on a Stored Procedure Other SPL commands
6/5/2002
Slide 7
procedure is also parsed at first execution if a table in SQL is not available at compile time.
6/5/2002
Slide 8
Procedure Definition:
CREATE PROCEDURE warren_proc ( id_num CHAR(4)) --accept one argument RETURNING CHAR(4), INTEGER, DECIMAL(9,2); -- 3 items
Define Variables
-- you can define any # of variables -- of any data type, -- including system level variables!
SELECT dmis_id, fy, AVG(cap_rate) INTO p_dmis_id, p_fy, p_cap_rate FROM cap_rate WHERE dmis_id=id_num GROUP BY 1,2 ; RETURN p_dmis_id, p_fy, p_cap_rate;
END PROCEDURE
1998
1899.21
6/5/2002
Slide 10
CREATE PROCEDURE warren_proc ( id_num CHAR(4)) --accept one argument RETURNING CHAR(4), INTEGER, DECIMAL(9,2); -- 3 items DEFINE p_dmis_id CHAR(4); DEFINE p_fy INTEGER; DEFINE p_cap_rate DECIMAL(9,2);
FOREACH
SELECT dmis_id, fy, AVG(cap_rate) INTO p_dmis_id, p_fy, p_cap_rate FROM cap_rate WHERE dmis_id=id_num GROUP BY 1,2 ;
RETURN p_dmis_id, p_fy, p_cap_rate WITH RESUME; --or it will return only the first row END FOREACH -- without this, the procedure will fail to compile with a syntax error END PROCEDURE
6/5/2002
Slide 11
6/5/2002
Slide 12
create a DBA-privileged procedure, must have DBA To create an owner-privileged procedure, must have Resource
user must have been granted execute on the procedure. A user does not necessarily have to be granted access to all the tables in the stored procedure.
privileges depend on the type of procedure being run 2 Types: Owner-Privileged and DBA-Priviliged
6/5/2002
Slide 13
When an Owner-Privileged Stored Procedure is executed, the server checks the existence of any referenced objects, and that the user has access to all the tables If the user has all necessary privileges, no problem If the user does not have all the necessary privileges, but the OWNER of the stored procedure does, no problem If neither has the necessary privileges, the query will fail
6/5/2002
Slide 14
When a DBA-Privileged SP is executed, the user assumes DBA privileges for the duration of the procedure. When a DBA-Privileged SP calls a User-Privileged SP, the called User-Privileged procedure does NOT act like a DBA-Privileged Stored Procedure. Likewise, if a DBA-Privileged SP is called by a UserPrivileged SP, it retains its privileges for the duration of its run, returns to the calling SP, which maintains its privileges.
6/5/2002
Slide 15
LOOPS The FOREACH loop- used to select and manipulate more than one row of data The FOR Loop - uses expressions or range operators to establish a finite number of iterations for a loop The WHILE Loop - indefinite loop while a given criterion holds true. Terminates when not true (beware of nulls!)
CALL -
use a call to execute a procedure from within a procedure IF, ELSE, ELIF - used to create branches within a procedure LET- used to assign values to variables CONTINUE - starts the next iteration of the innermost loop EXIT- stops the execution of a loop
6/5/2002
Slide 16
Raising Exceptions Returning diagnostic/error information Stored Procedure Maintenance DBINFO function Calling a Stored Procedure from a Stored Procedure Create Your Own Exceptions Tracing and Debugging The Value of Current (current date-time) Commands You Cant Use Stored Procedure in a Select Statement Stored Procedures in Java Web Reporting Stored Procedures in 4GL Blocking Statements A Very Special XPS Stored Procedure Using the system command
6/5/2002
Slide 17
On Exception
ON EXCEPTION set sql_code,isam_code,sql_message return null, null, null, null,sql_code, isam_code, sql_message; END EXCEPTION If the stored procedure gets an exception , (e.g 206 table does not exist ) What will be returned is (expression) -206 (expression) -111 (expression) The specified table does not exist
6/5/2002
Slide 18
Since stored procedures are cached in memory you may be executing the old version of the procedure after you drop and recreate the procedure. This is true for older releases (i.e. 5.0) If a table structure that a stored procedure uses is altered, you are NOT notified that the procedure may fail. You will only find out when it is executed! When update statistics is run, procedures are flagged to be re-parsed. They are re-parsed when first executed after that point.
6/5/2002
Slide 20
DBINFO Function
Informix functions can be used in stored procedures Use DBINFO(sqlca.sqlerrd2) to determine if any rows were selected. You cant access SQLCODE unless there is an exception./error condition. When you insert a record with a serial field , you can get the value of the serial with the following: DBINFO("sqlca.sqlerrd1"); DBINFO(sessionid) produces the informix session
6/5/2002
Slide 21
Create procedure -----ON EXCEPTION etc... END EXCEPTION . .EXECUTE PROCEDURE sp_state_lookup ( p_state ) INTO v_state_code,sscode,sisamcode,smsg; IF sscode < 0 THEN RAISE EXCEPTION sscode,sisamcode,smsg; END IF . End procedure
6/5/2002
Slide 22
Informixs Special SQLCODE -746 > finderr -746 -746 message-string You supply message-string for this message. You can apply this message to error conditions that you specify in a stored procedure. The corrective action for this error depends on the condition that caused it. You, the user, define both the condition and the message text.
6/5/2002
Slide 23
6/5/2002
Slide 24
create procedure get_orders (pfname char(20),plname char(30),p_debug_flag char(1) default "N") : if p_debug_flag = "Y" then set "debug file to /tmp/debuglog"; set trace on; else set trace off; end if
6/5/2002
Slide 25
Trace file
Sample Trace File trace expression :21:45 START TIME iteration of cursory procedure sp_wf_cam_b_t1 select cursor iteration. select cursor returns G94440 , 52 , WASHINGTON TECH procedure sp_wf_cam_b_t1 returns G94440 , 52 , WASHINGTON TECH , 2 , NULL. , Success trace expression :21:45 END TIME
6/5/2002
Slide 26
Why is the start time the same as the end time? Why does CURRENT always return the same value in SPL? It is a requirement of ANSI SQL that a stored procedure return a constant value for CURRENT throughout its lifetime. This is the way that Informix implemented CURRENT in SPL to meet the standard
6/5/2002
Slide 27
CLOSE CLOSE DATABASE CONNECT CREATE DATABASE CREATE PROCEDURE CREATE PROCEDURE FROM DATABASE DECLARE DESCRIBE EXECUTE EXECUTE IMMEDIATE
FETCH FLUSH FREE GET DESCRIPTOR INFO LOAD/UNLOAD OPEN OUTPUT PREPARE PUT SET DESCRIPTOR WHENEVER
6/5/2002
Slide 28
Select Customer_no, Cust_code_proc(customer_no) from Customer Restricts what commands you can use in the procedure
6/5/2002
Slide 29
ALTER FRAGMENT ALTER INDEX ALTER TABLE BEGIN WORK COMMIT WORK CREATE TRIGGER DELETE DROP DATABASE DROP INDEX DROP SYNONYM
DROP TABLE DROP TRIGGER DROP VIEW INSERT RENAME COLUMN RENAME TABLE ROLLBACK WORK UPDATE
6/5/2002
Slide 30
JAVA
try { callableStatement = oConnection.prepareCall ("{call sp_get_insert_sbs()}"); rs = callableStatement.executeQuery(); if (rs.next()) { setBatchSerialId((rs.getString(1)),session_id); sqlcode = rs.getInt(2); isam_code = rs.getInt(3); if ( sqlcode < 0 ) { } } } catch (Exception e) { }
6/5/2002
Slide 31
At EDS, we had to produce a web report of statistical data using IBIs Web Focus report utility The report had to run On_DEMAND and produce REAL-TIME data The report had to get data from some major tables in the database including a table with 20 million records What we did was write a stored procedure
6/5/2002
Slide 32
Calling a Stored Procedure From Web Focus Case Statement Setting PDQPRIORITY Using Temp Tables Optimizer Directives Returning multiple rows for reporting
6/5/2002
Slide 33
EX sp_wf_cam_b_sum "&PROGYR", "®ION", "&SCH_CODE" ,"&CAM", "&STATE", "&DTE_TODAY", "&END_PREVMO", "&LST_FRIDAY", "&DTE_30PREV", "&DTE_90PREV", "&DTE_365", "&DTE_30PR_365", "&DTE_90PR_365";
6/5/2002
Slide 34
THEN "A"
6/5/2002
Slide 36
6/5/2002
Slide 37
If only one set of values are returned: PREPARE p_exec FROM "EXECUTE PROCEDURE procname(?,?,?)" EXECUTE p_exec USING value01, value02, value03
6/5/2002
Slide 38
Blocking Statements
create procedure block3 ( ) returning int; define error_num int; define c int; define cmd char(30); BEGIN on exception in ( -206,-217 ) set error_num if error_num = -206 then create table t ( c int ) ; insert into t values ( 10 ); elif error_num = -217 then alter table t add (d int ); let c = ( select d from t ); else return error_num; END IF END EXCEPTION WITH RESUME insert into t values (10); let c = ( select d from t ); END BEGIN let c = ( select e from t ); END return c; end procedure 6/5/2002
Slide 40
Scope of Control of an ON EXCEPTION Statement An ON EXCEPTION statement is valid for the statement block that follows the ON EXCEPTION statement, all the statement blocks nested within that following statement block, and all the statement blocks that follow the ON EXCEPTION statement. It is not valid in the statement block that contains the ON EXCEPTION statement. Example does not agree with statement above It is not valid in the statement block that DOES NOT contain the ON EXCEPTION statement
6/5/2002
Slide 41
exception block was being executed both for unexpected errors and bad records. A stored procedure may not be the best tool to migrate data from a legacy system.
6/5/2002
Slide 44
CREATE PROCEDURE public.sysdbopen () SET ENVIRONMENT IMPLICIT_PDQ ON; SET ENVIRONMENT BOUND_IMPL_PDQ ON; END PROCEDURE;
6/5/2002
Slide 45
The system command permits calling operating system commands and scripts from a stored Procedure There is no easy way to determine if the system call completed successfully There can be a major performance impact when using many system calls. Informix explained that this is due to reauthentication each time a system command is used
6/5/2002
Slide 46
create PROCEDURE "informix".sp_chg_pwd(newpwd CHAR(100)) RETURNING INT; DEFINE retval INT; LET retval = 0; SYSTEM newpwd; LET retval = 1; RETURN retval; END PROCEDURE;
6/5/2002
Slide 47
Informix Guide to SQL Tutorial Informix Guide to SQL Syntax Informix Stored Procedure Programming Michael L. Gonzales (Prentice-Hall Publisher ) 1996 ISBN 0-13-206723-4
6/5/2002
Slide 48
INFORMIX FAQ
www.iiug.org/ver1/resources/informix_faq.html
6/5/2002
Slide 49
Suggestions
6/5/2002
Slide 50