How do I do this update?

I am an Oracle idiot coming from MSSQL world. I have a specific
update I need to accomplish. Say I have two tables:
Table A with 2 columns 'Id' and 'Name':
101 Matt
102 Kevin
103 Matt
104 Peter
Table B with 2 columns 'Id' and 'Name':
300 Matt
400 John
Basically, for any name in table A that matches a name in table
B, I need to update Id column of table A with the corresponding
Id value found in table B. That is, the result should be:
Table A:
300 Matt
102 Kevin
300 Matt
104 Peter
I tried
"UPDATE A SET A.Id = (SELECT B.Id FROM B WHERE A.Name =
B.Name);"
The result is not what I want (as follows). Apprently the engine
decides to pad NULL for me. Urr..
Table A:
300 Matt
Kevin
300 Matt
Peter
Then I tried "MERGE" statement. But MERGE forces me to
specify "... WHEN NOT MATCHED THEN INSERT ..." as in follows:
"MERGE INTO A USING B ON (A.Name = B.Name)
WHEN MATCHED THEN UPDATE SET A.Id = B.Id
WHEN NOT MATCHED THEN INSERT ... /* Can I skip this? */"
Well, I just don't want to insert any new rows into TABLE A if
the ON condition is false.
Any expert out there please help me?
As a courtesy, in MSSQL, I can say
"UPDATE A SET A.Id = B.Id FROM A, B WHERE A.Name = B.Name
Nice and sweet and clean. Does Oracle have equivalent?
Thanks,
Matt

The problem is that your update isn't constrained so every row
gets updated. Try...
UPDATE A
SET A.Id = (SELECT B.Id FROM B WHERE A.Name = B.Name)
where exists (select * from b where a.name = b.name); Richard

Similar Messages

  • How can I rewrite this update stmt to improve its poor performance?

    Hi,
    I have the following update stmt that runs for over 6 hours. Here is the SQL and its plan:
                UPDATE TABLE1
                     SET mainveh = 'Y'
                 WHERE (comp#,polnum,a8dtef,a8deef,a8dtac,
                        DECODE(everif,'Y',10000,0)+auunit*100+vrcdsq)
                    IN (SELECT comp#,polnum,a8dtef,a8deef,a8dtac,
                               MAX(DECODE(everif,'Y',10000,0)+auunit*100+vrcdsq)
                          FROM TABLE1
                         GROUP BY comp#,polnum,a8dtef,a8deef,a8dtac);
    PLAN_TABLE_OUTPUT
    | Id  | Operation             | Name     | Rows  | Bytes |TempSpc| Cost (%CPU)|
    |   0 | UPDATE STATEMENT      |          |     1 |   108 |       |   798K  (1)|
    |   1 |  UPDATE               | TABLE1   |       |       |       |            |
    |   2 |   HASH JOIN           |          |     1 |   108 |  1079M|   798K  (1)|
    |   3 |    TABLE ACCESS FULL  | TABLE1   |    21M|   834M|       |   224K  (1)|
    |   4 |    VIEW               | VW_NSO_1 |    21M|  1364M|       |   440K  (1)|
    |   5 |     SORT GROUP BY     |          |    21M|   794M|  2453M|   440K  (1)|
    |   6 |      TABLE ACCESS FULL| TABLE1   |    21M|   794M|       |   224K  (1)|I'm using Oracle 10.2.0.3. The TABLE1 table has 21 million rows. The update stmt will update about 15 million rows. How can I rewrite this update stmt so it'll perform better? There is a primary index on all the columns selected in the subquery. That is the only index on TABLE1.
    Thank you!
    Edited by: user6053424 on Jul 21, 2010 6:59 AM

    Hi,
    Thank you for your suggestions. There is an index on the columns in the group by, it is the PK index on TABLE1. I'm suspecting that due to the amount of data to update, the optimizer decided that full table scan is cheaper than index scan. I'm very interested in the GTT idea, but still need some help if I decide to create a GTT from the subquery, because if I just do this:
    create global temporary table table1_tmp
    on commit preserve rows
    as SELECT comp#,polnum,a8dtef,a8deef,a8dtac,
               MAX(DECODE(everif,'Y',10000,0)+auunit*100+vrcdsq)
          FROM TABLE1
         GROUP BY comp#,polnum,a8dtef,a8deef,a8dtac;then the original update stmt still has the DECODE and such in it, I'm not sure how much benefit that'll do to us:
    UPDATE TABLE1
                     SET mainveh = 'Y'
                 WHERE (comp#,polnum,a8dtef,a8deef,a8dtac,
                        DECODE(everif,'Y',10000,0)+auunit*100+vrcdsq)
                    IN (SELECT comp#,polnum,a8dtef,a8deef,a8dtac,???
                          FROM TABLE1);Your input is greatly appreciated! Thanks!

  • How I can do this update, when I have two records (one primary key, no seq)

    How I can do this update in pl\sql
    Here is the table with some data
    CREATE TABLE INSERT_TE
    PIDM        VARCHAR2(8),
    FORM_ID     VARCHAR2(2),
    TEACHER     VARCHAR2(30)
    INSERT into INSERT_TE
    PIDM,
    FORM_ID,
    TEACHER
    SELECT
    '1106651',
    'TE',
    'Teacher, Alber Howard'
    from dualcommit;
    INSERT into INSERT_TE
    PIDM,
    FORM_ID,
    TEACHER
    SELECT
    '1106651',
    'TE',
    'Teacher, Alber2 '
    from dualcommit;
    INSERT into INSERT_TE
    PIDM,
    FORM_ID,
    TEACHER
    SELECT
    '2321241',
    'TE',
    'Teacher, Silly Billy '
    from dualcommit
    You are going to ended with something like this: In a cursor..
    PIDM     FORM_ID     TEACHER
    1106651     TE     Teacher, Alber Howard
    1106651     TE     Teacher, Alber2
    2321241     TE     Teacher, Silly Billythen I need to update a table
    if the there is only one record like this one 2321241 TE Teacher, Silly Billy
    not problem I do this
    UPDATE   saturn.sarchkl
             SET
             sarchkl_receive_date = SYSDATE,
             sarchkl_activity_date = SYSDATE,
             sarchkl_source = 'U',
             sarchkl_source_date = SYSDATE
             WHERE
              sarchkl_pidm = v_pidm3
              AND sarchkl_receive_date IS NULL
              AND sarchkl_term_code_entry = p_term
              AND sarchkl_admr_code = 'REC1'But if there is a PIDM (pk) with two records
    PIDM FORM_ID TEACHER
    1106651 TE Teacher, Alber Howard
    1106651 TE Teacher, Alber2 I need to do 2 updates (notice the AND sarchkl_admr_code rec1 for the firs and rec2 for the second
    The first record should update the table when the condition is sarchkl_admr_code = 'REC1' and the second record with the
    condition is sarchkl_admr_code = 'REC2'
    UPDATE   saturn.sarchkl
             SET
             sarchkl_receive_date = SYSDATE,
             sarchkl_activity_date = SYSDATE,
             sarchkl_source = 'U',
             sarchkl_source_date = SYSDATE
             WHERE
              sarchkl_pidm = v_pidm3
              AND sarchkl_receive_date IS NULL
              AND sarchkl_term_code_entry = p_term
              AND sarchkl_admr_code = 'REC1'and
    UPDATE   saturn.sarchkl
             SET
             sarchkl_receive_date = SYSDATE,
             sarchkl_activity_date = SYSDATE,
             sarchkl_source = 'U',
             sarchkl_source_date = SYSDATE
             WHERE
              sarchkl_pidm = v_pidm3
              AND sarchkl_receive_date IS NULL
              AND sarchkl_term_code_entry = p_term
              AND sarchkl_admr_code = 'REC2'I am doing this in pl\sql, I don't have a sequence in the table INSERT_TE, how I know that the first record in
    1106651 TE Teacher, Alber Howard
    1106651 TE Teacher, Alber2 update a rec1 and the second updates when sarchkl_admr_code = 'REC2',
    I guess I can create a sequence, but I am inserting a table from a select satement, I am getting the error
    ORA-02287: sequence number not allowed here, when I try to do the following:
    INSERT INTO SYTEACH
    SYTEACH_PIDM,
    SYTEACH_ID,
    SYTEACH_TEACHER,
    SYTEACH_SEQ
    SELECT  
          DISTINCT
          spriden_pidm,
          sarchkl_admr_code,
          szsform_form_id,
          szsform_schl_off_type||', '||szsform_schl_off_firstname||' '||szsform_schl_off_lasttname teacher,
           SZSFORM_SEQ.NEXTVAL
            FROM   saturn_midd.szsform, saturn.spriden, saturn.sarchkl
           WHERE       spriden_ntyp_code = 'CAPP'
                   AND szsform_common_app_id = spriden_id
                   AND spriden_pidm = sarchkl_pidm
                   AND sarchkl_admr_code = 'REC1'
                   AND szsform_form_id = 'TE'
    What is the simple way of doing this update..

    Hi,
    you said:
    >
    But if there is a PIDM (pk) with two records
    PIDM FORM_ID TEACHER
    1106651 TE Teacher, Alber Howard
    1106651 TE Teacher, Alber2
    >
    No! pks are unique! (otherwise we don't call them pk)
    For your table you could define a unique like this:
    PIDM column + a sequence. (new column):
    ALTER TABLE INSERT_TE ADD(TEACH_SEQ NUMBER)
    UPDATE INSERT_TE x
       SET TEACH_SEQ =
              (SELECT n
               FROM   (SELECT ROWID rid,
                              ROW_NUMBER() OVER(PARTITION BY pidm ORDER BY TEACHER) n
                       FROM   INSERT_TE t) s
               WHERE  s.rid = x.ROWID)
    PIDM     FORM_ID     TEACHER     TEACH_SEQ
    1106651     TE     Teacher, Alber Howard      1
    1106651     TE     Teacher, Alber2       2
    2321241     TE     Teacher, Silly Billy      1I need to do 2 updates (notice the AND sarchkl_admr_code rec1 for the firs and rec2 for the second
    The first record should update the table when the condition is sarchkl_admr_code = 'REC1' and the second record with the condition is sarchkl_admr_code = 'REC2'
    >
    normally no.
    but you can (now) do
    select PIDM, FORM_ID, TEACHER, 'REC'||TEACH_SEQ TEACH_SEQ from INSERT_TE
    PIDM     FORM_ID     TEACHER     TEACH_SEQ
    1106651     TE     Teacher, Alber Howard     REC1
    1106651     TE     Teacher, Alber2      REC2
    2321241     TE     Teacher, Silly Billy      REC1and use the last column for you update ...
    Edited by: user11268895 on Aug 20, 2010 2:01 PM

  • OK, I've downloaded Firefox 4.0, but there is no "continue" button to proceed with the "install" process! How do I install this update???

    OK, I've downloaded Firefox 4.0, but there is no "continue" button to proceed with the "install" process! How do I install this update???

    After saving the file which is likely called "Firefox Setup 4.0.1.exe" you need to run it to install Firefox 4.
    If you do not clear the downloads from the download manager, you can run the installer from the downloads manager. Open the download manager, then right-click on the Firefox setup file and select Open. As soon as you start running the setup file, close Firefox. You need to do this so that the setup file can replace the files in the Firefox installation directory.

  • How do I uninstall this update?

    I've just installed the latest Yosemite update and iPhoto has become Photo. I detest it - 10 000 photos all jumbled up in a tiny screen like on an iPad.
    So, how do I uninstall this update?
    <Re-Titled By Host>

    iPhoto running in 10.10.3
    And as you just want to argue about every fact I am done with you, I shall no longer respond.
    Bye

  • HT1338 I just updated my system to 10.6.8.  New computer just out of the box, but I wanted to download Numbers from the App store and it says I need 10.7.4.  How do I get this update?

    I have a new IMac, purchased from my Dad that he never used.  I just did the latest update to 10.6.8.  I wanted to get "Numbers" on the computer from the App Store but it says I need 10.7.4.   How do I get this?  Everytime I try update it goes to 10.6.8.  I am new to Apple and struggling a bit.

    Choose About this Mac from the Apple menu and check the processor.
    If it's a Core Duo Mac, you must install the DVD version of iWork. If you previously got a compatible version of Numbers from the Mac App Store, go to its home page and click on Support under Quick Links.
    If it's a Core 2 Duo or better Mac, try purchasing Mac OS X 10.8 from the Mac App Store. If you get told that it's incompatible, phone the online Apple Store and order a download code for 10.7.
    If upgrading to 10.7 or 10.8 requires any other upgrades or replacements(they both require at least 2GB RAM and don't support PowerPC-only software such as Office 2004), it may be cheaper to buy and install the DVD version of iWork even if your computer can be upgraded to use the Mac App Store version.
    (74446)

  • Please, Please help. How do I install this update

    I keep getting this error (F3-FF00-0006) when trying to do a system recovery on a Toshiba Satellite L500 Laptop that was running W7. The Toshiba support site say's to download the HDD Recovery Update 1.0.0.4 and install  it. I can download it on another computer and put it onto a disk, but if the L500 I am trying to do a recovery on is all but useless...how do I get the update onto it to fix this recovery error!!!
    This seems to be a flaw in Toshibas logic here. There are no instructions on what to do with the update, any help will be most appreciated. Oh, I have put the update on a disk and put into the DVD drive of the L500...and am still getting the same error
    Kind regards
    Calmlee

    Have ordered Recovery disks from Toshiba support at $66 the set. See if that sorts out my problem.

  • HT5278 How do I download this update?

    Hi, I am new to iphone and I receieved a software update notice. It is for ios5.1.1  How do I download and install this update?

    Along the left border of the iTunes window, find a tab with the name of your phone.  Click it and iTunes will go to the summary page.
    Actually, you can update without the computer since you already have iOS 5.:
    Settings > General > Software Update

  • HT6058 how do i do this update iOS 7.0.4

    I do not know how to do this iOS 7.0.4 , my I PHONE4S is telling me that i have an udate to do and i am not able to do the update its requesting for me to do. Because i just dont know how to do it. Please help me out here with this . Thank you so much,

    trose6201 wrote:
    i just dont know how to do it. Please help me out here with this .
    http://lmgtfy.com/?q=how+to+update+iphone

  • HT5934 How do I stop this update uploading to my mobile? Because I can't take any photos at all and the update is using up every inch of my mobile's space,not allowing me to keep all of my photos even though I've not got that many on there in the first pl

    How do I stop updates uploading on my mobile please? Because I cannot take any photos at all on it as the iOS update program 7.0.3 is hogging up every inch of my mobile's space, not allowing me to keep my photos even though I've not that many anyway. Help?

    Turn off Wifi in settings and then turn it on again; this will pause your device's update from updating.
    To stop automatic updates of applications, go to settings, Itunes&appstore, scroll down to automatic downloads and turn off updates
    Hope I've helped

  • HT4528 How can I get this update off my iPhone. It is horrible, not user friendly at all!!

    Can someone please tell me how to get this new horrible update off my phone?? It is not user friendly!

    Jimmy tomb wrote:
    Can someone please tell me how to get this new horrible update off my phone?? It is not user friendly!
    That's unfortunate.  Go to http://www.apple.com/feedback/ and leave Apple feedback about iOS 7.

  • HT201270 need a newer version of ios ...have 4.3 cant download anthing...how do I do this update

    my ipad has ios 4.3 and will not let me upate to a newer version
    unable to download facebook, games etc because of this older version
    help!!!

    How to update iDevices

  • How I can do this update....merge ?

    I have this query
    select GORADID_ADID_CODE from GORADID a
    where exists
    (select * from goradid_cvt  b
    where a.goradid_pidm = b.goradid_pidm
    and a.GORADID_ADID_CODE <> b.goradid_adid_code  ) I need to do an update the GORADID_ADID_CODE on the table GORADID
    needs to be update with the GORADID_ADID_CODE from the table goradid_cvt
    I know a pl\sql precedure will do it , I know how to do it, but I just want to write a little code like merge, perhaps
    The query can be change to
    select GORADID_ADID_CODE from GORADID_cvt a
    where exists
    (select * from goradid   b
    where a.goradid_pidm = b.goradid_pidm
    and a.GORADID_ADID_CODE <> b.goradid_adid_code  )

    Actually this does not work
    I have in the table GORADID
    GORADID_PIDM     GORADID_ADDITIONAL_ID     GORADID_ADID_CODE     GORADID_USER_ID     GORADID_ACTIVITY_DATE     GORADID_DATA_ORIGIN
    1210136     United States     CTZ1                      Conversion                                                                      03/06/2012 16:25:12                     Recruitment Plus
    1210136     United States     CTZ2                      James                                                                       03/07/2012 10:43:10     Jamesin the CVT table GORADID_cvt
    GORADID_PIDM     CONVERT_PIDM     GORADID_ADDITIONAL_ID     CONVERT_ADDITIONAL_ID     GORADID_ADID_CODE     CONVERT_ADID_CODE     GORADID_USER_ID     CONVERT_USER_ID
    1210136     B0020     United States     United States     CTZ2     CTZ2     James     James
    1210136     B0020     Australia                        Australia                         CTZ1     CTZ1     James     JamesI need to ended with an update exactly as the one in the cvt table
    This is the code
    MERGE INTO    goradid        dst
    USING   (
    SELECT   goradid_pidm
            ,        SUBSTR(A.goradid_adid_code,1,4) 
           ,        INITCAP(a.GORADID_ADDITIONAL_ID) aDDITIONAL_ID
           ,a.GORADID_ACTIVITY_DATE
           ,A.GORADID_USER_ID
           ,a.GORADID_DATA_ORIGIN
            FROM     goradid_cvt    a
           WHERE A.GORADID_CVT_STATUS LIKE 'U%'
         AND   EXISTS (
                                 SELECT  1
                    FROM    goradid   b
                        WHERE    a.goradid_pidm         = b.goradid_pidm
                     AND b.GORADID_ADID_CODE like 'C%'
                    AND A.GORADID_PIDM = 1210136
                    AND    substr(a.goradid_adid_code,1,4) !=  substr(b.goradid_adid_code,1,4))
                    )  SRC
                  ON    (src.goradid_pidm = dst.goradid_pidm)
                 WHEN MATCHED THEN UPDATE
                   SET  dst.goradid_adid_code = dst.goradid_adid_code,
                           dst.goradid_user_id =    dst.GORADID_USER_ID,
                           dst.goradid_additional_id = dst.goradid_additional_id,
                            dst.goradid_activity_date = trunc(sysdate),
                           dst.goradid_data_origin =  'James1'
                   SELECT COUNT(*) ,
                           GORADID_CVT_STATUS
                           FROM  GORADID_CVT
                           GROUP BY  GORADID_CVT_STATUS the select stament returns
    SELECT   goradid_pidm
            ,        SUBSTR(A.goradid_adid_code,1,4) 
           ,        INITCAP(a.GORADID_ADDITIONAL_ID) aDDITIONAL_ID
           ,a.GORADID_ACTIVITY_DATE
           ,A.GORADID_USER_ID
           ,a.GORADID_DATA_ORIGIN
            FROM     goradid_cvt    a
           WHERE A.GORADID_CVT_STATUS LIKE 'U%'
         AND   EXISTS (
                                 SELECT  1
                    FROM    goradid   b
                        WHERE    a.goradid_pidm         = b.goradid_pidm
                     AND b.GORADID_ADID_CODE like 'C%'
                    AND A.GORADID_PIDM = 1210136
                    AND    substr(a.goradid_adid_code,1,4) !=  substr(b.goradid_adid_code,1,4))
                    )  SRC
    {code)
    returns1210136     CTZ1     Australia      3/7/2012     James     James
    when I run the merge updates the two records, I nned to ended upating the  
    I need to update the table GORADID like this GORADID_PIDM     CONVERT_PIDM     GORADID_ADDITIONAL_ID     CONVERT_ADDITIONAL_ID     GORADID_ADID_CODE     CONVERT_ADID_CODE     GORADID_USER_ID     CONVERT_USER_ID
    1210136     B0020     United States     United States     CTZ2     CTZ2     James     James
    1210136     B0020     Australia      Australia      CTZ1     CTZ1     James     James
    Edited by: 893973 on Mar 7, 2012 8:55 AM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • I have an ipad 1 with 4.3 IOS but I understand it can be updated as far as 5.1.1 How do I get this update. My ipad says all updates are complete.

    I need to know how to update my ipad operating system from 4.3 to 5.1

    You need to use iTunes on your computer in order to update your iPad.
    There are some important things that you should know before you update.
    http://support.apple.com/kb/ht4972
    The updating instructions are in this article. Make sure you read the part about using iTunes.
    http://support.apple.com/kb/HT4623

  • HT4972 I need to update my iPad 1 to ioS5 how do I get this update?

    How do I update my iPad 1 to ioS5 or later?

    If you have an iPad 1, the max iOS is 5.1.1. For newer iPads, the current iOS is 6.1.3. The Settings>General>Software Update only appears if you have iOS 5.0 or higher currently installed.
    iOS 5: Updating your device to iOS 5 or Later
    http://support.apple.com/kb/HT4972
    How to install iOS 6
    http://www.macworld.com/article/2010061/hands-on-with-ios-6-installation.html
    iOS: How to update your iPhone, iPad, or iPod touch
    http://support.apple.com/kb/HT4623
    If you are currently running an iOS lower than 5.0, connect the iPad to the computer, open iTunes. Then select the iPad under the Devices heading on the left, click on the Summary tab and then click on Check for Update.
    Tip - If connected to your computer, you may need to disable your firewall and anitvirus software temporarily.  Then download and install the iOS update. Be sure and backup your iPad before the iOS update. After you update an iPad (except iPad 1) to iOS 6.x, the next update can be installed via wifi (i.e., not connected to your computer).
    Tip 2 - If you're updating via wifi, place your iPad close to your router to preclude getting a corrupted download.
     Cheers, Tom

Maybe you are looking for

  • While activating  Transformation 0PS_C041 - 0CO_OM_WBS_2 am getting error

    Dear all While activating the Transformation 0PS_C041 - 0CO_OM_WBS_2 am getting the following error Error when writing routines Message no. RSAA561 Error while activating transformation Message no. RSTRAN510 Error when activating Transformation 0N7SM

  • Unable to get help from Adobe regarding Creative Cloud

    How can I actually get help administering my Creative Cloud team?  The system is throwing me an error when I try to invite team members and the link to contact customer support drives me to a non-technical call center where the person was actually ru

  • Mail.app immediately crashing on launch, cannot get email

    I'm having trouble with Mail.app. Every time I launch it it crashes. I click on the icon, it bounces, the window opens, but then it freezes, after a few seconds, it crashes. Here is an excerpt from the crash log: +Process: Mail [2668]+ +Path: /Applic

  • Valuation of multiple specification is not happening

    Hi All, I am doing the result recording for an inspection lot with manual valuation. Valuation is happening for this but this valuation is not getting updated to multiple specification tab. Where I am getting lightning symbol against each country obj

  • Is anyone having issues with icloud?

    I log in just fine but when I click on contact it gives me an error.  Anyone having issues?