Free Newsletters:
DatabaseJournal  
DBANews
Database Journal
Search Database Journal:
 
HOME News MS SQL Oracle DB2 Access MySQL PostgreSQL PHP SQL Etc Scripts Links Discussion
internet.com

» HOME
» NEWS
» FEATURES
» SERIES
MS SQL
Oracle
MS Access
MySQL
DB2
» RESOURCES
Products
Scripts
Links
» DISCUSSION
» TECH JOBS

Marketplace Partners
Be a Marketplace Partner




internet.commerce
Be a Commerce Partner
Condos For Sale
Promotional Gifts
Build a Server Rack
Cell Phones
Phone Cards
Holiday Gift Ideas
Online Education
Compare Prices
Computer Hardware
Online Universities
Laptops
Logo Design
Compare Prices
Televisions




MySpace Joins eBay, Yahoo in Open Profile Push

News Corp. Unit Under Fire for Ties to Hacker

Are Non-PC Devices Hurting 'Net Innovation?

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

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


Linked Data Planet Conference & Expo

CA ERwin® Data Modeler Proven database design and modeling. Efficiently analyze, design and deploy effective database solutions. Whitepaper: Manage SQL Server Deployments
Try it free: CA ERwin® Data Modeler


Guide to Oracle 11g and Database Migration
Oracle Database 11g includes more features for self-management and automation, which makes it easier for customers to cost-effectively manage their data. Download this Internet.com eBook for an overview of some of the new features in 11g and for an overview of the issues you need to consider as you prepare for a database migration. »
Innovate Faster with Oracle Database 11g
Read this in-depth analysis of 56 customers, which shows significant differences between the value software vendors Oracle and SAP deliver to midsize companies. »
Oracle Business Intelligence Standard Edition One
Find out how Newport Beach, CA-based Mobilitie is shaking up the telecom industry by leveraging technology to provide an entirely different financial model for deploying, upgrading, and owning wireless and wireline network assets. »
Business Intelligence and Enterprise Performance Management: Trends for Emerging Businesses
Quickly implementing an ERP software solution can be of tremendous benefit; however, companies often struggle to balance the benefits of reducing implementation time and cost with the risks of an accelerated deployment. Read this white paper to learn about easy-to-follow best practices for achieving a successful accelerated implementation. »
Making the Case for Oracle Database on Windows
Users benefit as vendors reduce enterprise complexity and deliver integration. »

Production Manager (hands on)
Aquent
US-MA-Cambridge

Justtechjobs.com Post A Job | Post A Resume
Oracle
May 7, 2008
Multi Table Loop

by JP Vijaykumar

This article examines how to loop through multiple tables to process the data.

drop table temp_jp10;
create table temp_jp10(col1 number,col2 varchar2(20));
insert into temp_jp10 values(2,'RAMA');
insert into temp_jp10 values(4,'SITA');
insert into temp_jp10 values(6,'HANUMAN');
 
drop table temp_jp01;
create table temp_jp01(col1 number,col2 varchar2(20));
insert into temp_jp01 values(1,'RADHA');
insert into temp_jp01 values(2,'RADHA');
insert into temp_jp01 values(3,'RADHA');
insert into temp_jp01 values(4,'RADHA');
insert into temp_jp01 values(5,'RADHA');
 
drop table temp_jp02;
create table temp_jp02(col1 number,col2 varchar2(20));
insert into temp_jp02 values(1,'KRISHNA');
insert into temp_jp02 values(2,'KRISHNA');
insert into temp_jp02 values(3,'KRISHNA');
insert into temp_jp02 values(4,'KRISHNA');
insert into temp_jp02 values(5,'KRISHNA');
 
drop table temp_jp03;
create table temp_jp03(col1 number,col2 varchar2(20));
insert into temp_jp03 values(1,'SATYA');
insert into temp_jp03 values(2,'SATYA');
insert into temp_jp03 values(3,'SATYA');
insert into temp_jp03 values(4,'SATYA');
insert into temp_jp03 values(5,'SATYA');
commit;
 
 
select * from temp_jp10;
 
       COL1 COL2
---------- --------------------
         2 RAMA
         4 SITA
         6 HANUMAN
 
select * from temp_jp01;
 
       COL1 COL2
---------- --------------------
         1 RADHA
         2 RADHA
         3 RADHA
         4 RADHA
         5 RADHA
 
select * from temp_jp02;
 
       COL1 COL2
---------- ------------------
         1 KRISHNA
         2 KRISHNA
         3 KRISHNA
         4 KRISHNA
         5 KRISHNA
 
select * from temp_jp03;
 
       CO1 COL2
---------- -------------
         1 SATYA
         2 SATYA
         3 SATYA
         4 SATYA
         5 SATYA

Now I want to loop through the records of temp_jp10 table and if I find any matching records in the tables temp_jp01, temp_jp02,temp_jp03, then update col2 and if there are no matching records then insert the record.

I created the following procedure: I am specifically not using the merge command to test my multi table loop script and compare the difference.

declare
v_num number(10);
begin
for c1 in (select * from temp_jp10) loop
 
