Solved

insert data into multiple tables using single stored procedure

Posted on 2009-05-18
7
2,137 Views
Last Modified: 2013-12-11
Hi All,
  I need to build a stored procedure to insert data into different tables of a database.
  Here each table is different from other in the concept of number of columns and data types.
Here i want to  put different insertion statements in one single stored procedure.
so just by executing this stored procedure once, i want to  insert data into one table based on the given table_name as input parameter to the store procedure.

Is this possible to develop a stored procedure like this?.
If possible ,Pls give me the syntax of the SP.
Thanks in Advance

0
Comment
Question by:gpinfotech
  • 2
  • 2
  • 2
7 Comments
 
LVL 3

Accepted Solution

by:
jakemdrew earned 334 total points
ID: 24418665
Just seperate each individual insert statment with a semi colon.  Does not matter how many or what they look like.

INSERT INTO CUSTOMERS VALUES ('230','Andrews','Rebecca','F','12-MAR-69','19-FEB-95','A',
'1456 Sycamore St','Falls Church','VA','22040','7038661521','7053567712','Y','','','');                        
INSERT INTO CUSTOMERS VALUES ('326','Milton','Henry','M','21-SEP-53','12-MAY-95','A',
'1723 Marshall Lane','Arlington','VA','22019','3019522214','7037213725','Y','Y','','');                            
INSERT INTO CUSTOMERS VALUES ('327','Jackson','Cindy','F','31-OCT-43','30-AUG-94','A',
'123 Courthouse Road','Fairfax','VA','22102','7034454454','7038547854','Y','','','Y');                            
INSERT INTO CUSTOMERS VALUES ('329','Jeffrey','Denning','M','15-MAR-64','19-FEB-96','A',
'1721 Whitney Drive','Falls Church','VA','22040','7036547896','7055435712','Y','','','');                      
INSERT INTO CUSTOMERS VALUES ('330','Mayer','Arthur','M','11-JUN-73','12-MAY-96','A',
'729 Castle Ct','Reston','VA','22019','7038246545','7037213324','','','Y','Y');                                    
INSERT INTO CUSTOMERS VALUES ('331','Baker','Paul','M','19-DEC-69','30-SEP-95','A',
'1423 North 15th Rd','Arlington','MD','22102','7034257894','7038542154','','Y','','');                              
INSERT INTO CUSTOMERS VALUES ('332','Johnson','Michelle','F','21-MAR-66','20-APR-95','A',
'123 North Main Street Apt. 309','Falls Church','MD','20129','7035643215','7038659856','','','Y','');          
COMMIT;
0
 
LVL 3

Assisted Solution

by:jakemdrew
jakemdrew earned 334 total points
ID: 24418673
You can also insert multiple rows into multiple tables. For example, if you wanted to insert into both the suppliers and customers table, you could run the following SQL statement:

INSERT ALL
   INTO suppliers (supplier_id, supplier_name) VALUES (1000, 'IBM')
   INTO suppliers (supplier_id, supplier_name) VALUES (2000, 'Microsoft')
   INTO customers (customer_id, customer_name, city) VALUES (999999, 'Anderson Construction', 'New York')
SELECT * FROM dual;

This example will insert 2 rows into the suppliers table and 1 row into the customers table.
0
 
LVL 22

Expert Comment

by:Ivo Stoykov
ID: 24418732
Hi,
Yes, it is possible, but I doubt one could give you a sample without knowing the loginc, conditions and so on.
Here is a good starting point -> http://download.oracle.com/docs/html/A95261_01/jdgtut1.htm
HTH
Ivo Stoykov
0
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.

 

Author Comment

by:gpinfotech
ID: 24418740
But i need to pass each row as input parameter.
How to pass values as input parameters to different input statements for different tables.

insert into table1(....)values(val1,val2)

insert into table2(-,-) values(valx,valy)
 
0
 

Author Comment

by:gpinfotech
ID: 24418778
Hello ivostoykov,

 create procedure pname
 as
begin

  inseret into table1(-,-) values(-,-)

  insert into table2(-,-) values(-,-)

  insert into table3(-,-) values(-,-)
------lke this stored procedures contains different insertion statements.
by executing SP i want to run only one insert statement out of many.And i want to pass values as input parameters.So based on the input parameters ,particular input statement should execute.
0
 
LVL 22

Assisted Solution

by:Ivo Stoykov
Ivo Stoykov earned 166 total points
ID: 24419452
you could just pass your query to SP and in there execute it and then do your inserts accordingly

HTH
Ivo Stoykov

create or replace procedure _test(q in Varchar2) is
 

r  your_table%ROWTYPE;
 

begin

OPEN p_cursor FOR q

    USING ...; -- params here

 LOOP

    FETCH p_cursor INTO r;

    EXIT WHEN p_cursor%NOTFOUND;

    .

    if ... then

      inseret into table1(-,-) values(-,-);

    end if;

    .

    .

 END LOOP;

 CLOSE p_cursor;

Open in new window

0

Featured Post

[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

912 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