The CREATE PROCEDURE statement defines a routine.
For information on the use of CONSTANT statements, see the DECLARE statement.
Supply the procedure's name using ProcedureName and the procedure's parameters using ParameterList. In ESQL the procedure is implemented using the single ESQL statement Statement. This statement can also be a compound statement, for example BEGIN ... END.
Alternatively, you can use the EXTERNAL clause instead of Statement; (see External stored procedures, or External Java methods if you are using Java).
ESQL procedures, stored procedures, and Java methods (unlike ESQL functions) can have OUT and INOUT parameters in addition to IN parameters. This allows them to return several values to the caller, although they have no RETURN value as such.
Procedures defined in a module are local in scope to the current node only. To use the same procedure in more than one node, define it in a schema.
Using the RETURNS clause allows you to invoke a routine as a procedure or a function. A procedure cannot return values of the ESQL reference data types.
>>--"-- className---.---methodName--"--------------><where
An example EXTERNAL NAME is "com.ibm.broker.test.MyClass.myMethod". The broker loads the Java class from the broker's CLASSPATH. If the specified class cannot be found an exception is thrown.
Overloaded LANGUAGE ESQL routines are not supported. (An overloaded routine is one that has the same name as another routine in the same broker schema which has a different number of parameters, or parameters with different types.) If the broker detects that a routine has been overloaded, it raises an exception.
OUT parameters passed into a procedure (whether internal or external) always contain a NULL value of the correct type when they are received by the procedure. This happens irrespective of their value before the CALL.
IN and INOUT parameters can be NULL when received by the procedure. For example, the following passes NULL to the procedure testProc:
DECLARE nullChar CHARACTER; CALL testProc( nullChar );
This happens because nullChar was not assigned a value before the CALL.
To call a stored procedure, you must define the procedure in the database and in the broker in ESQL. When writing stored procedures, in languages like C, you must use NULL indicators to ensure that your procedure can process the data correctly.
You can specify either a qualified or unqualified name in the EXTERNAL clause (the qualifier is the name of the database schema in which the procedure is defined). If you do not provide a schema name, the database connection user name is used as the default schema. If the required procedure does not exist in this schema, you must give an explicit schema name:
CREATE PROCEDURE ..... EXTERNAL NAME "mySchema.myProc";
If the procedure belongs to an Oracle package, you must provide an explicit qualified name of the form:
...EXTERNAL NAME "mySchema.myPackage.myProc"
You cannot use SQL wildcards for schema, package, or procedure names except to specify a dynamic schema name. The wildcards (percent, representing one or more characters, and underscore, representing a single character) are modified by the broker to include the database escape character immediately before each wildcard. The database therefore receives these as literal characters.
The clause below is modified by the broker and mySchema.Proc\_ is passed to the database (this assumes that the database escape character is a backslash);
...EXTERNAL NAME "mySchema.Proc_"
If you specify a database schema name as a single percent character when you create the procedure, the broker handles this as a special case and resolves the name at runtime. The clause below is valid and identifies a dynamic database schema:
...EXTERNAL NAME "%.myProc"
If you use this technique, you must include the EXTERNAL SCHEMA clause on the CALL statement that you use to invoke this procedure to identify the appropriate schema.
Database routine example 1 demonstrates how to define and call a stored procedure in Oracle and DB2. Although the database definitions vary between the databases, the ESQL does not. The names given to parameters in the ESQL do not have to match the names they are given on the database side. However, the external name of the procedure must match its defined name in the database, including any package or container specifications. Enclose ExternalRoutineName in quotation marks if it contains a character that is not allowed in an identifier.
All external procedures have the following restrictions:
The following ESQL code demonstrates how to define and call DB2 stored procedures:
ESQL Definition: DECLARE inputParm CHARACTER; DECLARE outputParm CHARACTER; DECLARE inputOutputParm CHARACTER; SET inputParm = 'Hello'; SET inputOutputParm = 'World'; CALL swapParms( inputParm, outputParm, inputOutputParm ); CREATE PROCEDURE swapParms ( IN parm1 CHARACTER, OUT parm2 CHARACTER, INOUT parm3 CHARACTER ) EXTERNAL NAME dbSwapParms;
To register this stored procedure with DB2, copy the following script to a file (for example, test1.sql)
-- DB2 Example Stored Procedure DROP PROCEDURE dbSwapParms @ CREATE PROCEDURE dbSwapParms ( IN in_param CHAR(32), OUT out_param CHAR(32), INOUT inout_param CHAR(32)) LANGUAGE SQL BEGIN SET out_param = inout_param; SET inout_param = in_param; END @and execute:
db2 -td@ -vf test1.sqlfrom the DB2 command prompt.
The following ESQL code demonstrates how to define and call Oracle stored procedures:
ESQL Definition: DECLARE inputParm CHARACTER; DECLARE outputParm CHARACTER; DECLARE inputOutputParm CHARACTER; SET inputParm = 'Hello'; SET inputOutputParm = 'World'; CALL swapParms( inputParm, outputParm, inputOutputParm ); CREATE PROCEDURE swapParms ( IN parm1 CHARACTER, OUT parm2 CHARACTER, INOUT parm3 CHARACTER ) EXTERNAL NAME dbSwapParms;
To register this stored procedure with Oracle, copy the following script to a file (for example, test1.sql)
CREATE OR REPLACE PROCEDURE dbSwapParms ( in_param IN VARCHAR2, out_param OUT VARCHAR2, inout_param IN OUT VARCHAR2 ) AS BEGIN out_param := inout_param; inout_param := in_param; END; /and execute:
sqlplus <userid>/<password> @test1.sql
The following ESQL code demonstrates how to define and call SQL Server stored procedures:
ESQL Definition: DECLARE inputParm CHARACTER; DECLARE outputParm CHARACTER; DECLARE inputOutputParm CHARACTER; SET inputParm = 'Hello'; SET inputOutputParm = 'World'; CALL swapParms( inputParm, outputParm, inputOutputParm ); CREATE PROCEDURE swapParms ( IN parm1 CHARACTER, INOUT parm2 CHARACTER, INOUT parm3 CHARACTER ) EXTERNAL NAME dbSwapParms;
To register this stored procedure with SQLServer, copy the following script to a file (for example, test1.sql)
-- SQLServer Example Stored Procedure DROP PROCEDURE dbSwapParms go CREATE PROCEDURE dbSwapParms @in_param CHAR(32), @out_param CHAR(32) OUT, @inout_param CHAR(32) OUT AS SET @out_param = @inout_param SET @inout_param = @in_param goand execute:
isql -U<userid> -P<password> -S<server> -d<datasource> -itest1.sql
If you declare these as OUT parameters in your ESQL you encounter a type mismatch error at run time. To avoid that mismatch you must declare SQL Server OUTPUT parameters as INOUT in your ESQL.
The following example shows the same procedure as in Database routine example 1, but implemented as an ESQL internal procedure. The CALL syntax for this procedure is the same, as well as the result.
CREATE PROCEDURE swapParms ( IN parm1 CHARACTER, OUT parm2 CHARACTER, INOUT parm3 CHARACTER ) BEGIN SET parm2 = parm3; SET parm3 = parm1; END;
The following example procedure parses a tree, visiting all places at and below the given starting point, and reports what it has found:
SET OutputRoot.MQMD = InputRoot.MQMD; DECLARE answer CHARACTER; SET answer = ''; CALL navigate(InputRoot.XML, answer); SET OutputRoot.XML.Data.FieldNames = answer; CREATE PROCEDURE navigate (IN root REFERENCE, INOUT answer CHARACTER) BEGIN SET answer = answer || 'Reached Field... Type:' || CAST(FIELDTYPE(root) AS CHARACTER)|| ': Name:' || FIELDNAME(root) || ': Value :' || root || ': '; DECLARE cursor REFERENCE TO root; MOVE cursor FIRSTCHILD; IF LASTMOVE(cursor) THEN SET answer = answer || 'Field has children... drilling down '; ELSE SET answer = answer || 'Listing siblings... '; END IF; WHILE LASTMOVE(cursor) DO CALL navigate(cursor, answer); MOVE cursor NEXTSIBLING; END WHILE; SET answer = answer || 'Finished siblings... Popping up '; END;
When given the following input message:
<Person><Name>John Smith</Name><Salary period='monthly' taxable='yes'>-1200</Salary></Person>
the procedure produces the following output, which has been manually formatted:
Reached Field... Type:16777232: Name:XML: Value :: Field has children... drilling down Reached Field... Type:16777216: Name:Person: Value :: Field has children... drilling down Reached Field... Type:16777216: Name:Name: Value :John Smith: Field has children... drilling down Reached Field... Type:33554432: Name:: Value :John Smith: Listing siblings... Finished siblings... Popping up Finished siblings... Popping up Reached Field... Type:16777216: Name:Salary: Value :-1200: Field has children... drilling down Reached Field... Type:50331648: Name:period: Value :monthly: Listing siblings... Finished siblings... Popping up Reached Field... Type:50331648: Name:taxable: Value :yes: Listing siblings... Finished siblings... Popping up Reached Field... Type:33554432: Name:: Value :-1200: Listing siblings... Finished siblings... Popping up Finished siblings... Popping up Finished siblings... Popping up Finished siblings... Popping up
public static <return-type> <method-Name> (< 0 - N parameters>)where:
CREATE PROCEDURE myProc1( IN P1 INTEGER, OUT P2 INTEGER, INOUT P3 INTEGER ) RETURNS INTEGER LANGUAGE JAVA EXTERNAL NAME "com.ibm.broker.test.MyClass.myMethod1";You can use the following ESQL to invoke myProc1:
CALL myProc1( intVar1, intVar2, intVar3) INTO intReturnVar3; -- or SET intReturnVar3 = myProc1( intVar1, intVar2, intVar3);
CREATE PROCEDURE myProc2( IN P1 INTEGER, OUT P2 INTEGER, INOUT P3 INTEGER ) LANGUAGE JAVA EXTERNAL NAME "com.ibm.broker.test.MyClass.myMethod2";You must use the following ESQL to invoke myProc2:
CALL myProc2(intVar1, intVar2, intVar3);
package com.ibm.broker.test; class MyClass { public static Long myMethod1( Long P1, Long[] P2 Long[] P3) { ... } public static void myMethod2( Long P2, Long[] P2 Long[] P3) { ... } /* When either of these methods is called: P1 may or may not be NULL (depending on the value of intVar1). P2[0] is always NULL (whatever the value of intVar2). P3[0] may or may not be NULL (depending on the value of intVar3). This is the same as with current ESQL procedures. When these methods return: intVar1 is unchanged intVar2 may still be NULL or may have been changed intVar3 may contain the same value or may have been changed. This is the same as with current ESQL procedures. When myMethod1 returns: intReturnVar3 is either NULL (if the method returns NULL) or it contains the value returned by the method. */ }
Any non-static objects created inside the Java routine are marked for garbage collection when the Java routine returns to ESQL, or earlier depending upon the form of reference. However, if the Java class that contains the Java routine has static data members, these exist across multiple invocations of the routine as there is only one copy of these static data members for each execution group.
Therefore, if you intend to call a Java routine that changes the static data from more than one message flow at once, or from a message flow that has an additional instance, you need to make use of the synchronization primitives within Java, in order to ensure data integrity.
Note, however, that static data is lost if you redeploy.