Solved

ORACLE - PL/SQL traverse a set of data, to insert the data into another table

Posted on 2011-02-14
5
637 Views
Last Modified: 2012-05-11
Good afternoon, I have a PRODUCTS table in oracle with more than 10000 records. In a new design this table has been divided into 6 tables.
What I need is to get the data from the PRODUCTS table and insert new records in these 5 tables.
 I understand that I do a select on this table with some filters in the where, my question is, as it runs a data set in oracle to insert each row in the corresponding table
0
Comment
Question by:enrique_aeo
  • 2
  • 2
5 Comments
 
LVL 76

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 166 total points
ID: 34891822
10,000 isn't a lot of data.  Can you do a simple: insert into new_table1 (select columns from old_table);

for each of the new tables?
0
 

Author Comment

by:enrique_aeo
ID: 34891904
hahaha, I sincerely hope that is so, you are awesome, I look a little more the subject and I confirm. Thanks
0
 
LVL 34

Accepted Solution

by:
Mark Geerlings earned 334 total points
ID: 34891916
You could use either one (or multiple)  PL\SQL procedures (or anonymous PL\SQL blocks) for this task, or you could (more efficiently) simply use separate SQL insert commands for each of these new tables.

The SQL insert statements could look like this:

insert into new_table1
select * from old_table
where [conditions for new_table1]

insert into new_table2
select * from old_table
where [conditions for new_table2]

etc.

If you are concered with performance, you can make those inserts significantly faster by first setting each of the new tables to "nologging" like this:
alter table [new_table1] nologging;
alter table [new_table2] nologging;
etc.

Then change your insert statements to look like this:

insert /*+APPEND */ into new_table1
select * from old_table
where [conditions for new_table1]

insert /*+APPEND */ into new_table2
select * from old_table
where [conditions for new_table2]

If you use the "nologging" option, you will want to take a backup of the new tables after you load them.
0
 

Author Comment

by:enrique_aeo
ID: 34891947
ok, I know why I do it row by row, for example in the table is a column coduser products, but in the new table name to be written, in order to do this I have a function in which you step and I returned coduser name
0
 
LVL 34

Assisted Solution

by:Mark Geerlings
Mark Geerlings earned 334 total points
ID: 34892100
OK, but you could call that function either inside a PL\SQL procedure (or anonymouse block) as you process each row, or the SQL insert statements could call this function for each row as well.  The syntax for that would look more like this:

insert into new_table1
 (column1, column2, column3, etc.)
select column1, column2, function (parameters...), etc.

Remember, whatever can be done with SQL statements wil be more efficient than using those same SQL statements inside PL\SQL.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now