• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 185
  • Last Modified:

insert with cursor

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
shwelopo
Asked:
shwelopo
1 Solution
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
< 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
 
shwelopoAuthor Commented:
I need to do it using cursor to fetch the records for validation purposes.  Thank you.
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Define 'validation purposes'.  You've been given a faster way..
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
Naveen KumarProduction Manager / Application Support ManagerCommented:
do you need it in oracle or sql server ?
0
 
James ElliottCommented:
0
 
slightwv (䄆 Netminder) Commented:
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
 
slightwv (䄆 Netminder) Commented:
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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now