Free Newsletters:
Database Journal  
DBAnews  

DBASupport

 The Knowledge Center for Oracle Professionals

Search DBAsupport:
 
HOME 11g Central 10g Central 9i Central 8i Central Oracle News Scripts FAQ OCP Zone Resources Technical Docs Tools & Utilities Forums
internet.com

» HOME
» FEATURES
    11g Central
    10g Central
    9i Central
    8i Central
    Oracle News
» COMMUNITY
    Scripts
    Forums
    FAQ
    OCP Zone
» RESOURCES
    Resources
    Technical Docs
    Tools & Utilities
    Tech Jobs


internet.commerce
Be a Commerce Partner
Find Project Software
Logo Design
Imprinted Promotions
Shop Online
Corporate Awards
Hurricane Shutters
Logo Design
Online Shopping
Server Racks
Computer Deals


internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


   DBAsupport.com > Oracle > Oracle 9i Central > Featured Stories


Solaris 8 Migration Assistant
Rapidly move your Solaris 8 application environments to new systems running Solaris 10 with the Solaris 8 Migration Assistant. Reduce migration risk while taking advantage of increased performance, reliability and security of the latest SPARC hardware platforms and Solaris 10 OS. »

 
Sun Partner Survey
Please take a quick 3 minute survey from Sun. Click here to take the survey. Your feedback will help Sun improve on their solution center and be more useful for you and your peers. Thank you for your time and consideration. »

 
Sun Eco Innovation: Power Calculators
Power consumption has increasingly become a priority in customer's minds when purchasing new systems or storage. Sun's Power Calculators provide data on power consumption of Sun products allowing IT managers to better plan the power requirements in the datacenter to achieve better energy and cost savings. »

 
Optimize the Web Tier: Consolidate to Get More Performance in Less Space and Lower Power Consumption
Expansion in the Web tier is generally accomplished by adding more servers whenever extra capacity is needed. As the pool of servers grows larger, however, the complexity of the environment can grow exponentially. »


Q&A with Lutz Ziob, GM of Microsoft Learning. Learn how IT professionals can become "certified heroes" within their enterprises by getting trained and certified in Windows Server 2008.

Sr. Oracle EBS Developer
Professional Technical Resources
US-OR-Forest Grove

Justtechjobs.com Post A Job | Post A Resume

Bulk Binds and Collects in PL/SQL - Part - 1
Amar Kumar Padhi, amar_padhi@hotmail.com




Bulk Binds (Writing data in bulk)



Bulk binds improve performance of DML statements by minimizing the number of switches between the PL/SQL and SQL engines.



You may have a piece of code, which has multiple update, delete or insert statements on the same table. This results in multiple calls to the SQL engine for carrying out the transaction. By using bulk binds, you can carry out mass scale data manipulation at one shot. The altered data has to be stored in a PL/SQL collection in the code.



The FORALL statement is used for doing the bulk-processing job at one go. This statement is similar to the FOR-LOOP statement except that LOOP/END LOOP key words are not used. The FORALL statement needs a range to work on, along with whatever DML activity is to be carried.



Syntax:

  forall <counter> in <range begin> .. <range end>
    update/insert/delete

Below is a simple example without Bulk Binds. Notice the number of times the update is performed. Every SQL statement present in the PL/SQL code results in a call to the SQL engine for processing.

create or replace procedure updsal is
   cursor cr_emp is
     select empno, job, sal
     from   amemp
     where  job in ('MANAGER', 'PRESIDENT', 'DBA');
begin
   for rec in cr_emp loop
     ... some checks on the employee
     ...
     if rec.job = 'MANAGER' then
        update amemp
        set    sal = sal * 1.1
        where  empno = rec.empno;
     else
        update amemp
        set    sal = sal * 1.2
        where  empno = rec.empno;
     end if;
   end loop;
end;

The above code is changed as shown below, using the bulk binding option. Notice the single update call to the backend.

create or replace procedure updsal is
   cursor cr_emp is
     select empno, job, sal
     from   amemp
     where  job in ('MANAGER', 'PRESIDENT', 'DBA');

   type amemp_tab1 is table of amemp.empno%type
        index by binary_integer;

   type amemp_tab2 is table of amemp.sal%type
        index by binary_integer;

   empnum amemp_tab1;
   empsal amemp_tab2;
   cnt   number := 0;
begin
   for rec in cr_emp loop
     ... some checks on the employee
     ...

     cnt := cnt + 1;
     empnum(cnt) := rec.empno;

     if rec.job = 'MANAGER' then
        empsal(cnt) := rec.sal * 1.1;
     else
        empsal(cnt) := rec.sal * 1.2;
     end if;
   end loop;

   forall i in 1 .. cnt
     update amemp
     set    sal = empsal(i)
     where  empno = empnum(i);
end;
/

Below is an example that uses both bulk binding and collection together.

declare
  type emp_t is table of amemp.empno%type;
  lst  emp_t;
begin
  select empno bulk collect 
  into   lst
  from   amemp
  where  sal < 2000;

  ..some checks here...

  forall i in lst.first .. lst.last
    update amemp
    set    sal = sal * 1.1
    where  empno = lst(i);
end;
/

Bulk collects updated value return

You can even use bulk collects to return a value to the calling procedure using the RETURNING clause, without any additional fetch. Below is an example that updates the salary as and stores the updated information in a collection for further processing.

declare
  type no_t  is table of amemp.empno%type;
  type sal_t is table of amemp.sal%type;
  lno  no_t;
  lsal sal_t;

begin
  select empno bulk collect
  into   lno
  from   amemp
  where  job = 'DBA';
  ..
  ..
  forall i in lno.first .. lno.last
    update amemp
    set    sal =  sal * 1.1
    where  empno = lno(i)
    returning sal bulk collect into lsal;
 ..
  for i in lno.first .. lno.last loop
    dbms_output.put_line(lno(i) || '/' || lsal(i));
  end loop;
end;
/

In the next installment, we will cover the above features with more collection examples and performance issues that may be encountered in heavy processing.

Previous   Next


Back to DBAsupport.com







JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
IBM eBook: Planning a Service Oriented Architecture
IBM eBook: Choosing the Right Architecture--What It Means for You and Your Business
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Avaya Article: Using Intelligent Presence to Create Smarter Business Applications
Intel Go Parallel Article: Getting Started with TBB on Windows
Microsoft Article: 7.0, Microsoft's Lucky Version?
Avaya Article: How to Feed Data into the Avaya Event Processor
IBM Article: Developing a Software Policy for Your Organization
Microsoft Article: Managing Virtual Machines with Microsoft System Center
Intel Go Parallel Article: Intel Threading Tools and OpenMP
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
HP Video: StorageWorks EVA4400 and Oracle
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Red Gate Download: SQL Toolbelt and free High-Performance SQL Code eBook
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
Silverlight 2 App and Walkthrough: Leverage Silverlight 2 with SQL Server and XML
IBM Article: Enterprise Search--Do You Know What's Out There?
HP Demo: StorageWorks EVA4400
Microsoft Article: The Progress and Promise of Deep Zoom
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES