Update vs Merge - Updating one table based on values in another

Hello
I have two tables lets say TAB_A and TAB_B. I altered table B to include a new column from table A
I wrote a merge statement as follows to merge the data
MERGE INTO TAB_A
USING TAB_B
ON (TAB_A.SECURITYPERSONKEY=TAB_B.SECURITYPERSONKEY)
WHEN MATCHED THEN
  UPDATE SET TAB_A.PPYACCOUNT=TAB_B.PPYACCOUNT;
I know INSERT is for inserting new records
UPDATE to my knowledge is to modify currently existing records (loosely)
MERGE is one I rarely used, until this particular scenario.
The code works perfectly fine, but I was wondering how could I write an update statement? Or in this scenario should I even be using an update statement?

The MERGE is good and performs well, although it malfunctions (sometimes updating data wrongly) if the destination is a view with INSTEAD OF triggers.
The UPDATE with similar performance to the MERGE depends on SECURITYPERSONKEY being a unique or primary key on TAB_B. It is:
update (select TAB_A.PPYACCOUNT a_PPYACCOUNT, TAB_B.PPYACCOUNT b_PPYACCOUNT
        from TAB_A JOIN TAB_B on TAB_A.SECURITYPERSONKEY=TAB_B.SECURITYPERSONKEY)
