You cannot create a procedure with a bind variable because stored procedures are objects on the server side and bind variables exist only on the client side.
Suppose I am using SQL * Plus, and that I have created some bind variables. As soon as I exit SQL * Plus, any bind variables that I create no longer exist. However, stored procedures must be stored in a database and, therefore, they cannot refer to anything that was created and then destroyed on the client.
Here is an example showing that you cannot create a procedure that references a bind variable:
SQL> variable i number
SQL> exec: i: = 0;
PL / SQL procedure successfully completed.
SQL> print: i
I
----------
0
SQL> create or replace procedure test_proc
2 as
3 begin
4: i: = 9;
5 end;
6 /
Warning: Procedure created with compilation errors.
SQL> show errors procedure test_proc;
Errors for PROCEDURE TEST_PROC:
LINE / COL ERROR
-------- ------------------------------------------ -----------------------
4/3 PLS-00049: bad bind variable 'I'
However, you can pass the bind variable as an OUT parameter to the procedure. The procedure can then assign a value to the OUT parameter, and that value will then be stored in your binding variable.
Suppose we perform the following procedure:
CREATE OR REPLACE PROCEDURE do_stuff ( p_output OUT INTEGER ) AS BEGIN p_output := 6; END;
We can use this to set the bind variable as follows:
SQL> variable i number
SQL> exec: i: = 0;
PL / SQL procedure successfully completed.
SQL> print: i
I
----------
0
SQL> exec do_stuff (: i);
PL / SQL procedure successfully completed.
SQL> print: i
I
----------
6
Luke woodward
source share