Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use of bind variable

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

like image 948
jasmeet Avatar asked Mar 06 '11 10:03

jasmeet


People also ask

How do you use a bind variable in a query?

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.

What are the benefits of using @bind variables?

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:

What is-bind variables in Oracle?

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.

What happens if you use a bind variable multiple times?

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.


2 Answers

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
like image 99
Luke Woodward Avatar answered Sep 21 '22 06:09

Luke Woodward


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;
like image 32
CoderSteve Avatar answered Sep 23 '22 06:09

CoderSteve