?
Solved

insert with cursor

Posted on 2015-01-29
7
Medium Priority
?
146 Views
Last Modified: 2015-02-02
I'd like to write an sql sp script that will fetch records from a table and insert into another table if it doesn't exist.  Pls help.  thank you.
0
Comment
Question by:shwelopo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 2000 total points
ID: 40578979
< Air code.  I do my own stunts too. >  This can be performed without a cursor, avoiding all the cursor overhead caused by performing actions Row By Agonizing Row (RBAR)

CREATE PROC p_some_proc() AS

INSERT INTO TargetTable (id, goo, foo, boo) 
SELECT id, goo, foo, boo
FROM SourceTable s
  --  LEFT means all rows from source table will be added... 
   LEFT JOIN TargetTable t ON s.id = t.id
-- WHERE the target.pk is null means only source rows that aren't already in target
WHERE t.id IS NULL
GO

Open in new window

0
 

Author Comment

by:shwelopo
ID: 40579007
I need to do it using cursor to fetch the records for validation purposes.  Thank you.
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40579014
Define 'validation purposes'.  You've been given a faster way..
0
Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 40579272
do you need it in oracle or sql server ?
0
 
LVL 12

Expert Comment

by:James Elliott
ID: 40579463
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40579617
In Oracle it would be something like this:

create or replace procedure myproc
     v_junk varchar2(10);
begin
    for I in (select col1, col2, col3 from some_table) loop
        --do some 'validations'...
        if i.col1 = 0 then
               v_junk := 'Got zero';
        else
               v_junk := 'Is OK';
        end if;

        insert into some_other_table(col1, col2, col3, col4) values(i.col1, i.col2, i.col3, v_junk);

   end loop;
end;
/

Since you mention: Doesn't exist.

If that is the ONLY validation necessary, look at the MERGE command.

It will do it all for you in a single SQL statement.

Again, only Oracle.
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40579625
Complete test case using Oracle's MERGE:
drop table tab1 purge;
drop table tab2 purge;

create table tab1( col1 char(1), col2 char(1));
create table tab2( col1 char(1), col2 char(1));

insert into tab1 values('a','1');
insert into tab1 values('b','2');

insert into tab2 values('a','1');
commit;


create or replace procedure myproc
is
begin
	merge into tab2 t2 using
		(select col1, col2 from tab1) t1
	 	on (t1.col1=t2.col1 and t1.col2=t2.col2)
	when not matched then insert(t2.col1,t2.col2) values(t1.col1,t1.col2);
end;
/

show errors

exec myproc

Open in new window

0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.
Suggested Courses

777 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