Solved

Insert into table only where record does not already exists, and source table is the same as destination table

Posted on 2003-10-31
5
1,091 Views
Last Modified: 2007-12-19
Hi I'm trying to insert data into to a table, by "copying" most of the data from the same table
This script works only if the record does not already exist.

INSERT INTO BOM (PART_NO, ISSUE, REV, C_TYPE, COMP_PART_NO, POS)
SELECT '195N2106', ISSUE, REV, C_TYPE, COMP_PART_NO, POS FROM BOM  -- The new PART_NO to be created
WHERE PART_NO = '195N2131'                                                                   -- The old PART_NO to copy from
AND ISSUE = 6
AND REV = 3

If I have to create a new PART_NO in this BOM table, and I know that it should contain the same components as the "sum" of 2 other PART_NO's (all from one, plus all from a second that differs from the first), then I could use the script mentioned above for the first run  (copying from the first old PART_NO) but when I run the second time (copying from the second old PART_NO) it will fail because there will be duplicates.

Can anyone tell me how to rewrite this script so it will just skip/ignore the insert statement when a record allready exists..

Thanks in advance

Best regards

Stefan Bruhn
0
Comment
Question by:Stefan_Bruhn
  • 2
5 Comments
 
LVL 6

Accepted Solution

by:
morphman earned 63 total points
ID: 9661542

procedure proc1(V_IN_PARAM char, V_IN_ISSUE number, V_IN_REV number) is

v_count number(1) := 0;

begin

SELECT count(*) FROM BOM  
into v_count
WHERE PART_NO = V_IN_PART_NO
AND ISSUE = V_IN_ISSUE
AND REV = V_IN_REV

if v_count > 0 then
    INSERT INTO BOM (PART_NO, ISSUE, REV, C_TYPE, COMP_PART_NO, POS)
    SELECT '195N2106', ISSUE, REV, C_TYPE, COMP_PART_NO, POS FROM BOM
    WHERE PART_NO = V_IN_PARAM  
    AND ISSUE = V_IN_ISSUE
    AND REV = V_IN_REV
end if;

end proc1;
/

And then process

exec proc1(part_no);

Sorted.
0
 
LVL 8

Expert Comment

by:Danielzt
ID: 9661911

try this.

INSERT INTO BOM (PART_NO, ISSUE, REV, C_TYPE, COMP_PART_NO, POS)
SELECT '195N2106', ISSUE, REV, C_TYPE, COMP_PART_NO, POS FROM BOM  WHERE PART_NO = '195N2131'                                                                  
AND ISSUE = 6
AND REV = 3
and not exist (select 1 from BOM where PART_NO = '195N2106')
0
 
LVL 8

Assisted Solution

by:Danielzt
Danielzt earned 62 total points
ID: 9661935
sorry.

a little change:

INSERT INTO BOM (PART_NO, ISSUE, REV, C_TYPE, COMP_PART_NO, POS)
SELECT '195N2106', ISSUE, REV, C_TYPE, COMP_PART_NO, POS FROM BOM  WHERE PART_NO = '195N2131'                                                                  
AND ISSUE = 6
AND REV = 3 and rownum=1
and not exists (select 1 from BOM where PART_NO = '195N2106')

this SQL will make sure insert 1 records no matter how many old records existing.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

815 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

11 Experts available now in Live!

Get 1:1 Help Now