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,086 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

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.

Question has a verified solution.

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

Suggested Solutions

Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
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.
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.

939 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

6 Experts available now in Live!

Get 1:1 Help Now