SET A_PPYACCOUNT=B_PPYACCOUNT;
This can set multiple columns. It also currently doesn't work on a view with INSTEAD OF triggers.
Frank's update also works with multiple columns (eg also updating col2 and col2), with this syntax:
UPDATE  tab_a
SET     (ppyaccount, col2, col3) = (
                         SELECT  ppyaccount, col2, col3
                         FROM   tab_b
                         WHERE  securitypersonkey  = tab_a.securitypersonkey
WHERE   EXISTS       (
                         SELECT  ppyaccount
                         FROM    tab_b
                         WHERE   securitypersonkey  = tab_a.securitypersonkey

Similar Messages

  • Updating a field on one screen based on values in another screen

    We have defined a new screen (ccalled Customer Data) for the Business Partner transaton BP.
    On this screen is a new field that has been defined and appended to BUT000. The field is a kind of customer GUID that allocates a unique ID string to a customer (we call it a matchcode, not to be confused with the SAP standard meaning).
    The value in the field is calculated by a function module, which needs uses some of the address data on the standard address tab of the BP transaction. If that data on the address screen changes, then the following needs to happen:
    1) The matchcode field on the Customer Data screen needs to be updated.
    2) When saved, the new matchcode is updated to the database (not a problem if the matchcode is recalcuated on the Customer Data screen).
    Here are the questions:
    1) How can I access the updated data on the address screen in the Customer Data screen? I was under the impression that the funtion modules which respond to events on each page are in their own function groups, so I cannot see the changed data from any function module bound to the Customer Data screen events. Is that true?
    2) How could I access the changed data on the address screen to re-calculate the matchcode field?
    BR,
    Tony.

    Closed; I spent a lot of time analysing this.Answer I think is no.
    1) No guarantee of the order in which the screens are processed (DSAVE event for addresses is after DSAVE for custom screen, so scratch that).
    2) Data exists in separate memory areas for separate screens and needs to be passed by EXPORT/IMPORT. 1) prevents this.

  • Update one table based on condition from another table using date ranges

    Hello!
    I have two tables:
    DateRange (consists of ranges of dates):
    StartDate            FinishDate
            Condition
    2014-01-02
          2014-01-03           true
    2014-01-03     
     2014-01-13          
    false
    2014-01-13      
    2014-01-14           true
    Calendar (consists of three-year dates):
    CalendarDate    IsParental
    2014-01-01
    2014-01-02
    2014-01-03
    2014-01-04
    2014-01-05
    2014-01-06
    2014-01-07
    2014-01-08
    2014-01-09
    2014-01-10
    I want to update table Calendar by setting  IsParental=1
      for those dates that are contained in table DateRange between
    StartDate and FinishDate AND Condition  IS TRUE.
    The query without loop should look similar to this but it works wrong:
    UPDATE
    Calendar
    SET IsParental = 1
     WHERE
    CalendarDate   BETWEEN
    (SELECT
    StartDate 
    FROM  DateRange
    WHERE Calendar.  CalendarDate   = DateRange. StartDate
               AND   
    (SELECT StartDate 
    FROM  DateRange
    WHERE Calendar.  CalendarDate   = DateRange. FinishDate
    AND Condition
     IS TRUE
    Is it possible to do without loop? Thank you for help!
    Anastasia

    Hi
    Please post DDL+DML next time :-)
    -- This is the DDL! create the database structure
    create table DateRange(
    StartDate DATE,
    FinishDate DATE,
    Condition BIT
    GO
    create table Calendar(
    CalendarDate DATE,
    IsParental BIT
    GO
    -- This is the DML (insert some sample data)
    insert DateRange
    values
    ('2014-01-02', '2014-01-03', 1),
    ('2014-01-03', '2014-01-13', 0),
    ('2014-01-13', '2014-01-14', 1)
    GO
    insert Calendar(CalendarDate)
    values
    ('2014-01-01'),
    ('2014-01-02'),
    ('2014-01-03'),
    ('2014-01-04'),
    ('2014-01-05'),
    ('2014-01-06'),
    ('2014-01-07'),
    ('2014-01-08'),
    ('2014-01-09'),
    ('2014-01-10')
    select * from DateRange
    select * from Calendar
    GO
    -- This is the solution
    select CalendarDate
    from Calendar C
    where EXISTS (
    select C.CalendarDate
    FROM DateRange D
    where C.CalendarDate between D.StartDate and D.FinishDate and D.Condition = 1
    UPDATE Calendar
    SET IsParental = 1
    from Calendar C
    where EXISTS (
    select C.CalendarDate
    FROM DateRange D
    where C.CalendarDate between D.StartDate and D.FinishDate and D.Condition = 1
    [Personal Site] [Blog] [Facebook]

  • How to restrict values in one prompt based on value from another prompt

    Hi
           I have a requirement as follows. I have two fields (objects)
    program code
    contract number
    one program code can have several contract numbers.
    I need to provide prompts on Program code and contract number. when user selects one program from the prompt on program code then contract field should only show contract numbers belonging the program user selected.
    Please let me know how to bring this functionality in Webi report.
    Thanks in advance.

    Hi
    if ur requirement is in query level its impossible.(i mean whn u click on programcode then u have to display in contract at query level)
    if you want to display at report level then  
    The two objects Program and contract have a mapping then place a program object in query filter and use operator equal to and select prompt and run the query then it will ask you to select program .whn on choosing it you will  get filtered contract numbers for that program in report level  . There no need to use prompt for contract numbers object .
    Hope this helps u
    Thanks
    sunil
    Edited by: K.sunil on Nov 7, 2011 9:30 AM

  • Deleting from a table based on values in a second table

    Is it possible to DELETE from (or for that matter do an UPDATE to) a table based on values in another table? I have gone through the online documentation but can't seem to find anything. I'm trying to delete rows from table A where A.field1 = B.field1 and B.field2 = 'X'. (B being the second table)

    It is done using subqueries in the where clause.
    delete from A
    where A.field1 in (select B.field1 from B where B.field2 = 'X');
    delete from A
    where exists (select 'x' from B where B.field1 = A.field1 and B.field2 = 'X');
    delete from A
    where A.rowid in (select A.ROWID from from A, B where B.field1 = A.field1 and B.field2 = 'X');
    And many other varieties. Eg. more specialised:
    delete from A
    where A.txdate < (select B.prune_date FROM B where B.field1 = A.field1 and B.field2 = 'X')
    and A.txstate in (select S.txstate from S where S.prodlass=a.prodclass and s.deletable='Y');

  • How to update one table based on another table ??

    Hello Friends:
    I am trying to run the following query in oracle but it won't run.
    UPDATE BOYS
    SET
    BOYS.AGE = GIRLS.AGE
    FROM GIRLS
    WHERE
    BOYS.FIRSTNAME = GIRLS.FIRSTNAME AND
    BOYS.LASTNAME = GIRLS.LASTNAME;
    This query runs fine in sql server but in oracle its saying can't find "SET". PLease tell me what is the correct syntax in oracle to update one table based on another table.
    thanks

    See if this helps.
    If you wrote an SQL statement:
    update boys set age = 10;
    Every row in the boys table will get updated with an age of 10. But if you wrote:
    update boys set age = 10
    where firstname = 'Joe';
    Then only the rows where the firstname is Joe would be updated with an age of 10.
    Now replace the 10 in the above statements with (select g.age from girls g where g.firstname = b.firstname and g.lastname = b.lastname) and replace where firstname = 'Joe' in the second statement with where exists (select null from girls g where g.firstname = b.firstname and g.lastname = b.lastname). The same concepts apply whether 10 is an actual value or a query and whether you have a where clause with the update statement to limit rows being updated.
    About the select null question regarding the outer where clause:
    Since the query is checking to see if the row in the girls table exists in the boys table what the column is in this select statement doesn't matter, this column isn't being used anywhere. In this case Todd chose to use null. He could have also used a column name from the table or a lot of times you'll see the literal value 1 used here.

  • Update table based on values from other table

    Hi,
    I am trying to update one table based on the values of another table. Since you can't use From in update statements, how do you execute this?
    For example i have to tables, Table A and Table B. I want to update a column or columns in Table A based on another value in Table B:
    So if the column in Table B was 1 then column in Table A would be Yes, if Table B was 2, then Table A would be Yes, if Table B was 3 then Table A would be N/A and so on...
    Any help would be appreciated.
    thanks,
    scott

    SQL> select * from t1;
    ID ST
    1
    2
    3
    SQL> select * from t2;
    NO
    1
    2
    3
    4
    SQL> update t1 set status=(select decode(no,1,'Y',2,'N','NA') from t2 where t1.id=t2.no);
    3 rows updated.
    SQL> select * from t1;
    ID ST
    1 Y
    2 N
    3 NA
    Daljit Singh

  • Problem updating a control on one panel based on a value change in another control on a different panel

    Hi,
    I am trying to update the value of a control on one panel when the value of another control on a different panel is changed.  The two panels are saved in two different .uir files, so there are two associated .h files generated by CVI.  The problem is that, inside the callback function for the control that is being modified (Ctrl_Id_A on Panel_A), when I call SetCtrlVal(Panel_B, Ctrl_Id_B, Value); 'Panel_B' and 'Ctrl_Id_B' (which have the same numeric values as Panel_A = 1 and Ctrl_Id_A = 2 in their respective .h files) are being interpreted as Panel_A and Ctrl_Id_A.  I never understood how CVI makes this distinction, eg. knowing which of PANEL_A = 1 and PANEL_B = 1 is being referred to, but didn't worry about it since I never needed cross-communication between panels until now.  Any help on how to implement this would be greatly appreciated.  Thanks!
    Solved!
    Go to Solution.

    This is a basic issue on which you can find tons of forum posts
    The online help for the function recitates:
    int SetCtrlVal (int panelHandle, int controlID, ...);
    Parameters
    Input
    Name
    Type
    Description
    panelHandle
    int
    Specifier for a particular panel that is currently in memory. You obtain this
    handle from LoadPanel, NewPanel, or DuplicatePanel.
    controlID
    int
    The defined constant, located in the .uir header file, that you assigned to the control in the User Interface Editor, or the ID returned by NewCtrl or DuplicateCtrl.
    value
    New value of the control. The data type of value must match the data type of the control.
    That is, you must not use the panel constant name in the first parameter of SetCtrlVal, use the panel handle instead. The system guarantees that all panel handles are unique throughout the whole application whichever is the number of panels used in every moment.
    int SetCtrlVal (int panelHandle, int controlID, ...);
    Purpose
    Sets the value of a control to a value you specify.
    When you call SetCtrlVal on a list box or a ring control, SetCtrlVal
    sets the current list item to the first item that has the value you
    specify. To set the current list item through a zero-based index, use SetCtrlIndex.
    When you call SetCtrlVal on a text box, SetCtrlVal appends value to the contents of the text box and scrolls the text box to display value. Use ResetTextBox to replace the contents of the text box with value.
    Note   This function updates the displayed value immediately. Use SetCtrlAttribute with ATTR_CTRL_VAL to set the control value without immediately updating the displayed value. For this reason, SetCtrlAttribute with ATTR_CTRL_VAL is generally faster than SetCtrlVal. However, if the control in which you are setting the value is the active control in the panel, SetCtrlAttribute with ATTR_CTRL_VAL displays the value immediately.
    Note   This function is not valid for graph and strip chart controls.
    Parameters
    Input
    Name
    Type
    Description
    panelHandle
    int
    Specifier for a particular panel that is currently in memory. You obtain this
    handle from LoadPanel, NewPanel, or DuplicatePanel.
    controlID
    int
    The defined constant, located in the .uir header file, that you assigned to the control in the User Interface Editor, or the ID returned by NewCtrl or DuplicateCtrl.
    value
    New value of the control. The data type of value must match the data type of the control.
    Proud to use LW/CVI from 3.1 on.
    My contributions to the Developer Zone Community
    If I have helped you, why not giving me a kudos?

  • FM / BAPI to update STEUC field in marc Table basing on material and plant

    Hi,
    I need to to update STEUC field in marc Table basing on material and plant. please suggest FM / BAPI.
    Thanks.
    raviraj.

    Hi
    Try this BAPI function module:
    " BAPI_MATERIAL_MAINTAINDATA_RT "
    Data: LS_HEADDATA TYPE BAPIE1MATHEAD,
             LS_RETURN TYPE BAPIRETURN1,
              LT_PLANT TYPE TABLE OF BAPIE1MARCRT,          LS_PLANT TYPE BAPIE1MARCRT,
              LS_PLANTX TYPE BAPIE1MARCRTX,          LT_PLANTX TYPE TABLE OF BAPIE1MARCRTX.
            CALL FUNCTION 'BAPI_MATERIAL_MAINTAINDATA_RT'
                   EXPORTING
                       HEADDATA   = LS_HEADDATA
                   IMPORTING
                       RETURN     = LS_RETURN
                   TABLES
                       PLANTDATA  = LT_PLANT
                       PLANTDATAX = LT_PLANTX.
    Check the PLANTDATA in the above fm in which pass your ''STEUC'' to BAPIE1MARCRT-CTRL_CODE......
    Hope this works.....

  • How to create a new row for a VO based on values from another VO?

    Hi, experts.
    in jdev 11.1.2.3,
    How to create a new row for VO1 based on values from another VO2 in the same page?
    and in my use case it's preferable to do this from the UI rather than from business logic layer(EO).
    Also I have read Frank Nimphius' following blog,but in his example the source VO and the destination VO are the same.
    How-to declaratively create new table rows based on existing row content (20-NOV-2008)
    http://www.oracle.com/technetwork/developer-tools/adf/learnmore/13-create-with-params-169140.pdf
    I have tried:
    1.VO1(id,amount,remark1) and VO2(id,amount,remark2) are based on different EO,but render in same page,
    2.Drag and drop a Createwithparams button for VO1(id,amount,remark),
    3.add: Create insertinside Createwithparams->Nameddata(amount),
    4.set NDName:amount, NDValue:#{bindings.VO2.children.Amount}, NDtype:oracle.jbo.domain.Number.
    On running,when press button Createwithparams, cannot create a new row for VO1, and get error msg:
    <Utils> <buildFacesMessage> ADF: Adding the following JSF error message: For input string: "Amount"
    java.lang.NumberFormatException: For input string: "Amount"
         at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
    Can anyone give some suggestions?
    Thanks in advance.
    bao
    Edited by: user6715237 on 2013-4-19 下午9:29

    Hi,CM,
    I'm really very appreciated for your quick reply! You know, today is Saturday, it's not a day for everyone at work.
    My principal requirement is as follows:
    1.select/check some rows from VO2, and for each selection create a new row with some attributes from VO2 as default values for VO1's corresponding attributes, and during this process the user may be cancel/uncheck or redo some of the selections.
    --so it's better to implement it in UI rather than in EO.
    2.it's better to implement this function with declarative way as in Frank Nimphius' blog.
    --little Jave/JS coding, the better. I only have experience in ORACLE FORMS, little experience in JAVA/JS.
    In order to get full information for the requirements of my use case, can take a check at:
    How to set default value for a VO query bind variable in a jspx page?
    (the end half of the thread: I have a more realworld requirement similar to the above requirement is:
    Manage bank transactions for clients. and give invoices to clients according to their transaction records. One invoice can contain one or many transactions records. and one transaction records can be split into many invoices.
    Regards
    bao
    Edited by: user6715237 on 2013-4-19 下午11:18
    JAVE->JAVA

  • Is it possible to reference one cell from the value of another?

    Is it possible to reference one cell from the value of another e.g.
    value of b1 = value of c(value of a1)
    So if a1 = 3 then b1 = c3, if a1 = 5 then b1 = c5.

    Excellent!
    Thanks Wayne. Just saved me many hours and a headache.
    Works like a dream.
    Thank you for your succinct (and accurate) answer.
    Mark

  • Updation of more then one table at a time

    Hi,
    Can we Update more than one table at a time with a single query????
    and pl Note my server is : SQL Server

    pls note that you are in a Oracle forum (FYI Oracle is not a MS product).
    Alos I'm sure that there are plenty of MS SQL Server forums on the WWW where you ll find an answer to your question.

  • Update 1 column,1 single table based on where results of multiple tables

    I would like the my_id column in 1 table updated to the static value of 247 for my_id stored in the 1 single table based on the where clause, which uses the my_id column in it.
    The update statement updates all rows in table1, instead of just the rows where
    the condition ( (b.my_id=a.my_id)
    and ( b.my_name like 'OIS SrClerk%')
    and (a.f_id=m.f_id)
    and (trunc(m.cr_time) < '02-Apr-2008')) is true
    What needs to be changed?
    update table1 a
    set a.my_id=247
    from table1 a, table2 b, table3 m
    where
    (b.my_id=a.my_id)
    and ( b.my_name like 'OIS SrClerk%')
    and (a.f_id=m.f_id)
    and (trunc(m.cr_time) < '02-Apr-2008')

    Are you looking for this?
    UPDATE table1 A
       SET A.my_id = 247
    WHERE EXISTS (SELECT 'x'
                     FROM table2 b,
                          table3 M
                    WHERE b.my_id = A.my_id
                     AND b.my_name LIKE 'OIS SrClerk%'
                     AND A.f_id = M.f_id
                     AND TRUNC (M.cr_time) < to_date('02-apr-2008 00:00:00','dd-mon-yyyy HH:MI:SS') )changed date string to to_date
    Message was edited by:
    devmiral

  • How to update one lov based on value selected from another lov.

    Hi ,
    I have a form in which I created the 2 LOVs for my event_date field and event_name field each which is stored in one table.
    t I want to update the event_date LOV everytime event_name is selected.
    How can I achieve this.
    Thanks.
    Neeti.

    Hi,
    You can do this using dynamic LOVs. Here are the steps for populating a employee LOV on selecting a department.
    1) Create a dynamic lov say emloyee_lov with a bind variable like this
    select empno,ename from scott.emp
    where deptno = :dept
    2) Create a form on scott.emp.
    3) In the empno field which should be a LOV type of field, associate it with
    the above employee LOV.
    4) Then set the binding(s) for the bind variable(s) defined in the lov by
    selecting the deptno column.
    This would help.
    Thanks,
    Sharmila

  • Insert values to one table based on a value inserted into another table

    Hi,
    I've got a form based off a report which creates a new project. I've added an additional process to this form to insert four new values into another table as soon as the new project is created and the PK for that project is generated. This was working last week (of course!) and now seems to not work at all. It's complaining that the PK I was getting from my first insert was null. Here is one the the statements in my process I'm trying to run:
    insert into week_group values(week_group_seq.nextval, (SELECT trunc(NEXT_DAY(SYSDATE, 'FRIDAY')) FROM dual), 0, '', :P45_PROJECT_SEQ, sysdate, :APP_USER);
    The complaint I get that it's getting null where :P45_PROJECT_SEQ should be.
    Thoughts?
    Thanks,
    Jon

    Hi Andy,
    Thanks for the tip. Those two values didn't match and I updated them so they do and I'm still getting a "cannot insert NULL..." error.
    When I turn on debug I see that I'm getting the PK and I see the value. Here's my debug output:
    0.24: ...Process "Get PK": PLSQL (AFTER_SUBMIT) declare function get_pk return varchar2 is begin for c1 in (select PROJECT_SEQ.nextval next_val from dual) loop return c1.next_val; end loop; end; begin :P45_PROJECT_SEQ := get_pk; end;
    0.25: ...Session State: Saved Item "P45_PROJECT_SEQ" New Value="252"
    0.25: ...Process "Process Row of PROJECT": DML_PROCESS_ROW (AFTER_SUBMIT) #OWNER#:PROJECT:P45_PROJECT_SEQ:PROJECT_SEQ|IUD
    0.26: ...Session State: Save "P45_PROJECT_SEQ" - saving same value: "252"
    0.26: ...Process "reset page": CLEAR_CACHE_FOR_PAGES (AFTER_SUBMIT) 45
    0.27: Nulling cache for application "120" page: 45
    0.27: ...Process "Add Week Groups": PLSQL (AFTER_SUBMIT) insert into week_group values(week_group_seq.nextval, (SELECT trunc(NEXT_DAY(SYSDATE, 'FRIDAY')) FROM dual), 0, '', :P45_PROJECT_SEQ, sysdate, :APP_USER); insert into week_group values(week_group_seq.nextval, (SELECT trunc(NEXT_DAY(SYSDATE, 'FRIDAY') +
    0.28: Encountered unhandled exception in process type PLSQL
    0.28: Show ERROR page...
    0.28: Performing rollback...
    I notice that when it runs my process "Add Week Groups" it's not displaying all of the SQL. But the SQL is fine, it's right here:
    insert into week_group values(week_group_seq.nextval, (SELECT trunc(NEXT_DAY(SYSDATE, 'FRIDAY')) FROM dual), 0, '', :P45_PROJECT_SEQ, sysdate, :APP_USER);
    Hmmm....what about the "reset page" action in the last of the 0.26 lines?
    Thanks,
    Jon

Maybe you are looking for

  • No more alpha in exported QuickTime movie from Keynote '09

    Hello In Keynote '08, I create a slide with no background (None). I add some text with some fancy text IN and OUT features. I export using the Animation codec and the transparency option. When I reimport the movie in another Keynote '08 file, it inhe

  • Itunes has encountered an error & needs to close

    I keep getting the above message when trying to open itunes. Have tried uninstalling and reinstalling itunes & quicktime without sucess. Also tried going back to various restore points that I know itunes worked ok. My spyware (adaware) and anti-virus

  • What exactly is the "Version Name" to be used for?

    Hi - I just picked up Aperture and have been trying to figure it out. And to be honest, I'm pretty darn OCD about photo organization - which is one reason Aperture appealed to me more than iPhoto, which I've been using forever. So I've literally been

  • Safari 5.0.6 crashes as soon as I open it. I am running OS X 10.5.8. Any suggestions?

    Here is the problem report: Process:         Safari [10921] Path:            /Applications/Safari.app/Contents/MacOS/Safari Identifier:      com.apple.Safari Version:         5.0.6 (5533.22.3) Build Info:      WebBrowser-75332203~3 Code Type:       X

  • LR3.2 Filmstrip does not update correctly for new imported images

    Installed 3.2 last week and today I did my first import. I downloaded from camera into a subfolder my photo folder as usually and then run synchronize on the photo folder in order for the images to appear in LR. After that everything looks as it used