Sybase Technical Library - Product Manuals Home
[Search Forms] [Previous Section with Hits] [Next Section with Hits] [Clear Search] Expand Search

Chapter 13 Retrieving information with System
Procedures [Table of Contents] Appendix A Backward
compatibility with MDI database gateways

Access Service User's Guide DirectConnect for AS/400

[-] Chapter 14 Using AS/400 Stored Procedures

Chapter 14

Using AS/400 Stored Procedures

This chapter describes how to create and implement stored procedures within the AS/400 and covers the following topics:

General description

An AS/400 stored procedure can be a SQL stored procedure (SSP) or an external stored procedure (ESP).

Unless specified otherwise in this chapter, both types of stored procedures are called "AS/400 stored procedures."

Advantages to using AS/400 stored procedures include the following:

Read the following sections for more information about each type of stored procedure.

SQL Stored Procedures

A SQL stored procedure consists of a single SQL statement (such as insert, update, delete, grant) that is compiled and bound at the database. Once you create a SQL stored procedure, any client application can use it.

External Stored Procedures

External stored procedures apply only to installations using OS/400 V3R1or higher.

An external stored procedure is a statically bound SQL call statement. It allows a user to make a call from within the AS/400 interface to a program that is outside of the database. The advantage of using an external stored procedure is that it is the only way to retrieve return argument values from a called AS/400 program.

An external stored procedure can return succeed or fail status to the client application by signalling its invoking program QSQCALL. For details, refer to the IBM AS/400 DB2/400 SQL Programming manual.

How external Stored Procedures differ from SQL Stored Procedures

An external stored procedure differs from a SQL stored procedure as follows:

Creating an AS/400 Stored Procedure

To create an AS/400 stored procedure, perform the following tasks:

All tables that the AS/400 stored procedure refers to should be qualified with the collection name. If a table is not qualified with the collection name, AS/400 SQL uses the user profile name of the AS/400 job (connection).

The access service library requires a naming convention to distinguish between SQL stored procedures that return a result set, those that do not, and external stored procedures:

Variables and syntax in Stored Procedures

An AS/400 stored procedure can contain variables, so that arguments can be passed to it when it runs. A argument can be used for any data value in the SQL statement but cannot be used for any table or column name.

The syntax for AS/400 stored procedures is as follows:

create procedure 
procname arglist optionlist as sqlstatement

where:

Arguments in the arglist must be in the following format:

@parmname argtype, ...

or

&parmname argtype, ...

where:

The following requirements apply to the use of variables:

It is best to describe the arguments exactly as they appear in the corresponding column. In some cases, this can allow the AS/400 DBMS to use the SQL statement more efficiently.

The optionlist allows a client to specify information about the stored procedure. The client can specify options only when SQL transformation is in Passthrough mode. Sybase mode does not allow options.

A client can specify the following options:

To include options in a create procedure statement, use a format as follows:

verify=req, isolv=ur, owner=userid

Consider the following guidelines for a create procedure statement:

If the name you supply for procname already exists, the create procedure statement fails. Either drop the conflicting procedure or use a different name for the new procedure.

Interaction between AS/400 Stored Procedures and CSPs

This section describes the interaction between AS/400 stored procedures and catalog stored procedures (CSPs). It also contains information about creating an AS/400 stored procedure and CSP tables.

When the create procedure statement executes, the following events occur:

Dropping an AS/400 Stored Procedure

To drop an AS/400 stored procedure, issue the drop procedure command as follows:

drop procedure procname 

where

procname is the name of the procedure to drop.

The access service library removes all relevant rows from the NULLID.SYSPROCS and NULLID.SYSPROCCOL CSP tables.

Granting authorization on an AS/400 Stored Procedure

After a create procedure statement executes successfully, the owner must grant execute authorization on the AS/400 stored procedure package to all users who will run that AS/400 stored procedure.

For example, if the AS/400 stored procedure executes an insert against the table XYZ, the owner must have insert authorization with grant option on table XYZ. However, the owner is not required to grant insert authorization on the table to each user of the AS/400 stored procedure; execute authorization is sufficient.

The following statement authorizes all users to execute an AS/400 stored procedure named ptest1:

grant execute on ptest1 to public

Executing an AS/400 Stored Procedure

The syntax for executing an AS/400 stored procedure depends upon the SQL transformation mode in effect:

Argument values

In the above transformation level examples, the arglist is a list of argument values separated by commas. These argument values must be specified in the exact order they are specified in the SQL statement contained in the AS/400 stored procedure. However, this is not necessarily the same order that was used in the create procedure statement.

Be sure to use a comma between argument values. For example:

use procedure y
1,2,'CHAR'

Another example, in which the first two arguments are null:

use procedure y
,,'FRED'

Datatypes of argument values

The datatypes of all argument values must be consistent with the datatypes of the relevant columns as follows:

Delimit characters in one of two ways:

Specify NULL values in one of two ways:

Procedure handling for AS/400 Stored Procedures

The following rules apply to AS/400 stored procedures:

Using the call Statement

To access an AS/400 stored procedure with a SQL language event, use the call statement:

call 
procname

where:

If the AS/400 program has any parameter definitions, the arguments needed to match the stored procedure interface must be declared directly in the call statement. In the following example, in library fred, program test1 accepts two arguments, a fixed character and an integer type:

call fred.test1 ('check', 123)

To reuse the call statement and include argument markers, use a dynamically prepared statement, inserting the question mark (?) for argument placeholders:

call fred.test1 (?, ?)

When the dynamically prepared statement executes, the access service substitutes the arguments into the argument marker positions.

In a dynamically prepared statement, all arguments are input only. Also, when a commit or rollback occurs and causes the statement to drop, you must prepare the statement again.

An AS/400 external stored procedure uses the call statement from within the AS/400 interface to call a program outside of the database. To access an AS/400 stored procedure using an external stored procedure, the client application must use a create procedure language event to define the external stored procedure, as follows:

create procedure x_myproc @C char(8) @I int as call fred.test1 (@C,@I)

Note that the "x_" prefix identifies the name of the external stored procedure.

Rules for arguments in call statements

The following rules apply to arguments in call statements:

Be sure the program can handle the datatypes declared in the define procedure statement. For example, RPG programs cannot handle VARCHAR datatypes, but COBOL programs can.


Chapter 13 Retrieving information with System
Procedures [Table of Contents] Appendix A Backward
compatibility with MDI database gateways