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,082 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.

Join & Write a Comment

Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

757 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

16 Experts available now in Live!

Get 1:1 Help Now