If this is not an appropriate ng, let me know and please redirect me.
I am not sure if my question is Oracle specific or if it is OK to ask
Oracle specific questions here.
I have a many to many relationship. I've learned that when that
happens you are to make an intermediate table between the two. So,
I've got a table with 2 fields, an ID from the one side and an ID from
the other.
In my application I am writing all the upserts for all my entities. In
doing so, I've come across my first upsert of this type.
Normally, my upserts looks like this:
BEGIN
UPDATE PROSPECT_INTERESTS
SET PERSON_ID = :pProspectID, INTEREST_ID = :pInterestID
WHERE PERSON_ID = :PersonID AND INTEREST_ID = :pInterestID;
IF SQL%NOTFOUND THEN
INSERT INTO PROSPECT_INTERESTS
(PROSPECT_ID, INTEREST_ID)
VALUES
(:pProspectID, :pInterestID);
END IF;
END;
In this case, the update statement makes no sense, because all there
is to update is the primary key which identifies the thing in the
first place. So, what is the proper way to say "If it doesn't exist
already insert it, otherwise do nothing?"
|
|
0
|
|
|
|
Reply
|
cpisz
|
2/19/2010 3:42:03 PM |
|
On 19 Feb, 16:42, cpisz <christopherp...@gmail.com> wrote:
> If this is not an appropriate ng, let me know and please redirect me.
> I am not sure if my question is Oracle specific or if it is OK to ask
> Oracle specific questions here.
>
> I have a many to many relationship. I've learned that when that
> happens you are to make an intermediate table between the two. So,
> I've got a table with 2 fields, an ID from the one side and an ID from
> the other.
>
> In my application I am writing all the upserts for all my entities. In
> doing so, I've come across my first upsert of this type.
>
> Normally, my upserts looks like this:
>
> BEGIN
> UPDATE PROSPECT_INTERESTS
> SET PERSON_ID =3D :pProspectID, INTEREST_ID =3D :pInterestID
> WHERE PERSON_ID =3D :PersonID AND INTEREST_ID =3D :pInterestID;
> IF SQL%NOTFOUND THEN
> =A0 =A0INSERT INTO PROSPECT_INTERESTS
> =A0 =A0 =A0 (PROSPECT_ID, INTEREST_ID)
> =A0 =A0 =A0 VALUES
> =A0 =A0 =A0 (:pProspectID, :pInterestID);
> END IF;
> END;
>
> In this case, the update statement makes no sense, because all there
> is to update is the primary key which identifies the thing in the
> first place. So, what is the proper way to say "If it doesn't exist
> already insert it, otherwise do nothing?"
You don't mention what version of Oracle you are using but if it is
new enough I would suggest merge:
MERGE INTO PROSPECT_INTERESTS x
USING (
VALUES (..., ...)
) y (PERSON_ID, INTEREST_ID)
ON (x.PERSON_ID, x.INTEREST_ID) =3D (y.PERSON_ID, y.INTEREST_ID)
WHEN NOT MATCHED THEN
INSERT (PERSON_ID, INTEREST_ID)
VALUES (y.PERSON_ID, y.INTEREST_ID)
If your version don't support merge you could try using dual as in
insert into PROSPECT_INTERESTS
select ...,... from dual x
where not exists (
select 1 from PROSPECT_INTERESTS y
where (y.PERSON_ID, y.INTEREST_ID) =3D (...,...)
)
I don't use Oracle myself so you might have to adjust the syntax
above.
/Lennart
|
|
0
|
|
|
|
Reply
|
Lennart
|
2/19/2010 6:40:43 PM
|
|
|
1 Replies
381 Views
(page loaded in 0.075 seconds)
Similiar Articles: If the records does not exist, insert it - comp.databases ...If this is not an appropriate ng, let me know and please redirect me. I am not sure if my question is Oracle specific or if it is OK to ask Oracle s... INSERT PICTURE INTO A CONTAINER FIELD DYNAMICALLY - comp.databases ...If the records does not exist, insert it - comp.databases ... INSERT PICTURE INTO A CONTAINER FIELD DYNAMICALLY - comp.databases ... INSERT PICTURE INTO A CONTAINER FIELD ... Insert statements and foreign keys etc.. - comp.databases.mysql ...If the records does not exist, insert it - comp.databases ... Insert statements and foreign keys etc.. - comp.databases.mysql ... If the records does not exist, insert it ... SQL - File does not exist - comp.soft-sys.sasIf the records does not exist, insert it - comp.databases ... SQL - File does not exist - comp.soft-sys.sas If the records does not exist, insert it - comp.databases ... S-Function Does not exist - comp.soft-sys.matlabSQL - File does not exist - comp.soft-sys.sas SQL - File does not exist - comp.soft-sys.sas If the records does not exist, insert it - comp.databases ... Insert into Y select * from X - autoincrement fields - comp ...If the records does not exist, insert it - comp.databases ... Insert into Y select * from X - autoincrement fields - comp ... If the records does not exist, insert it ... how to use a trigger to stop an insert/update? - comp.databases ...If the records does not exist, insert it - comp.databases ... how to use a trigger to stop an insert/update? - comp.databases ... If the records does not exist, insert it ... Sharepoint linked table - can add records but can't update ...Hello I have an Access 2003 database linked to a simple list (one field) in Sharepoint 2007. When I open the table in Access, I can add new records... How to check object(button) exist in webpage or not - comp.lang ...If the records does not exist, insert it - comp.databases ... Hey all, Is there a way to check if a VAR exists (or does not exist ... Button does not work on web page ... go to a particular record in a form - comp.databases.ms-access ...If the records does not exist, insert it - comp.databases ..... in A2010 Navigation control with ... > > (If the sub-form does not exist, then the current active one is ... How to write INSERT IF NOT EXISTS queries in standard SQL at XaprbIf necessary, INSERT IF NOT EXISTS queries can be written in a single ... nothing to the INSERT statement and hence there is no insert. This of course returns 0 records ... If the records does not exist, insert it - comp.databases ...If this is not an appropriate ng, let me know and please redirect me. I am not sure if my question is Oracle specific or if it is OK to ask Oracle s... 7/26/2012 4:30:27 PM
|