Oracle DECLARE and EXECUTE

PowerBuilder requires a declarative statement to identify the database stored procedure that is being used and a logical name that can be referenced in subsequent SQL statements. The general syntax for declaring a procedure is:

DECLARE logical_procedure_name PROCEDURE FOR
	Oracle_procedure_name(:InParam1,:InParam2, ...)
	{USING transaction_object};

where logical_procedure_name can be any valid PowerScript data identifier and Oracle_procedure_name is the name of the stored procedure in the database.

The parameter references can take the form of any valid parameter string that Oracle accepts. PowerBuilder does not inspect the parameter list format except for purposes of variable substitution. The USING clause is required only if you are using a transaction object other than the default transaction object.

You can use Oracle Named or Positional notation to specify the procedure arguments. Positional is simpler to specify, but you must use Named if any output parameters are defined to the left of any input parameters.

Example 1

If a stored procedure is defined as:

CREATE PROCEDURE spm1
	(dept varchar2, mgr_name OUT varchar2) 
	IS lutype varchar2(10);
	BEGIN 
	SELECT manager INTO mgr_name FROM mgr_table
	WHERE dept_name = dept;
	END;

To declare that procedure for processing within PowerBuilder, you code:

DECLARE dept_proc PROCEDURE FOR
	spm1(:dept);

Note that this declaration is a non-executable statement, just like a cursor declaration. Where cursors have an OPEN statement, procedures have an EXECUTE statement.

When the EXECUTE statement executes, the procedure is invoked. The EXECUTE refers to the logical procedure name.

EXECUTE dept_proc;

Example 2

The following example that declares a function in a service object that reads a pipe shows the use of named notation:

public function integer f_GetId (string as_PipeName)
double ldbl_Id
DECLARE f_GetId PROCEDURE FOR 
	f_GetId (pipe_name => :as_PipeName) USING SQLCA;
EXECUTE f_GetId;
FETCH f_GetId INTO :ldbl_Id;
CLOSE f_GetId;
RETURN ldbl_Id;

Example 3

Given this procedure:

CREATE OR REPLACE PROCEDURE spu_edt_object(
o_id_object OUT NUMBER,
o_message OUT VARCHAR2,
a_id_object NUMBER,
a_param VARCHAR2 := NULL,
a_value VARCHAR2 := NULL
) as
begin
o_id_object := 12345;
o_message := 'Hello World';
end;

The DECLARE statement must use named notation because output parameters are defined to the left of input parameters:

dec{0} o_id_object, id_obiect = 54321
string o_message, param = 'Test'
DECLARE proc_update PROCEDURE FOR spu_edt_object (
a_id_object => :id_object,
a_param => :param
)
USING SQLCA;
EXECUTE proc_update;
if SQLCA.SqlCode 0 then
SQLCA.f_out_error()
RETURN -1
end if
FETCH proc_update INTO :o_id_object, o_message;
if SQLCA.SqlCode 0 then
SQLCA.f_out_error()

RETURN -1
end if