use of bind variable

后端 未结 3 1704
刺人心
刺人心 2021-01-12 12:24

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?

<         


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-12 12:57

    You can't bind a sqlplus variable in a session to a function/procedure. It will give you error of "Bad bind variable". You can actually just pass bind variable from your oracle session to any procedure.

    Let's see a example

        variable v1 NUMBER;
    
        begin
           select salary into :v1 from employees where employee_id = 100;
           dbms_output.put_line(:v1);
       end;
       /
    

    And if you run the above example by enclosing in procedure/function it will show you error.

       create or replace procedure proc is
       begin
          select salary into :v1 from employees where employee_id = 100;
          dbms_output.put_line(:v1);
       end;
       /
    

    Error -

    PROCEDURE proc compiled
    Warning: execution completed with warning
    3/20           PLS-00049: bad bind variable 'V1'
    4/22           PLS-00049: bad bind variable 'V1'
    

    Thus, it is not possible to use session-level bind variables in procedures/functions. In below example t2 is a bind variable

    create or replace procedure proc is
        t2 NUMBER;
        begin
           select salary into t2 from employees where employee_id = 100;
           dbms_output.put_line(t2);
        end;
        /
    

    You can call this procedure from sqlplus as

    exec proc;
    

提交回复
热议问题