How to do update instead of insert / cancel the insert

Hello,
we are porting an application, written originally for PostgreSQL, to Oracle. Most of the porting is pretty straightforward - change column types, simple trigger syntax changes etc.
But we have a table (more precisely several tables) with a set of triggers, holding a tree-like structure. The point is in PostgreSQL we're using a RETURN NULL; behavior in BEFORE triggers, which means 'do nothing' to simplify thing.
For example imagine tables A and B - and in a trigger on B we could have something like
INSERT INTO A VALUES (id, ...)
without a check on a uniqueness of the id value. And in a BEFORE INSERT trigger on A we have something like
SELECT INTO c id FROM A where id = NEW.id;
IF FOUND THEN
UPDATE A SET .... WHERE id = NEW.id;
-- cancel the insert without an exception
RETURN NULL;
END IF;
which checks for existence of the row, and if it already exists it effectively replaces with UPDATE and cancels the INSERT without an exception.
Is there something like this in Oracle or do I have to completely rewrite / refactor the whole thing?
PS: We do have UNIQUE (more precisely PRIMARY KEY) on the columns - I am aware that this is not sufficient to enforce uniqueness in Oracle.

There are a couple of ways that this could be accomplished in Oracle, although the though of an insert into table b also inserting or updating rows in table a makes me cringe. The easiest way only requires a trigger on table b.
Something along the lines of:
SQL> CREATE TABLE a (ID NUMBER PRIMARY KEY, descr VARCHAR2(10));
Table created.
SQL> CREATE TABLE b (id NUMBER, descr VARCHAR2(10), descr2 VARCHAR2(20));
Table created.
SQL> CREATE TRIGGER b_bi
  2     BEFORE INSERT ON b
  3     FOR EACH ROW
  4  BEGIN
  5     INSERT INTO a VALUES (:new.id, :new.descr);
  6  EXCEPTION
  7     WHEN DUP_VAL_ON_INDEX THEN
  8        UPDATE a SET descr = :new.descr
  9        WHERE id = :new.id;
10  END;
11  /
Trigger created.
SQL> INSERT INTO a VALUES(1, 'One');
1 row created.
SQL> COMMIT;
Commit complete.
SQL> INSERT INTO b VALUES(1, 'NewOne', 'New One');
1 row created.
SQL> SELECT * FROM a;
        ID DESCR
         1 NewOne
SQL> INSERT INTO b VALUES(2, 'Two', 'Old Two');
1 row created.
SQL> SELECT * FROM a;
        ID DESCR
         1 NewOne
         2 Two
SQL> COMMIT;
Commit complete.If you really require the trigger on table a for other reasons, then you could do something like:
SQL> RENAME a to a_table;
Table renamed.
SQL> CREATE VIEW a AS SELECT * FROM a_table;
View created.
SQL> CREATE TRIGGER a_ii
  2     INSTEAD OF INSERT ON a
  3     FOR EACH ROW
  4  BEGIN
  5     INSERT INTO a_table VALUES(:new.id, :new.descr);
  6  EXCEPTION
  7     WHEN DUP_VAL_ON_INDEX THEN
  8        UPDATE a_table SET descr = :new.descr
  9        WHERE id = :new.id;
10  END;
11  /
Trigger created.
SQL> CREATE OR REPLACE TRIGGER b_bi
  2     BEFORE INSERT ON b
  3     FOR EACH ROW
  4  BEGIN
  5     INSERT INTO a VALUES (:new.id, :new.descr);
  6  END;
  7  /
Trigger created.
SQL> INSERT INTO b VALUES(2, 'NewTwo', 'New Two');
1 row created.
SQL> SELECT * FROM a;
        ID DESCR
         1 NewOne
         2 NewTwo
SQL> INSERT INTO b VALUES(3, 'Three','Third');
1 row created.
SQL> SELECT * FROM a;
        ID DESCR
         1 NewOne
         2 NewTwo
         3 ThreeNeither should require any changes to your application code. However, once agan, I think it is a really bad idea for triggers to have side efects on other tables.
John

