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,111 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
[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
  • 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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.
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
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 shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.
Suggested Courses

636 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