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:
1 |
DECLARE <span>logical_procedure_name</span> PROCEDURE FOR |
1 |
<span>Oracle_procedure_name</span>(:In<span>Param1</span>,:In<span>Param2</span>, ...) |
1 |
{USING <span>transaction_object</span>}; |
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:
1 |
CREATE PROCEDURE spm1<br> (dept varchar2, mgr_name OUT varchar2) <br> IS lutype varchar2(10);<br> BEGIN <br> SELECT manager INTO mgr_name FROM mgr_table<br> WHERE dept_name = dept;<br> END; |
To declare that procedure for processing within PowerBuilder,
you code:
1 |
DECLARE dept_proc PROCEDURE FOR<br> 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.
1 |
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:
1 |
public function integer f_GetId (string as_PipeName)<br>double ldbl_Id |
1 |
DECLARE f_GetId PROCEDURE FOR <br> f_GetId (pipe_name => :as_PipeName) USING SQLCA; |
1 |
EXECUTE f_GetId; |
1 |
FETCH f_GetId INTO :ldbl_Id; |
1 |
CLOSE f_GetId; |
1 |
RETURN ldbl_Id; |
Example 3
Given this procedure:
1 |
CREATE OR REPLACE PROCEDURE spu_edt_object(<br>o_id_object OUT NUMBER,<br>o_message OUT VARCHAR2,<br>a_id_object NUMBER,<br>a_param VARCHAR2 := NULL,<br>a_value VARCHAR2 := NULL<br>) as<br>begin<br>o_id_object := 12345;<br>o_message := 'Hello World';<br>end; |
The DECLARE statement must use named notation because output
parameters are defined to the left of input parameters:
1 |
dec{0} o_id_object, id_obiect = 54321<br>string o_message, param = 'Test' |
1 |
DECLARE proc_update PROCEDURE FOR spu_edt_object (<br>a_id_object => :id_object,<br>a_param => :param<br>)<br>USING SQLCA; |
1 |
EXECUTE proc_update;<br>if SQLCA.SqlCode 0 then<br>SQLCA.f_out_error()<br>RETURN -1<br>end if |
1 |
FETCH proc_update INTO :o_id_object, o_message;<br>if SQLCA.SqlCode 0 then<br>SQLCA.f_out_error()<br> |
1 |
RETURN -1<br>end if |