SELECT in an UPDATE

Hi Friends,
Just wanted to know if there is a better way to write an UPDATE on a table based on the 2 columns from a SELECT statement.
My SELECT goes as below:
SELECT    d.activity_id, sum(a.promo_shipped + a.promo_distro_shipped) sales
    FROM test_sales_hist a,
         ah_store_group_cust b,
         ah_item_xref c,
         ah_activity_external d,
         ah_chain_div_storegroup e,
         ah_div f
   WHERE a.cust_num = b.cust_num
     AND a.entry_dt >= d.activity_start
     AND a.entry_dt <= d.activity_end
     AND a.itm_id_edw = c.item_id_edw
     AND c.item_code_cust = d.item_code_cust
     AND b.store_group = e.store_group
     AND f.division = d.division
     AND e.div_id = f.div_id
     AND (   a.promo_ordered > 0
          OR a.promo_shipped > 0
          OR a.promo_distro_ordered > 0
          OR a.promo_distro_shipped > 0
GROUP BY activity_id;There is SALES column in AH_ACTIVITY_EXTERNAL table.
I want to update the SALES value from the above select query that matches the ACTIVITY_ID in AH_ACTIVITY_EXTERNAL table.
Right now I am thinking to use a BULK COLLECT and fetch all the ACTIVITY_ID's and SALES using the above SELECT query and using a FORALL to update the SALES column in AH_ACTIVITY_EXTERNAL for the matching ACTIVITY_ID.
Any better ideas are highly appreciated.
I am sure there must be a straight forward UPDATE joining the select query above.
Thanks,
Raj.

Ok. I've checked it - it is working.
satyaki>
satyaki>select * from v$version;
BANNER
Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - Prod
PL/SQL Release 10.2.0.3.0 - Production
CORE    10.2.0.3.0      Production
TNS for 32-bit Windows: Version 10.2.0.3.0 - Production
NLSRTL Version 10.2.0.3.0 - Production
Elapsed: 00:00:00.01
satyaki>
satyaki>
satyaki>create table frank_o
  2  as
  3    select empno e_no, comm cc_code
  4    from emp;
Table created.
Elapsed: 00:00:00.24
satyaki>
satyaki>desc frank_o;
Name                                      Null?    Type
E_NO                                               NUMBER(4)
CC_CODE                                            NUMBER(7,2)
satyaki>
satyaki>select * from frank_o;
      E_NO    CC_CODE
      9999       3455
      7777       3400
      7521        500
      7566
      7654       1400
      7698
      7782
      7788
      7839
      7844          0
      7876
      E_NO    CC_CODE
      7900
      7902
13 rows selected.
Elapsed: 00:00:00.10
satyaki>
satyaki>
satyaki>create table frank_n
  2  as
  3    select empno e_no, nvl(comm,rownum) cc_code
  4    from emp;
Table created.
Elapsed: 00:00:00.21
satyaki>
satyaki>
satyaki>desc frank_n;
Name                                      Null?    Type
E_NO                                               NUMBER(4)
CC_CODE                                            NUMBER
satyaki>
satyaki>
satyaki>select * from frank_n;
      E_NO    CC_CODE
      9999       3455
      7777       3400
      7521        500
      7566          4
      7654       1400
      7698          6
      7782          7
      7788          8
      7839          9
      7844          0
      7876         11
      E_NO    CC_CODE
      7900         12
      7902         13
13 rows selected.
Elapsed: 00:00:00.28
satyaki>
satyaki>
satyaki>merge into frank_o o
  2  using frank_n n
  3  on ( o.e_no = n.e_no )
  4  when matched then
  5    update set o.cc_code = n.cc_code
  6  when not matched then
  7    insert(o.e_no,o.cc_code) values(n.e_no,n.cc_code)
  8    where 1=2;
13 rows merged.
Elapsed: 00:00:01.59
satyaki>
satyaki>select * from frank_o;
      E_NO    CC_CODE
      9999       3455
      7777       3400
      7521        500
      7566          4
      7654       1400
      7698          6
      7782          7
      7788          8
      7839          9
      7844          0
      7876         11
      E_NO    CC_CODE
      7900         12
      7902         13
13 rows selected.
Elapsed: 00:00:00.18
satyaki>
satyaki>
satyaki>commit;
Commit complete.
Elapsed: 00:00:00.04
satyaki>Regards.
Satyaki De.

Similar Messages

  • When I select "Check for updates..."

    Dear Firefox,
    When I select "Check for Updates" the pop up screen simply notes that my system is "Downloading update" but nothing ever happens.
    Please let me know what I can do to fix this.
    Thank you,
    Lee

    You can download the latest Firefox 4 beta version from http://www.mozilla.com/firefox/all-beta.html
    Trash the current Firefox 4.0b7 version and install the latest beta.

  • I have just switched on my new 5s and gone through the intro sections. After entering my apple id, I selected the icloud update I wanted it to use (from my 4s). It is now stuck on the progress bar screen. What can I do?

    I have just switched on my new 5s and gone through the intro sections. After entering my apple id, I selected the icloud update I wanted it to use (from my 4s). It is now stuck on the progress bar screen and has been for hours. Can I get it back to the original settings? I want to set it up as a new phone, until I can get on to a better wi-fi signal. Any help would be great.

    Greetings,
    I've never seen this issue, and I handle many iPads, of all versions. WiFi issues are generally local to the WiFi router - they are not all of the same quality, range, immunity to interference, etc. You have distance, building construction, and the biggie - interference.
    At home, I use Apple routers, and have no issues with any of my WiFi enabled devices, computers, mobile devices, etc - even the lowly PeeCees. I have locations where I have Juniper Networks, as well as Aruba, and a few Netgears - all of them work as they should.
    The cheaper routers, Linksys, D-Link, Seimens home units, and many other no name devices have caused issues of various kinds, and even connectivity.
    I have no idea what Starbucks uses, but I always have a good connection, and I go there nearly every morning and get some work done, as well as play.
    You could try changing channels, 2.4 to 5 Gigs, changing locations of the router. I have had to do all of these at one time or another over the many years that I have been a Network Engineer.
    Good Luck - Cheers,
    M.

  • When should SELECT ..FOR UPDATE be used?

    DB Version:10gR2
    This is what 10gR2 PL/SQL documentation says about SELECT...FOR UPDATE
    With the SELECT FOR UPDATE statement, you can explicitly lock specific rows of a
    table to make sure they do not change after you have read them. That way, you
    can check which or how many rows will be affected by an UPDATE or DELETE
    statement before issuing the statement, and no other application can change the
    rows in the meantime
    But i don't see SELECT...FOR UPDATE much in our production codes. Is SELECT ..FOR UPDATE used when huge amount of rows need to be updated?

    Its maily used for locking table with differnt node , commely this concpet where used in
    ERP products, bcoz different user can acces same table at time and do some DML
    operation, using FOR UPDATE will protected ,
    before that read this
    hb venki

  • Select single for update not creating lock on entry

    Hi All,
    I want to update an entry from MARA table so what ii did is
    select single for update *   from mara where matnr = 'ZYS'.
    The for update is supposed to create a lock  for me and the the lock is released after the commit or roll back.
    I'm running this prg in debug mode and after the statment
    select single for update *   from mara where matnr = 'ZYS''.
    I opened MM02 for this material and could change the data which i'm not supposed to be as there is lock on this particular mater from single for update statemnt
    or
    I even tried from other program
    select single  *   from mara where matnr = 'ZYS'. in debug mode and it returned a sy-subrc eq 0.
    and i don't see a lock in SM12 FOR SELECT * FOR UPDATE.....
    Can anyone clarify on this
    Thanks
    David

    Hi All,
    I knew we can create a lock object but thought to give a shot without creating lock object.
    So i'm just curious to know when we can use select * for update
    F1 Help says select * for update creates an E type lock- which will prevent other locks(X AND S type locks) overwriting or even reading that entry
    Also ,
    can someone throw some light DB lock and SAP Lock ( is this same as SAP LUW and DB LUW)
    Thanks
    David

  • HT1386 Why when I select not to update the calendar with any entry older than 1 day it ignors that and goes back as far as my calendar has entries

    Why when I select not to update the calendar with any entry older than 1 day it ignors that and goes back as far as my calendar has entries

    FCPx answer: At least the answer that worked for me...
    I had the same problem, as I believe many others had.
    In my case the problem was with one of OSX Mavericks' new features: APP NAP.
    It looks like this is ON by DEFAULT for ALL Applications and that FCPx, or Compressor for that matter, will not work if this is enabled on them.
    I just disabled App Napp on FCPx and now it works, again, like a charm.
    This is how you disable App Nap: http://reviews.cnet.com/8301-13727_7-57612009-263/how-to-disable-app-nap-in-os-x -mavericks/
    I hope this helps,
    Mariano

  • HT201210 Error 37 after selecting 'Restore and Update' from iTunes

    Error 37 after selecting 'Restore and Update' from iTunes. 
    3GS had been working perfectly fine. 
    Now it's a Brick. 
    <Edited by host>

    Update and restore error messages on iPhone and iPod touch

  • HT5706 To the above Step 1, "Select Settings General Update Software"; starting in the finder window of my Apple Air laptop (with version 10.8.5 OSX), how do I get to the starting point "Settings"?

    Starting from the finder window of an Air model laptop (OSX 10.8.5) were do I find 'Select Settings' to access review and update of the Apple TV hardware I just spent eight hours trying to set up?

    I believe those instructions are for the Apple TVand not the MacBook Air.

  • Use of Select stmts with Update stmts

    Hi,
    I want to execute the following update stmt...
    UPDATE Test1_tab a
    SET a.invpln_seq_no = (SELECT b.seq_no
    FROM Test2_tab b
    WHERE b.contract_id = a.contract_id
    AND b.date_from = a.period_from_date)
    WHERE a.invpln_seq_no != (SELECT b.seq_no
    FROM Test2_tab b
    WHERE b.contract_id = a.contract_id
    AND b.date_from = a.period_from_date)
    Here the problem is I have used 'SELECT b.seq_no FROM Test2_tab b WHERE b.contract_id = a.contract_id AND b.date_from = a.period_from_date' twice, one in the SET clause and the other one in WHERE clause...But it is the same SELECT stmt...So this same Select stmt runs twice when I run the whole update stmt I guess. Is there a way to do the above update, where it runs the SELECT stmt only once...
    Any input is highly appreciated...
    Thanks And Best Regards,
    /Dinesh...

    Then you can use Merge statement instead.
    try this
    merge into test1_tab t1
    using
    (SELECT a.rowid,b.seq_no SEQ_DEST
    FROM test1_tab a,test2_tab b
    WHERE b.contract_id = a.contract_id
    AND  b.date_from = a.period_from_date
    AND a.invpln_seq_no != b.seq_no) t2
    ON (t1.rowid=t2.rowid)
    WHEN MATCHED THEN
    UPDATE SET t1.invpln_seq_no=t2.seq_destor if you are sure that sno is a unique key with out any duplicates and if you are going to perform one time manual update then you can use an undocumented hint /*+ bypass_ujvc */ to do this.
    *DO NOT include this code if you are about to add it in a production procedure or a function
    UPDATE /*+ bypass_ujvc */ (SELECT a.invpln_seq_no SEQ_SRC,b.seq_no SEQ_DEST FROM test1_tab a,test2_tab  b
    WHERE b.contract_id = a.contract_id
    AND  b.date_from = a.period_from_date
    AND a.invpln_seq_no != b.seq_no)
    SET SEQ_SRC = SEQ_DESTRegards,
    Prazy

  • TX - row lock contention in SELECT query without update clause

    Hi,
    We are having problem in one of our application on production. The ASH report shows 'eq: Tx row lock contention' for only Select statements. There is no FOR UPDATE in the select statements. The exact statement is
    enq: TX - row lock contention : SELECT COUNT (1) FROM Table1 WHERE col1= :1 AND col2= :1 AND col3= :1 AND ROWNUM = 1
    enq: TX - row lock contention : SELECT MODULE_CD , MSG_DESC , SEVERITY FROM GS_ERROR_MSG WHERE MSG_NUM = :1
    I don't know why the select are locking the table rows and resulting in waits..
    Our environment is Oracle 10g and Forms & Reports..
    Please help.
    -- Prashant

    Hi,
    are you sure that there is no dml against the tables?
    You can query v$active_session_history (eg column BLOCKING_SESSION) to see which session locked the row.
    HTH..
    - wiZ

  • Error occuring in SELECT statement FOR UPDATE when using WITH cluase

    Hi,
    Iam using oracle 11g version
    Iam having a query with fetch records for update insid with cluase as
    cursor c1 is
    With abc as (
    SELECT col1,col2 from table1, table2 where < condition>
    for update of <col1> skip locked
    select * from abc,table3 where <condition>
    union all
    select * from table4 where id not in (select * from abc)
    if i add "for update of <col1> skip locked" this cluase
    iam getting error ORA-00907: missing right parenthesis
    when i remove "for update of <col1> skip locked" the block is working fine..
    can i know why error is occuring i

    Hi, first of all, your placing of "for update" clause is wrong. It should be at the end like:
    With abc as (
    SELECT col1,col2 from table1, table2 where < condition>
    select * from abc,table3 where <condition>
    union all
    select * from table4 where id not in (select * from abc)
    for update of <col1> skip locked;Second, if you try to compile it with this syntax then you will get a new error.
    ORA-01786:     FOR UPDATE of this query expression is not allowed
    Cause:     An attempt was made to use a FOR UPDATE clause on the result of a set expression involving GROUP BY, DISTINCT, UNION, INTERSECT, or MINUS.
    Action:     Check the syntax, remove the FOR UPDATE clause, and retry the statement.So, you will not be successful with the union clause in your query.

  • Use of SELECT SINGLE FOR UPDATE

    Hello,
    Am I missing something with FOR UPDATE addition in Open SQL? When I use it I can't get a lock to appear to SM12. For example, if I select a single record for update from table EKKO it's still possible to effect a normal MEPO update of the releavnt purchase order even before a COMMIT WORK command in my code.
    Any ideas?
    Thanks,
    Chris.

    Hello Chris
    FOR UPDATE command creates only a database lock,
    you can't see database locks in sm12.
    You should use related "lock object"  to lock entries in table EKKO with sm12
    For finding the lock object follow steps below :
    - Go to transaction se11
    - Write your table EKKO
    - Click button : Where Used List
    - Choose only "Lock object" in the next popup screen
    Every lock object has two function module ENQUEUE_<LOCK OBJ NAME> and DEQUEUE_<LOCK OBJ NAME>
    enqueue function creates lock in sm12 and dequeue deletes lock entry from sm12.
    And you can display parameters from se37 and use it.
    I hope it helps.

  • 'Missing select' error for update statement using WITH clause

    Hi,
    I am getting the below error for update statement using WITH clause
    SQL Error: ORA-00928: missing SELECT keyword
      UPDATE A
      set A.col1 = 'val1'
         where
      A.col2 IN (
      WITH D AS
      SELECT col2 FROM
      (SELECT col2, MIN(datecol) col3 FROM DS
      WHERE <conditions>
        GROUP BY PATIENT) D2
      WHERE
      <conditions on A.col4 and D2.col3>

    Hi,
    The format of a query using WITH is:
    WITH  d  AS
        SELECT  ...  -- sub_query
    SELECT  ...   -- main query
    You don't have a main query.  The keyword FROM has to come immediately after the right ')' that ends the last WITH clause sub-query.
    That explains the problem based on what you posted.  I can't tell if the real problem is in the conditions that you didn't post.
    I hope this answers your question.
    If not, post a complete test script that people can run to re-create the problem and test their ideas.  Include a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all the tables involved, and the results you want from that data.
    In the case of a DML operation (such as UPDATE) the sample data should show what the tables are like before the DML, and the results will be the contents of the changed table(s) after the DML.
    Explain, using specific examples, how you get those results from that data.
    Always say what version of Oracle you're using (e.g. 11.2.0.2.0).
    See the forum FAQ: https://forums.oracle.com/message/9362002

  • How to convert this select statement into update

    Hai All
    I have two table Namely Daily_attend , Train_mast
    Daily_attend Consist Of fields are Train_mast consist Of fields are
    Name varchar Train no var
    Empcode Num T_date date
    Intime Date Train_name var
    Outtime date Late_hrs var
    IND_IN Number
    IDE_OUT Number
    Attend_date date
    I need to update IDE_IN In Daily_attend table Depend upon late_hrs in the Train_mast table
    I have got Through in select statement This is my select statement
    select to_number(TO_DATE(TO_CHAR(Intime,'DD-MON-YYYY')||' '||
    TO_CHAR(0815,'0000'),'DD-MON-YYYY HH24:MI')+late_hrs/(24*60)-intime
    ) * 24*60 from dail_Att,train_mast;
    How can i convert it to update
    Any help is highly appricateable
    Thanks In Advance
    Regards
    Srikkanth.M

    Srikkanth,
    Try this code. And 1 more thing, i can't see any WHERE condition to join between the 2 tables DAIL_ATT, TRAIN_MAST.
    UPDATE DAIL_ATT A SET A.IDE_IN = (SELECT TO_NUMBER(TO_DATE(TO_CHAR(INTIME, 'DD-MON-YYYY')|| ' ' || TO_CHAR(0815, '0000'), 'DD-MON-YYYY HH24:MI') + LATE_HRS / (24 * 60) - INTIME) * 24 * 60 FROM TRAIN_MAST B WHERE <condition>);Regards,
    Manu.
    If my response or the response of another was helpful or Correct, please mark it accordingly

  • Automatic change of data selection for full update

    Hi,
    I have created datasource for full update and load data mannulay on weekly basis by date selection in infopackage.
    If I want to schedule the job on weekly basis, is there any way to change the date selection in infopackage for each data load.
    i.e. if the previous range was 1st to 7th for full update, then next it should become 8th to 14th.
    Regards,
    Pravin.

    Hi,
    Go to that infopackage - dataselection tab - calday field - there will you will  type  (near to TO value ) - there enter 6 for abap routine and give the name..
    Eg :
    data: l_idx like sy-tabix.
              read table l_t_range with key
                   fieldname = 'XXXXXX'.  " XXX- techname
                  l_idx = sy-tabix.
    data : date_low like sy-datum,
            date_high like sy-datum.
            date_high = sy-datum - 1.
            date_low  = sy-datum - 5.
            l_t_range-low = date_low.
            l_t_range-high = date_high.
            l_t_range-sign = 'I'.
            l_t_range-option = 'BT'.
              modify l_t_range index l_idx.
              p_subrc = 0.
    Here in the above eg , we are loading daily data from the interval of last 5 days.. ( from 03.02 to 07.02.08)
    Hope you have got an idea..
    Regards,
    Siva.

Maybe you are looking for