Can we use a bind variable in oracle inside a procedure or function ?
I'm trying to update a bind variable inside my procedure. Can I do so in any case?
if (condition) then
:v_bind:=10;
end if;
Can I do the above thing inside a procedure or function..?
variable v_bind number;
create procedure abc as v_one
BEGIN
select count(a) into v_one from ab;
if(v_one<>0) then
:v_bind:=10;
end if;
Will I able to do this? It is showing me bad variable v_bind
To use bind variables in an Oracle SQL query, you use the colon character : to indicate a bind variable. You use : then the variable name. When the query is run by the application, a value needs to be provided to use in place of the user_id. To use a bind variable in SQL Server, you use the @ symbol before the variable name.
Bind variables make the code more secure and help avoid SQL injection security issues because user data is never treated as a part of the executable SQL statement. If user data is concatenated with a SQL statement, it will be vulnerable to a SQL injection attack:
Hard parsing each statement is quite a pain and severely impact the performance of the oracle database. -Bind Variables helps application to send exactly the same SQL to Oracle every time. Bind variable can highly improve performance by avoiding hard parsing.
If you use a bind variable multiple times in the query, you must repeat the bind values. Bind variables can be IN or OUT. The IN bind variables allow you to pass data from Python to Oracle Database while the OUT bind variables allow you to get data back from the Oracle Database to Python.
You can't create a procedure with a bind variable in it because stored procedures are server-side objects and bind variables only exist on the client side.
Suppose I'm using SQL*Plus, and that I've created some bind variables. Once I exit SQL*Plus, any bind variables I created don't exist any more. However, stored procedures have to persist in the database, and hence they can't have any reference to anything that was created and then destroyed on the client.
Here's an example showing that you can't 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'
You can, however, pass a bind variable as an OUT
parameter for a procedure. The procedure can then assign a value to the OUT
parameter, and this value will then be stored in your bind variable.
Suppose we have 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 a 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
No, you cannot do what you are asking. Bind variables in plsql are handled transparently. You do not explicitly code bind variables unless you are going to use 'execute immediate' to run the code outside of plsql like this:
declare
v_bind number := 1;
begin
execute immediate 'select * from table where x = :v_bind';
end;`
The following code uses bind variables as well, but it is handled transparently by plsql:
declare
v_bind number := 1
y number;
begin
select count(*) into y from table where x = v_bind;
end;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With