execute immediate 'select count(*) from temp_jp01 where col1 = '||c1.col1 into v_num;
if (v_num > 0) then
update temp_jp01 set col2 = c1.col2 where col1 = c1.col1;
else
insert into temp_jp01 values(c1.col1,c1.col2);
end if;
 
execute immediate 'select count(*) from temp_jp02 where col1 = '||c1.col1 into v_num;
if (v_num > 0) then
update temp_jp02 set col2 = c1.col2 where col1 = c1.col1;
else
insert into temp_jp02 values(c1.col1,c1.col2);
end if;
 
execute immediate 'select count(*) from temp_jp03 where col1 = '||c1.col1 into v_num;
if (v_num > 0) then
update temp_jp03 set col2 = c1.col2 where col1 = c1.col1;
else
insert into temp_jp03 values(c1.col1,c1.col2);
end if;
 
end loop;
commit;
end;
 
PL/SQL procedure successfully completed
 
select * from temp_jp01;
 
      COL1 COL2
---------- --------------------
         1 RADHA
         2 RAMA
         3 RADHA
         4 SITA
         5 RADHA
         6 HANUMAN
 
 
select * from temp_jp02;
 
      COL1 COL2
---------- --------------------
         1 KRISHNA
         2 RAMA
         3 KRISHNA
         4 SITA
         5 KRISHNA
         6 HANUMAN
 
 
select * from temp_jp03;
 
      COL1 COL2
---------- --------------------
         1 SATYA
         2 RAMA
         3 SATYA
         4 SITA
         5 SATYA
         6 HANUMAN

What if I had 100 tables to loop through to find matching records with my temp_jp10 table and update, if not insert. Imagine how my procedure will look.

select * from temp_jp10;
 
      COL1 COL2
---------- --------------------
         2 RAMA
         4 SITA
         6 HANUMAN
 
select * from temp_jp01;
 
      COL1 COL2
---------- --------------------
         1 RADHA
         2 RADHA
         3 RADHA
         4 RADHA
         5 RADHA
 
select * from temp_jp02;
 
      COL1 COL2
---------- --------------------
         1 KRISHNA
         2 KRISHNA
         3 KRISHNA
         4 KRISHNA
         5 KRISHNA
 
select * from temp_jp03;
 
      COL1 COL2
---------- --------------------
         1 SATYA
         2 SATYA
         3 SATYA
         4 SATYA
         5 SATYA

I want to create script to loop through all these tables in one go:

The following script does the required trick. I find the tables from a list of given tables and create a view inside my procedure and then perform the necessary dml operation on my view, which in turn updates the base tables.

declare
v_num number(10);
begin
for c1 in (select * from gzbgqt.temp_jp10) loop
 
for c2 in (select table_name from all_tables
           where owner= 'GZBGQT' 
                   and table_name in ('TEMP_JP01','TEMP_JP02','TEMP_JP03')) loop
 
execute immediate 
'create or replace view gzbgqt.temp_jp_vw as select * from gzbgqt.'||c2.table_name;
 
execute immediate 
'select count(*) from gzbgqt.temp_jp_vw where col1 ='||c1.col1 into v_num;
 
if (v_num > 0) then
update gzbgqt.temp_jp_vw set col2 = c1.col2  where col1 = c1.col1;
else
insert into gzbgqt.temp_jp_vw values(c1.col1,c1.col2);
end if;
 
end loop;
 
end loop;
commit;
end;

Once my multi table loop is executed the tables temp_jp01, temp_jp02 and temp_jp03 are updated as expected.

select * from temp_jp01;
 
      COL1 COL2
---------- --------------------
         1 RADHA
         2 RAMA
         3 RADHA
         4 SITA
         5 RADHA
         6 HANUMAN
 
 
select * from temp_jp02;
 
      COL1 COL2
---------- --------------------
         1 KRISHNA
         2 RAMA
         3 KRISHNA
         4 SITA
         5 KRISHNA
         6 HANUMAN
 
 
select * from temp_jp03;
 
      COL1 COL2
---------- --------------------
         1 SATYA
         2 RAMA
         3 SATYA
         4 SITA
         5 SATYA
         6 HANUMAN

Instead of looping through each and every table in the list, select the table from all_tables view and dynamically create a view based on the table and update the base table  through the view.

This simplifies my coding to write the if then logic for all of my tables.

HAPPY SCRIPTING

Tools:
Add databasejournal.com to your favorites
Add databasejournal.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed

Oracle Archives

Download: SQL Backup & DBA Best Practices eBook.
Download: SQL Backup & DBA Best Practices eBook
Quest Whitepaper: Improving Oracle Database Performance Using Real-Time Visual Diagnostics
HP eBook: Using Business Service Management (BSM) to Manage Your Business Applications
Data Sheet: IBM Information Server Blade


Latest Forum Threads
Oracle Forum
Topic By Replies Updated
GET DATA FROM .DBF FILE, ORACLE 9i revelation 5 May 5th, 10:55 AM
Could not locate Java runtime. Oracle installation error revelation 0 April 10th, 12:06 AM
Database Backup junOOni 4 March 20th, 06:28 AM
Helpme to How to Write Text File intelram_18 1 March 17th, 02:54 PM







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
Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server ‘08
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
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
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
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
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES