Using Merge to Update to Insert, Update, and also deleting obsolete records

Hi all!!!
Suppose that a have the following table, called Orders:
ORDERID        STATUS      PRICE
         1          0        100
         2          0        200
         3          0        300
         4          0        350
         5          0        390 Also, there is a procedure that will generate some data on a global temporary table, called Orders_Temp. This temporary table will be used to update the Orders table.+Orders+ have a trigger that will perform different operations when a Insert, Delete or Update command is performed.
Consider that the Orders_Temp table have the following data after running a procedure that is responsible for updating orders data:
ORDERID        STATUS      PRICE
         1          1        100
         2          2        200
         3          0        300
         5          0        390
         6          0        350 As you can see, orders 1 and 2 were updated. Order 3 and 5 were untouched, order 4 was deleted and order 6 was inserted. Merge will handle the correctly handle orders 1, 2, 3, 5 and 6, even though generating a update to orders 3 and 5, that were not changed.
So, here are the questions:
1) How could I delete the order 4 from Orders, since this order was not present on the Orders_Temp? Is there a way to use the merge command to do this?
2) Is there any way to not generate a update operation for orders 3 and 5, that were not changed?
I´m using Oracle 10.2 SE.
Thanks a lot!
Regis

Hello G!
Thank you so much for your help.
Your code took only a modification:
MERGE INTO orders tgt
USING (SELECT A.*, 1 mask
          FROM orders_temp A
        UNION ALL
        SELECT A.*, 0
          FROM orders A
         WHERE NOT EXISTS
                 (SELECT 1
                    FROM orders_temp b
                   WHERE A.order_id = b.order_id)) src
    ON (tgt.order_id = src.order_id)
WHEN MATCHED
THEN
  UPDATE SET tgt.status = src.status, tgt.price = src.price
     /*WHERE src.mask = 1
       AND tgt.status != src.status
       AND tgt.price != src.price*/
  DELETE
     WHERE src.mask = 0
WHEN NOT MATCHED
THEN
  INSERT VALUES (src.order_id, src.status, src.priceWithout doing this, the DELETE WHERE clause was never called. After reading the docs, I found why:
Specify the DELETE where_clause to clean up data in a table while populating or updating it. The only rows affected by this clause are those rows in the destination table that are updated by the merge operation. That is, the DELETE WHERE condition evaluates the updated value, not the original value that was evaluated by the UPDATE SET ... WHERE condition. If a row of the destination table meets the DELETE condition but is not included in the join defined by the ON clause, then it is not deleted. Any delete triggers defined on the target table will be activated for each row deletion.http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_9016.htm
>
From what I understand, if we restrict the records on update set_clause, the delete  where_clause will no be able to process because they were not touched. So, as as workaroud, I´m checking if the record was changed inside my trigger.
Thanks a lot!
Regards.
Regis

Similar Messages

  • HT4623 Please  help!  i have just update my iphone 4s with 7.4 update and my phone is now asking for a password which i dont have.  I have tried my keypad lock i used before the update and also my itunes password and neither work, how do i rectify this ??

    Please  help!  i have just update my iphone 4s with 7.4 update and my phone is now asking for a password which i dont have.  I have tried my keypad lock i used before the update and also my itunes password and neither work, how do i rectify this ???

    Did you buy this iPhone new from an authorized seller?

  • I have an apple ID, I'm trying to use this to update and download apps etc. however, whenever I try this, it keeps asking me to use my old ID, which is attached to an email that no longer exists! I can't download or update anything! Help!

    I have an apple ID, I'm trying to use this to update and download apps etc. however, whenever I try this, it keeps asking me to use my old ID, which is attached to an email that no longer exists! I can't download or update anything! Help!

    You can log out of the currently logged in account by taping on the id in Settings > Store (Settings > iTunes & App Stores on iOS 6) and you can then log back in.
    If you updated your existing account with a new email address then that shoud 'refresh' it on it
    If you created a new account then any content that you purchased/downloaded via the old account will remain tied to that old account, and only that old account can redownload its content and download updates to its apps. You should be able to update an account's email address via this page : http://appleid.apple.com

  • Hi can anybody help please. I am having terrible problems trying to use my Nikon D7100 to tether. I have downloaded the latest Lightroom updates and also checked my firmware which is also the latest avaiable and still Lightroom wont detect my camera!

    Hi can anybody help please. I am having terrible problems trying to use my Nikon D7100 to tether. I have downloaded the latest Lightroom updates and also checked my firmware which is also the latest avaiable and still Lightroom wont detect my camera!. When I use a friends Canon camera it works every time!

    Hi Keith thanks for your reply I have Lightroom 5.7.1 64 bit and my Nikon's firmware is version 1.02

  • I have a wifi connection but "safari cannot open the page because it is not connected to the internet" I have reset everything; I have used Itunes to update and reset. Still no good. It worked until 2 days ago

    I have a wifi connection but "safari cannot open the page because it is not connected to the internet" I have reset everything; I have used Itunes to update and reset. Still no good. It worked until 2 days ago

    Reset everything.. did you do this?
    Tap Settings / General / Reset / Reset Network Settings
    If you're already been there, done that, could be a third party app preventing Safari from accessing the web.
    Re sync your apps 5 at a time, then test the iPad.

  • TS2774 hi i have done the settings that mentioned in apple support threads but it wont work for me i am using BSNL cellone SIM and activated 3G too but unfortunately internet is not connecting but my facebook is updated and also showing the data statistic

    hi i have done the settings that mentioned in apple support threads but it wont work for me i am using BSNL cellone SIM and activated 3G too but unfortunately internet is not connecting but my facebook is updated and also showing the data statistics

    They are not a supported carrier. You'll have to take it up with them.

  • My hp support assistance wont run. it keeps stopping during updates and when deleting the temporary

    My hp support assistance wont run. It keeps stopping during updates and when deleting the temporary internet files. So it never finishes the process.. help

    The failure to remove the Temporary Files could just be due to having your internet connection active during the removal process.  For most folks, the amount of data in this directory is minimal.   If you want / need to remove the files, you could close your browser(s), make sure your Internet music and TV connections are closed, and then try removing the files manually:
    Path to Folder:
    C:\Users\--YourAccountNameHere--\AppData\Local\Microsoft\Windows\Temporary Internet Files
    Warning: 
    Do NOT remove the "Temporary Internet Folder" itself.  Go "inside" the folder before removing anything.
    ==================================================​==================================
    The HP Support Assistant is great when it works -- and not so great when it won't cooperate.  You can try removing the program from your computer and installing it again.  That may work.
    Control Panel > icon view > Programs and Features >
    Scroll down, find, and Uninstall HP Support Assistant.
    Restart your computer after uninstalling the existing version of Support Assistant.
    Install Support Assistant
    HP Support Assistant
    Using HP Support Assistant (Windows 8)
    Troubleshooting HP Support Assistant
    NOTE:  At writing, there are possible issues with HPSA version 7.x -- you might want to sitck with version 6.x for now.
    ==================================================​====================================
    If you have issues removing your version of HPSA:
    Fix problems with programs that can't be installed or uninstalled
    ==================================================​===================================
    Workaround to get Updates:
    If all avenues fail to bring joy and it just won't work, you can still get your updates from your computer's website.
                                                      ​         Install Software and Drivers from HP Website
    Go to the top of the HP forum page to Support and Drivers
    If prompted, Enter your Country or Region and
    Enter your Computer Model information,
    Go to Software and Drivers...
    Enter your Operating System in the Drop-down menu...  Scroll down
    Select desired Category
    Expand and decide if you need to install that package.
    Assure that you download and install only the most recent versions of each driver or program.
    Save the appropriate *.exe installation package on your computer
    NOTE the name and the location - the *.exe file will likely save to "Downloads “
    Install the Software / Driver installation package:  If you have ‘Admin’ control, you may highlight the package(s) and “double-click” to install, else Right-Click, Select ‘run as Administrator’ and install.
    NOTE:  There is sometimes a lot of information at the computer’s website.  Be sure to take an extended look at what’s available.
    ==================================================​====================================
    Good Luck!
    Kind Regards,
    Dragon-Fur

  • Firefox sent an update and also said java was outdated, and may need to uninstall my current java to get the new. i uninstalled and now need the new, I also lost my email addresses,

    Firefox sent an update and also said my java was outdated. I uninstalled my present java and now need the updated version,and instructions, I also lost my email address book, and i think these
    are related, thank you

    You can get beta 3 from http://releases.mozilla.org/pub/mozilla.org/firefox/releases/4.0b3/win32/
    Switching to earlier betas is not recommended . If add-on compatibility is important to you, you should consider using the latest release version (3.6.8) instead of betas.

  • I updated the newest ios update and it deleted my Notes.

    I updated the newest ios update and it deleted my Notes. I backup to a paid icloud account and can't find how to restore the notes. Or are they lost ? Please help!

    Vans2day707 wrote:
    My iPhone 4 has locked itself with a password and I have no clue what the password is. 
    Is this your issue  >  http://support.apple.com/kb/HT5818

  • HT1766 How do I restore music library I created prior to update and also missing all apps.

    How do I restore music library I created prior to update and also missing apps. How do I reverse an update?

    You sync your content back by connecting and selecting your iPhone in computer iTunes.
    You cannot reverse an update. Downgrading was never supported.

  • JAVA ISN'T WORKING. I've tried software update and also reinstalling java. But it's still not working. How can I fix it? OS X 10.5.8

    I've tried software update and also reinstalling java. But it's still not working. How can I fix it? OS X 10.5.8

    Bad juju not having a bootable backup that you can revert back to when you run into these kinds of issues. Get a bootable, external HD, and make a bootable backup/clone before updating/upgrading and ensure that it's bootable and works like the original. See these for details:
    http://docs.info.apple.com/article.html?artnum=106941
    http://www.macmaps.com/upgradefaq.html
    http://www.macmaps.com/backup.html
    http://www.thexlab.com/faqs/installswupdates.html
    http://www.thexlab.com/faqs/backuprecovery.html
    Back to your issue. Boot with the Leopard install disk, select your language, select Disk Utility from the Utilities menu, and repair the disk and permissions. Report back.

  • Itunes updated and it deleted all my playlist and half my music along with it. Now if I want to sync my phone it tells me in order to sync it has to delete more stuff off my phone. I would like to get my music and playlists back, how do I do that?

    tunes updated and it deleted all my playlist and half my music along with it. Now if I want to sync my phone it tells me in order to sync it has to delete more stuff off my phone. I would like to get my music and playlists back, how do I do that?

    Empty/corrupt library after upgrade/crash
    Hopefully it's not been too long since you last upgraded iTunes, in fact if you get an empty/incomplete library immediately after upgrading then with the following steps you shouldn't lose a thing or need to do any further housekeeping. In the Previous iTunes Libraries folder should be a number of dated iTunes Library files. Take the most recent of these and copy it into the iTunes folder. Rename iTunes Library.itl as iTunes Library (Corrupt).itl and then rename the restored file as iTunes Library.itl. Start iTunes. Should all be good, bar any recent additions to or deletions from your library.
    See iTunes Folder Watch for a tool to catch up with any changes since the backup file was created.
    When you get it all working make a backup!
    Should you be in the unfortunate position where you are no longer able to access your original library, or a backup of it, then see Recover your iTunes library from your iPod or iOS device.
    tt2

  • I HAVE A SOURCE TABLE WITH 10 RECORDS AND TARGET TABLE 15 RECORDS. MY WUESTION IS USING WITH THE TABLE COMPARISON TRANSFORM I WANT TO DELETE UNMATCHED RECORDS FROM THE TARGET TABLE ??

    I HAVE A SOURCE TABLE WITH 10 RECORDS AND TARGET TABLE 15 RECORDS. MY QUESTION IS USING WITH THE TABLE COMPARISON TRANSFORM .I WANT TO DELETE UNMATCHED RECORDS FROM THE TARGET TABLE ?? HOW IT IS ??

    Hi Kishore,
    First identify deleted records by selecting "Detect deleted rows from comparison table" feature in Table Comparison
    Then Use Map Operation with Input row type as "delete" and output row type as "delete" to delete records from target table.

  • Can i use time capsule for external h drive and also for time machine?

    Can i use time capsule for external h drive and also for time machine?

    Hello, vascocaco.
    If I understand correctly, you're wondering if you can use your Time Capsule's internal hard disk both for Time Machine backups and as an ordinary drive. Is this your question?
    You can, but Time Machine backups may take longer.
    To do it, you should partition your Time Capsule's hard disk to create one volume for the backups and one for other files.

  • HT1414 when i facetime somebody they cant hear me, and also when i record a video the sound is muffled. there isnt a problem when making a phone call. any solutions?

    when i facetime someone they cant hear me, and also when i record a video there is no sound, however when i make a phone call there is no problem. any solutions?

    When I try to make a call it will go through and I can hear the other person but they cannot hear me, is there a setting that disables the speaker when using the headphones to try to make a call, also speakerphone automatically goes on when I start a call which I do not like, any suggestions how to fix this?
    I also have this problem...........

Maybe you are looking for

  • Parked document 1000 700010042 2010 does not exist, Message no. FP076

    Hi, There is one Document which is not on tax return but made a entry in a GL account (VAT). When I display document from FB03 for fiscal year 2010 the transaction code FB03 is diverting to FBV3 and giving error message : "Parked document 1000 700010

  • BPM Process is not started

    Hi All, I defined an Integration Process which is triggered by an Asynchronous message (Receive step with "start process" checked). The trigerring message is sent through the HTTP Adapter (See: http://help.sap.com/saphelp_nw04/helpdata/en/82/f4993c03

  • HT201250 Is Time Machine Scalable?

    I have a MBA with about 150GB of files and a TIme Capsule.  It looks like this is too much for Time Machine to handle.  The initial backup takes 2 days and the hourly backups take more than an hour.  While the backup processes are running my MBA occa

  • MUlti-threading concept

    Hi All, Want to know how we can multi-thread a program for a particular PLSQL code in Oracle EBS for which the child thread doesn't consume any thread in the front-end even though it executes with user-specified threads?

  • Vollbildansicht in Bridge CC auf Macbook Pro 13 Retina (late 2013)

    Hallo zusammen, habe seit ein paar Tagen ein 13" MBP (late 2013) mit Retina Display und verzweifele langsam bei der Suche eines Bildbetrachters. Vorweg, der Bildschirm ist als "skaliert, mehr Fläche" konfiguriert, damit man wenigstens ein paar Tools