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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
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.

820 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