Advance Features In Procedues And Triggers

52 %
48 %
Information about Advance Features In Procedues And Triggers
Technology

Published on December 8, 2008

Author: journalofinformix

Source: slideshare.net

Description

describes IDS v11 features for stored procedures and triggers.

Advance features in stored procedures and triggers. Keshava Murthy, Architect, Informix Software Session Number TIX-2258

Disclaimer © Copyright IBM Corporation [current year]. All rights reserved. U.S. Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. THE INFORMATION CONTAINED IN THIS PRESENTATION IS PROVIDED FOR INFORMATIONAL PURPOSES ONLY.  WHILE EFFORTS WERE MADE TO VERIFY THE COMPLETENESS AND ACCURACY OF THE INFORMATION CONTAINED IN THIS PRESENTATION, IT IS PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED. IN ADDITION, THIS INFORMATION IS BASED ON IBM’S CURRENT PRODUCT PLANS AND STRATEGY, WHICH ARE SUBJECT TO CHANGE BY IBM WITHOUT NOTICE.  IBM SHALL NOT BE RESPONSIBLE FOR ANY DAMAGES ARISING OUT OF THE USE OF, OR OTHERWISE RELATED TO, THIS PRESENTATION OR ANY OTHER DOCUMENTATION. NOTHING CONTAINED IN THIS PRESENTATION IS INTENDED TO, NOR SHALL HAVE THE EFFECT OF, CREATING ANY WARRANTIES OR REPRESENTATIONS FROM IBM (OR ITS SUPPLIERS OR LICENSORS), OR ALTERING THE TERMS AND CONDITIONS OF ANY AGREEMENT OR LICENSE GOVERNING THE USE OF IBM PRODUCTS AND/OR SOFTWARE. IBM, the IBM logo, ibm.com, and informix software are trademarks or registered trademarks of International Business Machines Corporation in the United States, other countries, or both. If these and other IBM trademarked terms are marked on their first occurrence in this information with a trademark symbol (® or ™), these symbols indicate U.S. registered or common law trademarks owned by IBM at the time this information was published. Such trademarks may also be registered or common law trademarks in other countries. A current list of IBM trademarks is available on the Web at “Copyright and trademark information” at www.ibm.com/legal/copytrade.shtml Other company, product, or service names may be trademarks or service marks of others.

© Copyright IBM Corporation [current year]. All rights reserved.

U.S. Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.

THE INFORMATION CONTAINED IN THIS PRESENTATION IS PROVIDED FOR INFORMATIONAL PURPOSES ONLY.  WHILE EFFORTS WERE MADE TO VERIFY THE COMPLETENESS AND ACCURACY OF THE INFORMATION CONTAINED IN THIS PRESENTATION, IT IS PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED. IN ADDITION, THIS INFORMATION IS BASED ON IBM’S CURRENT PRODUCT PLANS AND STRATEGY, WHICH ARE SUBJECT TO CHANGE BY IBM WITHOUT NOTICE.  IBM SHALL NOT BE RESPONSIBLE FOR ANY DAMAGES ARISING OUT OF THE USE OF, OR OTHERWISE RELATED TO, THIS PRESENTATION OR ANY OTHER DOCUMENTATION. NOTHING CONTAINED IN THIS PRESENTATION IS INTENDED TO, NOR SHALL HAVE THE EFFECT OF, CREATING ANY WARRANTIES OR REPRESENTATIONS FROM IBM (OR ITS SUPPLIERS OR LICENSORS), OR ALTERING THE TERMS AND CONDITIONS OF ANY AGREEMENT OR LICENSE GOVERNING THE USE OF IBM PRODUCTS AND/OR SOFTWARE.

IBM, the IBM logo, ibm.com, and informix software are trademarks or registered trademarks of International Business Machines Corporation in the United States, other countries, or both. If these and other IBM trademarked terms are marked on their first occurrence in this information with a trademark symbol (® or ™), these symbols indicate U.S. registered or common law trademarks owned by IBM at the time this information was published. Such trademarks may also be registered or common law trademarks in other countries. A current list of IBM trademarks is available on the Web at “Copyright and trademark information” at www.ibm.com/legal/copytrade.shtml

Other company, product, or service names may be trademarks or service marks of others.

