Dot Net Fluke: Getting by on C# for iSeries RPG Developers

Useful tutorials on C# and .NET for RPG iSeries AS/400 developers. Brought to you from the folks at AranRock Consulting

8/10/10

How to Create Stored Procedures

Stored procedures are programs written in SQL.  They are their own object, stored on the iSeries db and can accept parameters. They are programs with their own variables, flow control and can be called by any other application. Their benefit is in being able to stack a bunch of statements together and so improve performance, reduce network traffic and make your life simple! Why have multiple db statements executing when you can issue one that does it all! It encourages separation of the data from logic and forces you to be modular. Begone Spaghetti!
They also help security; especially useful in distributed  apps (client/server)  so that the code that updates the db is hidden in the stored procedure and not visible at the front end. Finally they are a super simple way to do a remote call.

You create a stored procedure using iSeries Navigator or directly through your 5250 screen. I'll go over the 5250 way since it is more verbose than the GUI.
To create our procedure we will use  the
CREATE PROCEDURE command in SQL -called an SQL stored procedure. You can also create them in RPG - an external procedure. 

There are 4 main parts to the creation:

1. The NAME of the procedure

 CREATE PROCEDURE  MyProcedureName
2. Parameters I have an input parm of 10 chars and an I/O parm of type integer

( IN REQKEY CHARACTER(10), INOUT RETVAL INTEGER )
 3. Procedure properties The language will always be SQL

 LANGUAGE SQL 
 3. The procedure code

BEGIN  DECLARE CURS1 CURSOR FOR     SELECT CUSTNAME + 1 FROM MYLIB. MYTABLE      WHERE TELNO = REQKEY FOR UPDATE OF CUSTNAME;
Here's an example which creates a stored procedure in SQL for updating a table called MYTABLE.
This has two fields; CustName and TelNo.  When called with a
customer name,  it returns the phone number.

 CREATE PROCEDURE MYLIB.STOREDPROC         
(IN REQKEY CHARACTER(10),     INOUT RETVAL INTEGER )
RESULT SETS  0
LANGUAGE SQL  
NOT DETERMINISTIC
/*Start of Procedure Code*/
  BEGIN  /* Code always begins with 'Begin' */
  DECLARE CURS1 CURSOR FOR
    SELECT CUSTNAME + 1 FROM MYLIB. MYTABLE
    WHERE TELNO = REQKEY
FOR UPDATE OF CUSTNAME;   OPEN CURS1  ;
FETCH CURS1  INTO RETVAL ;
UPDATE MYLIB. MYTABLE SET CUSTNAME= RETVAL
    WHERE CURRENT OF CURS1 ;
 END



I'll cover calling these from .NET in more detail soon. 
In the meantime, create yourself a simple Stored 
procedure and try calling it. Then take one of  your
 existing programs and replace your inline SQL 
replacing it with a stored procedure.


Labels: ,

3 Comments:

At October 29, 2009 at 8:24 AM , Blogger Unknown said...

I got some value out of this; would be interested in seeing part 2, calling the SP from a .net web form.

 
At January 8, 2010 at 1:45 PM , Blogger The Twilight Troll said...

I don't know much about the ISeries and the admins I work with don't know much about SQL within the ISeries.

I'd like to create a stored procedure to update a record if it exists otherwise insert the record if it doesn't. This works well for SQL Server.

However, my ISeries admin is worried that if I create stored proc in DB2 or need to drop it that we will have to get everyone out of either the table or the database. This doesn't seem right to me but the ISeries is a strange beast.

I don't know if this matters but most of our files are declared through DDA (RPG) rather than SQL.

What are your thoughts? Will they have to get out of the db or table?

Thank You.

 
At March 2, 2010 at 12:12 PM , Blogger Scott said...

db2 sql stored procedures are their own cle *pgm objects, and are not related to a db2 table. no table lock is needed to create or delete because it has nothing to do with a table.

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home