Home » SQL & PL/SQL » SQL & PL/SQL » How to use bind variables in cursors?
How to use bind variables in cursors? [message #606325] Wed, 22 January 2014 06:47 Go to next message
adityalsr89
Messages: 3
Registered: January 2014
Junior Member
Please help me how to use bind variables in a cursor. I have seen people helping out with parameterized cursor but is it possible to use bind variables in a cursor?

Thanks
Re: How to use bind variables in cursors? [message #606326 is a reply to message #606325] Wed, 22 January 2014 06:53 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
If the cursor is in PL/SQL then you don't need to do anything. All PL/SQL variables / parameters used in sql are automatically treated as bind variables.
Re: How to use bind variables in cursors? [message #606330 is a reply to message #606326] Wed, 22 January 2014 07:30 Go to previous messageGo to next message
dariyoosh
Messages: 538
Registered: March 2009
Location: France
Senior Member
cookiemonster wrote on Wed, 22 January 2014 13:53
If the cursor is in PL/SQL then you don't need to do anything. All PL/SQL variables / parameters used in sql are automatically treated as bind variables.

So long as we're not talking about dynamic SQL in a plsql code where bind variables are to be specified explicitly.
Re: How to use bind variables in cursors? [message #606332 is a reply to message #606325] Wed, 22 January 2014 08:00 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

PL/SQL User's Guide and Reference
Application Developer's Guide - Fundamentals

Re: How to use bind variables in cursors? [message #606365 is a reply to message #606326] Thu, 23 January 2014 00:38 Go to previous messageGo to next message
adityalsr89
Messages: 3
Registered: January 2014
Junior Member
Thanks for the reply.

Can u please tell me in the below example is variable 'i' bound?

declare
type rc is ref cursor;
l_rc rc;
l_dummy all_objects.object_name%type;
l_start number default dbms_utility.get_time;
begin
for i in 1 .. 1000
loop
open l_rc for
'select object_name
from all_objects
where object_id = ' || i;
fetch l_rc into l_dummy;
close l_rc;
-- dbms_output.put_line(l_dummy);
end loop;
dbms_output.put_line
(round((dbms_utility.get_time-l_start)/100, 2) ||
' Seconds...' );
end;
Re: How to use bind variables in cursors? [message #606366 is a reply to message #606365] Thu, 23 January 2014 00:44 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

No it is not.
The correct correct should be (not the format done using How to use [code] tags and make your code easier to read:
declare
      type rc is ref cursor;
      l_rc rc;
      l_dummy all_objects.object_name%type;
      l_start number default dbms_utility.get_time;
  begin
      for i in 1 .. 1000
      loop
          open l_rc for
          'select object_name
             from all_objects
            where object_id = :1'
            using i;
          fetch l_rc into l_dummy;
          close l_rc;
          -- dbms_output.put_line(l_dummy);
      end loop;
      dbms_output.put_line
       (round((dbms_utility.get_time-l_start)/100, 2) ||
        ' Seconds...' );
  end;
/

See the difference in execution times.
Yours:
SQL> declare
  2  type rc is ref cursor;
  3  l_rc rc;
  4  l_dummy all_objects.object_name%type;
  5  l_start number default dbms_utility.get_time;
  6  begin
  7  for i in 1 .. 1000
  8  loop
  9  open l_rc for
 10  'select object_name
 11  from all_objects
 12  where object_id = ' || i;
 13  fetch l_rc into l_dummy;
 14  close l_rc;
 15  -- dbms_output.put_line(l_dummy);
 16  end loop;
 17  dbms_output.put_line
 18  (round((dbms_utility.get_time-l_start)/100, 2) ||
 19  ' Seconds...' );
 20  end;
 21  /
31.25 Seconds...

PL/SQL procedure successfully completed.

Mine:
SQL> declare
  2        type rc is ref cursor;
  3        l_rc rc;
  4        l_dummy all_objects.object_name%type;
  5        l_start number default dbms_utility.get_time;
  6    begin
  7        for i in 1 .. 1000
  8        loop
  9            open l_rc for
 10            'select object_name
 11               from all_objects
 12              where object_id = :1'
 13              using i;
 14            fetch l_rc into l_dummy;
 15            close l_rc;
 16            -- dbms_output.put_line(l_dummy);
 17        end loop;
 18        dbms_output.put_line
 19         (round((dbms_utility.get_time-l_start)/100, 2) ||
 20          ' Seconds...' );
 21    end;
 22  /
.61 Seconds...

PL/SQL procedure successfully completed.


[Updated on: Thu, 23 January 2014 00:45]

Report message to a moderator

Re: How to use bind variables in cursors? [message #606367 is a reply to message #606365] Thu, 23 January 2014 00:47 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Please read:
PL/SQL User's Guide and Reference
Application Developer's Guide - Fundamentals

You think you can learn PL/SQL without them, you can't.

Re: How to use bind variables in cursors? [message #606368 is a reply to message #606366] Thu, 23 January 2014 01:15 Go to previous messageGo to next message
adityalsr89
Messages: 3
Registered: January 2014
Junior Member
thanks for the reply.

But as you have mentioned in above posts, All PL/SQL variables / parameters used in sql are automatically treated as bind variables.

Isn't variable 'i' considered as a pl/sql variable in the above piece of code?
Re: How to use bind variables in cursors? [message #606369 is a reply to message #606368] Thu, 23 January 2014 01:28 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

No as you do not use it as a variable in the statement but you concatenate ITS VALUE to it, so you have not ONE statement but 100.
Please read the book, you lack the minimum knowledge to take profit of forum answers.


Re: How to use bind variables in cursors? [message #606377 is a reply to message #606368] Thu, 23 January 2014 02:31 Go to previous message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
adityalsr89 wrote on Thu, 23 January 2014 07:15


But as you have mentioned in above posts, All PL/SQL variables / parameters used in sql are automatically treated as bind variables.


dariyoosh already pointed out that doesn't apply to dynamic sql.
Previous Topic: Concatenate issue
Next Topic: PL/SQL statement
Goto Forum:
  


Current Time: Thu Apr 25 07:19:54 CDT 2024