Trigger Enhancements employee_tab Insert trigger Delete trigger Select triggers employee_tab Pre 11 server 11 server Single insert and delete triggers, and multiple update and select triggers on mutually exclusive columns Multiple insert, update, delete and select triggers without exclusivity rule. The multiple insert and delete triggers feature is new in Cheetah. Update triggers Insert triggers Delete triggers Update triggers Select triggers New Feature: Multiple Triggers New feature: No Exclusivity rule

Insert Trigger Create trigger emptab_instrig INSERT on emptab Referencing new as n Before ( insert into log_tab(CURRENT, “start of emptab insert”) ) For each row ( execute procedure update_sal (n.empsalary) into empsalary, execute procedure insert_payroll (n.empid, n.emplname, n.empfname, n.empaddr, n.empaddrress, n.empsalary) ) After ( insert into log_tab(CURRENT, “end of emptab insert”) );

Create trigger emptab_instrig INSERT on emptab

Referencing new as n

Before ( insert into log_tab(CURRENT, “start of emptab insert”) )

For each row

( execute procedure update_sal (n.empsalary) into empsalary,

execute procedure insert_payroll (n.empid, n.emplname, n.empfname, n.empaddr, n.empaddrress, n.empsalary) )

After ( insert into log_tab(CURRENT, “end of emptab insert”) );

Insert Trigger -- add a new trigger Create trigger emptab_instrig2 INSERT on emptab Referencing new as n Before ( insert into log_tab(CURRENT, “start emptab insert”) ); For each row (execute procedure update_401k(n.empid, n.emplname, n.empfname, n.empaddr, n.addrress,n.empsalary)) After ( insert into log_tab(CURRENT, “end of emptab insert”) );

Create trigger emptab_instrig2 INSERT on emptab

Referencing new as n

Before ( insert into log_tab(CURRENT, “start emptab insert”) );

For each row

(execute procedure update_401k(n.empid, n.emplname, n.empfname, n.empaddr, n.addrress,n.empsalary))

After ( insert into log_tab(CURRENT, “end of emptab insert”) );