Similar Messages

  • HT4623 How do I update my iphone 5 to the new ios7 software using itunes on my computer?

    How do I update my iphone 5 to the new ios7 software using itunes on my computer?

    Just for clarity, do you have iTunes 11.1 now on your computer?  If yes, when you connect your iPhone via your USB / Lightning cable is your iTunes opening with a small box near the top right of screen that shows your iPhone connected?  If yes then you can click through the various views of your iPhones content areas, such as Music, Photos, etc.  You will eventually get to a screen that is Status.  On that screen you should see the iOS Update option. 

  • How can I update my ipad 2 to the new OS system when I can't find the software

    how can I update my ipad 2 with the new operating system?  I am in 4.3.5 version

    iOS- How to update your iPhone, iPad, or iPod touch

  • Error message "update was terminated" while cancelling  the billing documen

    Hi Experts,
    Kindly can you advise on the following issue. We are getting the error message  "update was terminated" while cancelling the billing document using transaction code VF11.
    Note: The accounting document associated with the billing document is not posted and also note that we have done a check with the Basis team who has confirmed back stating they are seeing the udpate error for the user "    ". The log says that the  update was terminated because the user was trying to enter some duplicate records into the database.
    We could observe that the cancellation document is not created and also we have confirmed with the FI that they still continue to have the billing document open in the customer balance statementu2026
    Thanks for your help,
    Mithun

    HI Mithun ,
    Check if that billing document is there in VFX3 . If it is there then try to clear it from there and then try to reverse it.
    Hopefully it will be done.
    Regards,
    Nikhil

  • How can i update my ipod 3g to the iOS5 software?

    how can i update my ipod 3g to the iOS5 software?

    bobjbkln wrote:
    .... t it is unlikely that someone with a 2G 32 gb iPod would think that they had a 3G, because Apple stopped selling the 2G 32g iPod when the 3G came out.
    If only that were true! There have even been cases (in these discussion pages) of people believing they have a 4th gen Touch but it turns out it was a 2nd gen Touch.

  • How to make form do update instead of insert?

    I want to use the same data block in a form to do both
    inserts and updates. How can you manually tell the data block to do an update?
    The reason I want to do it manually is, I populate the block manually using a function when I want the user to
    do a data update.
    It seems like the data block is in insert mode by default. Is that true or do I have something else incorrect?
    Thanks,
    Tim

    Sounds like you are going out of your way to make this difficult for yourself.
    If I am understanding you correctly, the simplest way to achieve your goal is as follows:
    1. Create a block based on your table using the wizard including your ID columns as base-table fields.
    2. Manually add as many fields as necessary for your non-base table code/dscription fields (make sure the database item property is set to NO).
    3. Create a POST-QUERY trigger on your block. This trigger fires each time a record is retrieved from the database. So you can code a simple cursor to populate the non-base table fields.
    4. Your LOV needs to be written in such a way that it populates both the visible code/description fields and also the hidden ID fields. This will make sure that forms marks the record for update.
    5. The LOV needs to be associated with the field that the user will be typing values into (ie. the code field).
    If performance is an issue, you could also consider basing the block on a key-preserved view which will allow updates. However, I would consider the post-query approach first before getting too advanced.

  • How can I update attribute1 of CSI_ITEM_INSTANCES with the operating org

    Hi
    I am working on Install base (Customer services) module of Oracle E-Business Suite.
    I want to update the CSI_ITEM_INSTANCES.attribute1 with the operating org from a profile option.
    Could anyone help me to figure out how can i do this?
    Is there is any API which can be used to update this table.
    Actually to update this column i wrote a trigger on this table which does the updation of CSI_ITEM_INSTANCE.ATTRIBUTE1 with the operating org from the profile option set at responsibility level but i got the wrong value for this profile option. Because when the data is inserted into this table the user_id, resp_id and resp_appl_id has been changed to sysadmin so i got the value for this user not the user who logged in.
    If anybody has worked on this or done something like this please guide me
    Thanks

    Hi Naga,
    Thanks for the Reply...
    Actually I want to update the org_id in the CSI_ITEM_INSTANCE.attribute1 at the time of creation as well as any update.
    I have looked into the api csi_item_instances_pub. it has two procedures 1) create_instance and 2) update _instance
    I am not be able to understand where can I call these api's to update and creation for an instance and it has several parameters like record type and table type. So I am in a confusion state about the parameters list also. Could you help me to figure out this please?
    And I want to restrict the Installed Base View Page by the value of attribute1(org_id) in CSI_ITEM_INSTANCES. In a thread I saw that this could not be done because this is a JTT/JTF page. Is there any way to customize the Install base view page by org_id?
    Please suggest.
    Thanks
    Munish Mittal

  • What does error -3259 mean? How can i update my ipod touch to the new ios 5?

    I been updating my ipod touch 4 software to the new ios 5 all day. i tried disabling my security programs and firewall, different download programs and reinstalling iTunes, and restarting my ipod touch. But for each attempt i get a different error. i went to the Specific update and restore errror code page and got a reason for each error except for error -3259. Basically my question is: 1. How can i update my ipod software to io5 and 2. What is the reason behind error -3259?

    Connect it to iTunes on a computer and update it from there. Note that the second generation iPod touch(which includes 8GB models sold at the same time as the third generation ones) can't be updated past 4.2.1.
    (71380)

  • I have an IPad 1. I went to download 2 iBooks. I cannot down the load the latest version of the iBooks app. How do I get my money back cancel the purchase or get the book?

    I have an ipad1. I went to download 2 iBooks. They failed to download. I cannot an download the latestv version of the iBooks app. How do I cancel the purchase get the book or get the latestest versionoff the app without an upgrade.?

    Try downloading the current version of the iBooks app on your computer's iTunes so that the app is in your account's purchase history. Then on your iPad go to the Purchased tab in the App Store app and you should be able to download an older compatible version of the app.

  • How do you update a later version of the iPod touch? Mine dosen't have the update button in settings.

    I am confused because I downloaded on the computer the newest iTunes and it is not updating when I plug it in........How do I update my iOS 4.1 iPod to the newest iOS??? Please help. Thanks!

    To update
    Connect the iPod to your computer and update via iTunes
    iTunes 10 for Windows: Update and restore software on iPod, iPhone, or iPad
    A 1G iPod can go to iOS 2.2 via iTunes and iOS 3.1.3 via
    Purchasing iOS 3.1 Software Update for iPod touch (1st generation)
    A 2G to 4.2.1. Requires iTunes version 10.X. If a Mac it requires OSX 10.5.8 or later. With 1OSX 10.4.11 you can only go to iTunes 9.X which gets you as high as iOS 4.1
    A 3G to 5.1.1  Requires iTunes version 10.5 or later
    A 4G to 6  Requires iTunes version 10.7 or later. For a Mac, that requires a Mac with OSX 10.6.8 or later
    Identifying iPod models

  • How do I update photos from iPhoto on the iPad to the Mac?

    I've been using the iPhoto app on the iPad to edit my photos, and now I don't know how to automatically update those edits on the Mac. I can transfer the edited photos using Photo Stream, but I've ended with duplicate photos. Any help?

    http://support.apple.com/kb/PH3129
    hope this will help u

  • How do you update ur apple comp to the 11.1??

    how do i update it to the 11.1 or whatever it needs to be updated too?

    Are you referring to iTunes? Your operating system? Please provide more details as to your question and your machine specifications.

  • HT4623 how do i update my mac book with the latest version of itunes?

    Hi all, can anyone help me with updating my Mac Book with the latest itunes. I'm a bit old school and usually stick to same programs but now need to update?? Cheers!!

    Make a backup of your computer first before doing any updates.  In rare but unfortunate cases an update seems to result (from what the people posting here say) in libraries being deleted.  Then too, sometimes people update and discover they really don't like the new version.  Downgrading without a backup is difficult. In the long run you can't avoid going to a newer version but I, for example, have been running the same iTunes version for 7 years now so it is possible to hang on.
    You also don't provide details about your MacBook which includes many models.  If you are truly old school you could even be running a MacBook that can't  update to the newest, or at least may require other software or hardware upgrades.  Get more information about your computer. Go to the Apple in the upper left corner of any window, then  "About This Mac".  Write down what it says about "version"and report that here.  Now continue to "More Info..."  Copy and paste the information here, but omit the serial number and Hardware UUID (if present).

  • How stop auto update before you insert the ipod

    This seems to be a real quandary. sometimes you want to insert your ipod without the whole updating thing taking over you ipod. but I don't think you can change that setting unless the ipod in plugged into computer, and if setting is auto update it just starts. any ideas appreciated. thanks.

    thanks Niel, but before I proceed, what exactly happens when you do that, does the ipod not do the auto update? and you can access or update manually?
    also, anyway to stop the updating once it starts? thanks

  • How do I create a table of 3 data elements for each trial and have it updated instead of waiting until the end of the program?

    I am trying to record 3 pieces of data, all (software) timing in ms (in U8 integers). I managed to finally get each data in the right column but now it doesn't record anything in the table and it doesn't update the numeric indicators during the experiment like it should. I can't figure for the life of me what I did wrong. I know at least the trial part of the program works, but so far the data recording part is giving me a headache.
    Thanks
    Attachments:
    BETACRT.vi ‏241 KB

    Why it is not reporting the data is easy; fixing the program may not be. LabVIEW uses a dataflow paradigm. This means that no part of the program executes until all of its data inputs are availble. In your case the table and the array functions driving it are fed by outputs from the outer while loop. Thus no data gets to the table until the while loop has finished executing (which is at the end of your experiment). One possible approach would be to store the data in a shift register and move the table inside the loop.
    Many experienced LV programmers try to avoid or minimize the use of sequence structures and local variables to read and write to/from front panel objects. We use a state machine which is a while loop with a case structure. Queues can be u
    sed to pass data between the user interface and the data acquistion loops. This subject is too involved to be handled in a brief posting, but if you search the archives and examples you can learn more.
    Also, be aware that software timing can be problematic if you are using a desktop operating system. If the OS decides to check the net for software updates or something you could have large discrepancies in your timing. These might be rare, but there is no reliable way of detecting them. I have built several systems similar to what you seem to be trying to do and have never been successful with software timing.

Maybe you are looking for