Delete Trigger Create trigger emptab_deltrig DELETE on emptab Referencing old as o For each row ( execute procedure delete_payroll (o.empid ); -- new trigger for DELETE Create trigger emptab_instrig2 DELETE on emptab Referencing old as o For each row (execute procedure delete_401k(n.empid));

Create trigger emptab_deltrig DELETE on emptab

Referencing old as o

For each row

( execute procedure delete_payroll (o.empid );

-- new trigger for DELETE

Create trigger emptab_instrig2 DELETE on emptab

Referencing old as o

For each row

(execute procedure delete_401k(n.empid));

Update Trigger multiple update triggers on the complete table. Multiple update triggers on each column or set of columns In summary, multiple update triggers on any combination.

multiple update triggers on the complete table.

Multiple update triggers on each column or set of columns

In summary, multiple update triggers on any combination.

Writing trigger code, easily. Passing row values into procedures Accessing and modifying row values easily within triggers. Use the power of stored procedure to enforce the business rules.

Passing row values into procedures

Accessing and modifying row values easily within triggers.

Use the power of stored procedure to enforce the business rules.

create trigger ins_trig_tab1 INSERT on tab1 referencing new as post for each row(execute procedure proc1() with trigger references); create trigger ins_trig_tab2 INSERT on tab2 referencing new as n for each row (execute procedure proc2() with trigger references);

create trigger ins_trig_tab1 INSERT on tab1 referencing new as post

for each row(execute procedure proc1() with trigger references);

create trigger ins_trig_tab2 INSERT on tab2 referencing new as n

for each row (execute procedure proc2() with trigger references);

create table tab1 (col1 int,col2 int); create table tab2 (col1 int); create table temptab1 (old_col1 int, new_col1 int, old_col2 int, new_col2 int); create procedure proc1() referencing OLD as o NEW as n for tab1; if ( INSERTING ) then -- INSERTING new boolean function n.col1 = n.col1 + 1; insert into temptab1 values(0,n.col1,1,n.col2); end if if ( UPDATING ) then -- UPDATING new boolean function insert into temptab1 values(o.col1,n.col1,o.col2,n.col2); end if if ( SELECTING ) then -- SELECTING new boolean function -- you can access relevant old and new values. insert into temptab1 values (o.col1,0,o.col2,0); end if if ( DELETING ) then -- DELETING new boolean function delete from temptab1 where temptab1.col1 = o.col1; end if end procedure; create procedure proc2() referencing OLD as o NEW as n for tab2 returning int; LET n.col1 = n.col1 * 1.1 ; -- increment the inserted value 10% end procedure; -- new syntax. New boolean functions: INSERTING SELECTING DELETING UPDATING -- You can modify the new row values.

create table tab1 (col1 int,col2 int);

create table tab2 (col1 int);

create table temptab1 (old_col1 int, new_col1 int, old_col2 int, new_col2 int);

create procedure proc1()

referencing OLD as o NEW as n for tab1;

if ( INSERTING ) then -- INSERTING new boolean function

n.col1 = n.col1 + 1;

insert into temptab1 values(0,n.col1,1,n.col2);

end if

if ( UPDATING ) then -- UPDATING new boolean function

insert into temptab1 values(o.col1,n.col1,o.col2,n.col2);

end if

if ( SELECTING ) then -- SELECTING new boolean function

-- you can access relevant old and new values.

insert into temptab1 values (o.col1,0,o.col2,0);

end if

if ( DELETING ) then -- DELETING new boolean function

delete from temptab1 where temptab1.col1 = o.col1;

end if

end procedure;

create procedure proc2()

referencing OLD as o NEW as n for tab2

returning int;

LET n.col1 = n.col1 * 1.1 ; -- increment the inserted value 10%

end procedure;

Explain Enhancements Explain enhancements Query Optimization Time Distributed query support Estimated and Actual cardinalities Controlled by EXPLAIN_STAT Dynamic explain enhancements Prints the location the explain file in online.log Provides any errors while trying to create the file onmode –Y <sid> 0|1|2 [filename] Specify the location of the dynamic explain Specify whether you need query stats

Explain enhancements

Query Optimization Time

Distributed query support

Estimated and Actual cardinalities

Controlled by EXPLAIN_STAT

Dynamic explain enhancements

Prints the location the explain file in online.log

Provides any errors while trying to create the file

onmode –Y <sid> 0|1|2 [filename]

Specify the location of the dynamic explain

Specify whether you need query stats

New Functions and Expressions ASCII(character_expression) The ASCII function returns the decimal representation of the first character in a character string. ASCII(‘a’) returns: 97 ASCII(‘Hello World!’) returns: 72 ASCII ADD_MONTHS(date/datetime expression , integer ) Adds months to a date or datetime value or expression. The first argument is date or datetime expression and second is an integer. Return type is same as first argument. ADD_MONTHS('4/16/2004', 20) returns: 12/16/2005 ADD_MONTHS(CURRENT, 3) returns: 2007-09-19 10:04:11.00000 ADD_MONTHS()

New Functions and Expressions CEIL (numerical_expession) returns the DECIMAL(32) representation of the smallest integer that is greater than or equal to its single argument. FLOOR(numerical_expression) returns the DECIMAL(32) representation of the largest integer that is smaller than or equal to its single argument. CEIL(-54.232) returns -54 FLOOR(-54.232) returns -55 CEIL(54.232) returns 55 FLOOR(54.232) returns 54 CEIL FLOOR BITAND(num1, num2) – returns the bitwise ANDed value. BITOR(num1, num2) – returns the bitwise ORed value. BITXOR(num1, num2) – returns the bitwise XORed value. BITNOT(num1) – returns the bitwise ANDed value. BITANDNOT(num1, num2) – is a short form for BITAND(arg1, BITNOT(arg2)) Bitwise functions

New Functions and Expressions LAST_DAY(date or datetime expression) Returns last day of the month in the argument. SELECT TODAY AS today, CURRENT AS current, LAST_DAY(TODAY) AS last_from_today, LAST_DAY(CURRENT) AS last_from_current FROM systables WHERE tabid = 1; today 06/19/2007 current 2007-06-19 10:23:01.000 last_from_today 06/30/2007 last_from_current 2007-06-30 10:23:01.00000 LAST_DAY FORMAT_UNITS(number, precision, units) Helps formatting of numbers in kilobytes to peta bytes. Detailed explanation with examples is in IDS SQL Syntax guide. SELECT FORMAT_UNITS( SUM(chksize), 'P') size, FORMAT_UNITS( SUM(nfree), 'p') free FROM syschunks; size 117 MB free 8.05 MB FORMAT_UNITS

New Functions and Expressions MONTHS_BETWEEN(date/datetime expr, date/datetime expr) Returns the difference between two date or datetime expressions in decimal, based on 31day months. SELECT CURRENT, MONTHS_BETWEEN(TODAY, LAST_DAY(CURRENT)) FROM systables WHERE tabid = 1; (expression) (expression) 2007-06-19 10:51:57.000 -0.3694433243728 MONTHS_BETWEEN LTRIM(source_string, pad_string) Returns the source_string after removing specified leading pad characters from a string. LTRIM will remove leading blanks when you simply pass the source string. LTRIM(‘Hello Cheetah!’, ‘Hello ‘) returns: Cheetah! LTRIM

New Functions and Expressions TO_NUMBER(character or numeric expression) converts a number or a character expression representing a number value to a DECIMAL. TO_NUMBER NULLIF(arg1, arg2) Returns NULL if arg1 and arg2 are equal, else returns arg1. If both are NULL – they won’t be equal – but still returns NULL because arg1 is NULL. NULLIF Next_day(date or datetime expr, abbreviated day of the week) Returns the date or datetime for next day matching the second argument. EXECUTE FUNCTION NEXT_DAY(TODAY, 'Mon') returns: 06/25/2007 EXECUTE FUNCTION NEXT_DAY(CURRENT, 'Mon') returns: 2007-06-25 6:57:52.00000 NEXT_DAY

Enhancements to Functions Works same as POW() function. POWER Returns the same value as CURRENT DATETIME year to fraction(5) SYSDATE These two functions can now take date and datetime expressions. IDS Syntax guide explains this in detail. TRUNC ROUND TO_CHAR(numeric expression) In addition to exisiting functionality, in IDS v11.10, this function will convert a number into a charater string. TO_CHAR

SYSDBOPEN() and SYSDBCLOSE() CREATE PROCEDURE public.sysdbopen (); SET pdqpriority 1; SET ENVIRONMENT OPTCOMPIND '1'; SET ISOLATION COMMITTED READ; SET ENVIRONMENT USELASTCOMMITTED 'ALL'; SYSTEM &quot;echo Logged IN: &quot; || USER || &quot; &quot; || CURRENT || &quot; >> /tmp/mylog&quot;; END PROCEDURE; -- log the entry. CREATE PROCEDURE public.sysdbclose (); SYSTEM &quot;echo Logged OUT: &quot; || USER || &quot; &quot; || CURRENT || &quot; >> /tmp/mylog&quot;; END PROCEDURE;

CREATE PROCEDURE public.sysdbopen ();

SET pdqpriority 1;

SET ENVIRONMENT OPTCOMPIND '1';

SET ISOLATION COMMITTED READ;

SET ENVIRONMENT USELASTCOMMITTED 'ALL';

SYSTEM &quot;echo Logged IN: &quot; || USER || &quot; &quot; ||

CURRENT || &quot; >> /tmp/mylog&quot;;

END PROCEDURE;

-- log the entry.

CREATE PROCEDURE public.sysdbclose ();

SYSTEM &quot;echo Logged OUT: &quot; || USER || &quot; &quot; ||

CURRENT || &quot; >> /tmp/mylog&quot;;

END PROCEDURE;

Named Parameters Support for JDBC This is an example of implicit positional binding: CallableStatement cstmt = con.prepareCall(&quot;call order_item(?, ?, ?, ?, ?,?, ?)&quot;); // Set parameters (positional notation) cstmt.setInt( 1 , 5739 ); cstmt.setInt( 2 , 8294); cstmt.setString( 6 , &quot;345, University ave.&quot;); cstmt.setInt( 7 , 94303); cstmt.setString( 4 ,&quot;4100 Bohannon Dr.&quot;); cstmt.setInt( 5 , 94025); cstmt.setInt( 3 ,5); // Execute cstmt.execute();

This is an example of implicit positional binding:

CallableStatement cstmt = con.prepareCall(&quot;call order_item(?, ?, ?, ?, ?,?, ?)&quot;);

// Set parameters (positional notation)

cstmt.setInt( 1 , 5739 );

cstmt.setInt( 2 , 8294);

cstmt.setString( 6 , &quot;345, University ave.&quot;);

cstmt.setInt( 7 , 94303);

cstmt.setString( 4 ,&quot;4100 Bohannon Dr.&quot;);

cstmt.setInt( 5 , 94025);

cstmt.setInt( 3 ,5);

// Execute

cstmt.execute();

Named Parameters Support for JDBC Here is the same code rewritten using named parameter notation: // Set parameters (named notation) cstmt.setInt(&quot; cust_id &quot;, 5739 ); Chapter 9. SQL Language 275 cstmt.setInt(&quot; item_id &quot;, 8294); cstmt.setString(&quot; shipping_addr &quot;, &quot;345, University ave.&quot;); cstmt.setInt(&quot; shipping_zip &quot;, 94303); cstmt.setString(&quot; billing_addr &quot;,&quot;4100 Bohannon Dr.&quot;); cstmt.setInt(&quot; billing_zip &quot;, 94025); cstmt.setInt(&quot; count &quot;, 5); // Execute cstmt.execute();

Stored Procedure Enhancements GO TO Statement LOOP Statement Stored procedure recompilation

GO TO Statement

LOOP Statement

Stored procedure recompilation

GO TO Statement CREATE PROCEDURE simple_goto(a int) RETURNS int; IF a < 50 THEN GOTO lessval; -- jump to lessval. END IF; RETURN a * a; -- return something <<lessval>> -- define the label lessval RETURN a * -1; END PROCEDURE;

CREATE PROCEDURE simple_goto(a int)

RETURNS int;

IF a < 50 THEN

GOTO lessval; -- jump to lessval.

END IF;

RETURN a * a; -- return something

<<lessval>> -- define the label lessval

RETURN a * -1;

END PROCEDURE;

LOOP Statement CREATE PROCEDURE plusnum(a int) RETURNS int; DEFINE x int; LET x = a; LOOP -- start the loop LET x = x + 1; IF x < 0 THEN CONTINUE; -- increment until +ve number. ELIF x < 1000 THEN RETURN x WITH RESUME; -- return and come back. END IF; EXIT LOOP WHEN x = 1000; -- exit loop at 1000. END LOOP; END PROCEDURE;

CREATE PROCEDURE plusnum(a int)

RETURNS int;

DEFINE x int;

LET x = a;

LOOP -- start the loop

LET x = x + 1;

IF x < 0 THEN

CONTINUE; -- increment until +ve number.

ELIF x < 1000 THEN

RETURN x WITH RESUME; -- return and come back.

END IF;

EXIT LOOP WHEN x = 1000; -- exit loop at 1000.

END LOOP;

END PROCEDURE;

LOOP Statement CREATE PROCEDURE sumto_a(a int) RETURNS int; DEFINE x int; DEFINE total int; LET total = 0; FOR x IN (1 TO a) LOOP -- start the loop LET total = total + x; END LOOP; RETURN total; END PROCEDURE;

CREATE PROCEDURE sumto_a(a int)

RETURNS int;

DEFINE x int;

DEFINE total int;

LET total = 0;

FOR x IN (1 TO a) LOOP -- start the loop

LET total = total + x;

END LOOP;

RETURN total;

END PROCEDURE;

LOOP Statement CREATE PROCEDURE facto(a int) RETURNS int AS x, int AS factx; DEFINE x,y, factx int; DEFING total int; LET total = 0; LET x = 1; <<main_loop>> WHILE (x <= a) LOOP -- start the loop LET factx = 1; <<facto_loop>> FOR y IN (1 TO x) LOOP -- Abruptly stop at 10 EXIT main_loop WHEN x = 10; LET factx = factx * y; END LOOP facto_loop; RETURN x, factx WITH RESUME; LET X = x+1; END LOOP main_loop; END PROCEDURE;

CREATE PROCEDURE facto(a int)

RETURNS int AS x, int AS factx;

DEFINE x,y, factx int;

DEFING total int;

LET total = 0;

LET x = 1;

<<main_loop>>

WHILE (x <= a) LOOP -- start the loop

LET factx = 1;

<<facto_loop>>

FOR y IN (1 TO x) LOOP

-- Abruptly stop at 10

EXIT main_loop WHEN x = 10;

LET factx = factx * y;

END LOOP facto_loop;

RETURN x, factx WITH RESUME;

LET X = x+1;

END LOOP main_loop;

END PROCEDURE;

Stored Procedure language(SPL) routines IDS Client IDS Client Informix routine manager Informix Dynamic Server Query processing and Optimizer C Language routines Java language routines

SQL inside stored procedure create procedure foo_static (vpkid int,vname varchar(32)) returns int; update mytab set myname = vname where pkid = vpkid; End procedure;

SQL inside stored procedure

create procedure foo_static

(vpkid int,vname varchar(32)) returns int;

update mytab set myname = vname

where pkid = vpkid;

End procedure;

Dynamic Statement create procedure foo_dynamic(vpkid int, vname varchar(32), tabname varchar(128)) returns int; define query lvarchar(512); let query = &quot;update &quot; || tabname || &quot;set myname = '&quot; || vname || &quot;' where pkid = &quot; || vpkid; execute immediate query; return sqlcode ; end procedure; execute procedure foo_dynamic (123, &quot;John Doe&quot;, &quot;customer&quot;);

Dynamic Statement

create procedure foo_dynamic(vpkid int,

vname varchar(32), tabname varchar(128)) returns int;

define query lvarchar(512);

let query = &quot;update &quot; || tabname || &quot;set myname = '&quot; || vname || &quot;' where pkid = &quot; || vpkid;

execute immediate query;

return sqlcode ;

end procedure;

execute procedure foo_dynamic

(123, &quot;John Doe&quot;, &quot;customer&quot;);

Dynamic SQL Statement in SPL Non cursory statements EXECUTE IMMEDIATE Cursory statements PREPARE, DECLARE, OPEN, FETCH, CLOSE, FREE SQLCODE function

Dynamic SQL Statement in SPL

Non cursory statements

EXECUTE IMMEDIATE

Cursory statements

PREPARE, DECLARE, OPEN, FETCH, CLOSE, FREE

SQLCODE function

EXECUTE IMMEDIATE { SQL_quoted_string | Str_variable } ; SQL_quoted_string: A string containing a single SQL statement Str_variable: A character variable containing the SQL statement Only string values or variables are allowed. Statement length is limited to 32K. Allows only ONE SQL statement allowed.

EXECUTE IMMEDIATE { SQL_quoted_string | Str_variable } ;

SQL_quoted_string: A string containing a single SQL statement

Str_variable: A character variable containing the SQL statement

Only string values or variables are allowed. Statement length is limited to 32K.

Allows only ONE SQL statement allowed.

create procedure update_this( owner varchar(32), tabname varchar(32), cid int, val decimal(10,2), operation varchar(24)) r eturns int as operation_code; define oper varchar(4); define update_query lvarchar; let oper = &quot;None&quot;; if (operation = &quot;add&quot;) then let oper = &quot; + &quot;; elif (operation = &quot;subtract&quot;) then let oper = &quot; - &quot;; else return -11111; end if; let update_query = &quot;update &quot; || owner || &quot;.&quot; || tabname || &quot; set balance = balance &quot; || oper || val || &quot; &quot;; if (tabname = &quot;noordinary_joe&quot;) then let update_query = update_query || &quot;, last_priv_usage = CURRENT &quot;; end if; let update_query = update_query || &quot;where customer = &quot; || cid; insert into log(b) values(update_query); EXECUTE IMMEDIATE update_query; return sqlcode ; end procedure;

create procedure update_this( owner varchar(32), tabname varchar(32), cid int, val decimal(10,2), operation varchar(24)) r eturns int as operation_code;

define oper varchar(4);

define update_query lvarchar;

let oper = &quot;None&quot;;

if (operation = &quot;add&quot;) then

let oper = &quot; + &quot;;

elif (operation = &quot;subtract&quot;) then

let oper = &quot; - &quot;;

else

return -11111;

end if;

let update_query = &quot;update &quot; || owner || &quot;.&quot; || tabname || &quot; set balance = balance &quot; || oper || val || &quot; &quot;;

if (tabname = &quot;noordinary_joe&quot;) then

let update_query = update_query || &quot;, last_priv_usage = CURRENT &quot;;

end if;

let update_query = update_query || &quot;where customer = &quot; || cid;

insert into log(b) values(update_query);

EXECUTE IMMEDIATE update_query;

return sqlcode ;

end procedure;

SQL Injection http://xkcd.com/327/ http://imgs.xkcd.com/comics/exploits_of_a_mom.png This work is licensed under a Creative Commons Attribution- NonCommercial 2.5 License . This means you're free to copy and share these comics (but not to sell them).

Need to use EXECUTE FUNCTION or EXECUTE PROCEDURE From JDBC, use Callable statements Using procedures with dynamic statements within other DML statements is disallowed SELECT MYPROC() from systables where tabid = 1; Use the new SQLCODE function to check for any error It should return zero when the statement is successful

Need to use EXECUTE FUNCTION or EXECUTE PROCEDURE

From JDBC, use Callable statements

Using procedures with dynamic statements within other DML statements is disallowed

SELECT MYPROC() from systables where tabid = 1;

Use the new SQLCODE function to check for any error

It should return zero when the statement is successful

Cursor support in dynamic SQL PREPARE <statementname> from query DECLARE <cursorname> cursor for <statementname> OPEN <cursor> FETCH <cursor>, CLOSE <cursor> FREE <cursor>, FREE <statement-id> sqlcode for error check on each operation

Cursor support in dynamic SQL

PREPARE <statementname> from query

DECLARE <cursorname> cursor for <statementname>

OPEN <cursor>

FETCH <cursor>, CLOSE <cursor>

FREE <cursor>, FREE <statement-id>

sqlcode for error check on each operation

create procedure getlist (skipm int, firstn int, maxid int) returns int, varchar(128) define tabname varchar(128); define tabid integer; define query varchar(128); let query = &quot;select skip &quot; || skipm || &quot;first &quot; || firstn || &quot; tabid, tabname from systables where tabid < ? and tabname = ? order by 1&quot;; prepare stmt_id from query; declare myCur cursor for stmt_id; open myCur using maxid, myname; while (1=1) fetch myCur into tabid, tabname; if ( sqlcode != 0) then exit; end if; return tabid, tabname with resume; end while; close myCur; free myCur; free stmt_id; end procedure; execute procedure getlist(maxid=100, skipm=10, firstn=5);

create procedure getlist (skipm int, firstn int, maxid int)

returns int, varchar(128)

define tabname varchar(128);

define tabid integer;

define query varchar(128);

let query = &quot;select skip &quot; || skipm || &quot;first &quot; || firstn

|| &quot; tabid, tabname from systables where tabid < ? and tabname = ? order by 1&quot;;

prepare stmt_id from query;

declare myCur cursor for stmt_id;

open myCur using maxid, myname;

while (1=1)

fetch myCur into tabid, tabname;

if ( sqlcode != 0) then

exit;

end if;

return tabid, tabname with resume;

end while;

close myCur;

free myCur;

free stmt_id;

end procedure;

execute procedure getlist(maxid=100, skipm=10, firstn=5);

Summary IDS 11.5 supports dynamic statements in its stored procedure language EXECUTE IMMEDIATE PREPARE, DELCARE, OPEN, FETCH, CLOSE, FREE SQLCODE IDS 11.5 Infocenter http://publib.boulder.ibm.com/infocenter/idshelp/v115/index.jsp

Summary

IDS 11.5 supports dynamic statements in its stored procedure language

EXECUTE IMMEDIATE

PREPARE, DELCARE, OPEN, FETCH, CLOSE, FREE

SQLCODE

IDS 11.5 Infocenter

http://publib.boulder.ibm.com/infocenter/idshelp/v115/index.jsp

Stored Procedure Recompilation This will be in 10.00.xC7 and 11.10.xC2 -211 error: Cannot read system catalog sysprocplan Happens after altering the table or creating/dropping indices. Also happens if you lock procedure plans when you UPDATE STATISTICS for procedures within BEGIN– COMMIT. Automatic Recompilation Automatically recognize and recompile the required statements Write back the plan in a separate transaction so the locks are release. Automatically available. Nothing for applications to do.

This will be in 10.00.xC7 and 11.10.xC2

-211 error: Cannot read system catalog sysprocplan

Happens after altering the table or creating/dropping indices.

Also happens if you lock procedure plans when you UPDATE STATISTICS for procedures within BEGIN– COMMIT.

Automatic Recompilation

Automatically recognize and recompile the required statements

Write back the plan in a separate transaction so the locks are release.

Automatically available. Nothing for applications to do.

 

Savepoints. Savepoints are markers within a transaction. Enables you to take corrective action within the transaction by partially rolling back the transaction. Even after the partial rollback, the transaction is still open. E.g. Customer order transaction. Basic order entry goes thru. Couldn’t update the info at CRM database System down, new user is still unknown/etc Rollback the status for order entry Save the non-critical activity for batch processing later But, continue with order entry transaction successfully.

Savepoints are markers within a transaction.

Enables you to take corrective action within the transaction by partially rolling back the transaction.

Even after the partial rollback, the transaction is still open.

E.g. Customer order transaction.

Basic order entry goes thru.

Couldn’t update the info at CRM database

System down, new user is still unknown/etc

Rollback the status for order entry

Save the non-critical activity for batch processing later

But, continue with order entry transaction successfully.

Savepoints. Begin work; Insert into mytab values(00, “step 00 completed”); Savepoint step1; Insert into mytab values(10, “step 10 completed”); Savepoint step2; Insert into mytab values(20, “step 20 completed”); Savepoint step3; Insert into mytab values(30, “step 30 completed”); -- something happens Rollback work to savepoint step2; -- continue the transaction; Savepoint stepfixup; Insert into mytab values(55, “All steps done”); Commit work;

Begin work;

Insert into mytab values(00, “step 00 completed”);

Savepoint step1;

Insert into mytab values(10, “step 10 completed”);

Savepoint step2;

Insert into mytab values(20, “step 20 completed”);

Savepoint step3;

Insert into mytab values(30, “step 30 completed”);

-- something happens

Rollback work to savepoint step2;

-- continue the transaction;

Savepoint stepfixup;

Insert into mytab values(55, “All steps done”);

Commit work;

Savepoints Enabled for All Informix APIs ESQL/C JDBC, ODBC, etc JCC will have the functionality in their next fixpack. Standard compliant. Scope of the savepoint names in procedures is limited to that instance of the stored procedure. Distributed queries are supported. You can enforce the uniqueness of savepoint names.

Enabled for All Informix APIs

ESQL/C

JDBC, ODBC, etc

JCC will have the functionality in their next fixpack.

Standard compliant.

Scope of the savepoint names in procedures is limited to that instance of the stored procedure.

Distributed queries are supported.

You can enforce the uniqueness of savepoint names.

Thank You [email_address] Session 2258

Thank You

[email_address]

Session 2258

Add a comment

Related presentations

Related pages

Advanced SQL, Stored Procedures and Triggers for Microsoft ...

Advanced SQL, Stored Procedures and Triggers for Microsoft SQL Server Training Course The aim of this course is to provide a clear understanding of the ...
Read more

Advanced MySQL Features - PHPKnowHow - PHP Tutorial ...

... stored procedures and other MySQL advanced features ... we have covered most of the MySQL features that you ... a trigger can be defined ...
Read more

Database Triggers - Oracle Help Center

These procedures are called database triggers. Triggers are similar to stored procedures, ... The standard security features of Oracle, ...
Read more

Top 10 advanced features of SQL Server 2005 - TechRepublic

Tim Chapman discusses 10 of the more advanced new features of the SQL Server 2005 ... stored procedures, triggers, ... TechRepublic's free SQL Server ...
Read more

SQL Server: Triggers, Stored Procedures, and Functions ...

SQL Server: Triggers, Stored Procedures, ... 0s 1h 52m Advanced Sep 24, ... Comparing triggers, functions, and stored procedures;
Read more

3 Triggers and Stored Procedures - Oracle Help Center

3 Triggers and Stored Procedures. ... Triggers are similar to stored procedures but differ in the way that they are invoked. Support for triggers in MySQL ...
Read more

SQL Developer Feature List - Oracle | Integrated Cloud ...

Oracle SQL Developer Feature List . ... Triggers; Crossedition Triggers; Types; ... Advanced data type viewers for BLOBs, ...
Read more

Advanced Features of High-Speed Digital I/O Devices ...

... Advanced Features of High-Speed ... features such as frequency ranges and resolution, importing and exporting clocks and triggers, and advanced ...
Read more

SQL Server: Comparing Triggers, Functions and Procedures

SQL Server: Comparing Triggers, Functions and Procedures. show more Comparing triggers, functions, ... Video duration: 3m 25s 1h 52m Advanced Sep 24, 2012.
Read more