Update with subquery

I want to update a table with data extracted from a xmltype.
But Oracle extracts only the first value from the subquery.
Am I missing something or this is the expected output...
drop table t
create table t(id number, val varchar2(20));
insert into t(id, val) values (1, null)
insert into t(id, val) values (2, null)
select *
from t
ID     VAL
1     
2     
update t set(val) =
(select t.id from dual)
where id = t.id
select *
from t
ID     VAL
1     1
2     2
update t set(val) =
(select extractValue(
               extract(xmltype('<root>'||
                                   '<node id="1">'||
                                   '<value>1</value>'||
                                   '</node>'||
                                   '<node id="2">'||
                                   '<value>2</value>'||
                                   '</node>'||
                                   '</root>'),
                         '//root/node[@id='||to_char(t.id)||']'),
               '/node/value') from dual)
where id = t.id
select *
from t
ID     VAL
1     1
2     1 -- <-- should be 2!

Hi,
try this way:
SQL> CREATE TABLE tab AS SELECT 1 id,cast(NULL AS VARCHAR2(20)) val FROM dual UNION ALL
  2                      SELECT 2,NULL FROM dual;
Table created.
SQL> DESC tab;
Name                                      Null?    Type
ID                                                 NUMBER
VAL                                                VARCHAR2(20)
SQL>
SQL> SELECT * FROM tab;
        ID VAL                                                                 
         1                                                                     
         2                                                                     
SQL>
SQL> UPDATE tab t
  2  SET val=(
  3      SELECT
  4         extractValue(
  5            extract(
  6               XMLType('<root>
  7                          <node id="1">
  8                            <value>1</value>
  9                          </node>
10                          <node id="2">
11                            <value>2</value>
12                          </node>
13                        </root>')
14               ,'/root/node[@id='||id||']')
15            ,'/node/value') va
16      FROM tab s
17    WHERE t.id=s.id) ;
2 rows updated.
SQL> SELECT * FROM tab;
        ID VAL                                                                 
         1 1                                                                   
         2 2                                                                   
SQL> spool off;btw, you don't need to || every line in xml, and you don't need to do to_char() when using ||
Ants

Similar Messages

  • Merge Vs Update with Subquery

    What is the main difference between Merge Statment(only update) with simple Update statement based on Select query ?
    Which one is faster ?

    There are no vanilla answers to such a question especially when asked by someone who doesn't have time to post version number or DML.
    The only honest answer ... build a test environment and test it using EXPLAIN PLAN and SET TIMING ON.
    That said: A merge statement should only be used where it is appropriate.

  • Update with subquery in TopLink ?

    I want to activate the user account updating its status to ACTIVE.
    UPDATE PujUserEntity as user SET user.status.id = (SELECT status.id from PujUserStatusEntity status WHERE status.tag='ACTIVE') WHERE user.login= :login AND user.email= :email
    the error during the deployment on Glassfish (TopLink):
    CLI171 Command deploy failed : Deploying application in domain failed;
    Exception Description: Syntax error parsing the query [activateUserAccount: UPDATE PujUserEntity as user SET user.status.id = (SELECT status.id from PujUserStatusEntity status WHERE status.tag='ACTIVE') WHERE user.login= :userLogin AND user.email= :userEmail], line 1, column 52: unexpected token [SELECT].
    Internal Exception: line 1:52: unexpected token: SELECT

    According to JPA specification you are not allowed to use subquery in that place. In SET part you can only use simple_arithmetic_expression | string_primary | datetime_primary | boolean_primary | enum_primary | simple_entity_expression | NULL (JSR 220 Java Persistence API specification page 104). You should be able to use native query, or find status.id first and then just pass it as parameter.

  • Insert / update with subquery

    Hi,
    i have to insert (if records don't exist) or update (if records already exist) records from table S into table D. S and D have the same structure.
    Table S
    ID NUMBER, Primary Key,
    DESC1 VARCHAR
    DESC2 VARCHAR
    Table D
    ID NUMBER, Primary Key,
    DESC1 VARCHAR
    DESC2 VARCHAR
    Are this statements OK?
    INSERT INTO D
    ( SELECT * FROM S T1 WHERE NOT EXISTS
    ( SELECT '1' FROM D T2 WHERE T2.ID = T1.ID));
    UPDATE D T1
    SET (DESC1, DESC2) =
    (SELECT DESC1, DECS2
    FROM S T2 WHERE T1.ID = T2.ID ) WHERE EXIST
    ( SELECT '1' FROM S T3 WHERE T1.ID = T3.ID );
    Thanks a lot

    Depends on exist or not the data in
    the destination where id doesn't exists in the source.
    SQL> select * from updatable_tab;
    ID DESC1 DESC2
    1 0 0
    1 0 0
    1 0 0
    1 0 0
    2 f f
    2 f f
    2 f f
    2 f f
    2 f f
    9 rows selected.
    SQL> update updatable_tab dst set (desc1, desc2) =
    2 (select desc1, desc2 from s where s.id = dst.id)
    3 /
    9 rows updated.
    SQL> select * from updatable_tab;
    ID DESC1 DESC2
    1 A A
    1 A A
    1 A A
    1 A A
    2 B B
    2 B B
    2 B B
    2 B B
    2 B B
    9 rows selected.
    But if you have the row in the destination table
    wich does not exists in the source table, it will
    nullify your dest1 and dest2.
    In this case you should of course:
    SQL> select * from updatable_tab;
    ID DESC1 DESC2
    1 0 0
    1 0 0
    1 0 0
    1 0 0
    2 f f
    2 f f
    2 f f
    2 f f
    2 f f
    3 m m
    10 rows selected.
    SQL> select * from s;
    ID DESC1 DESC2
    1 A A
    2 B B
    SQL> update updatable_tab dst set (desc1, desc2) =
    2 (select desc1, desc2 from s where s.id = dst.id)
    3 where exists (select null from s where s.id = dst.id)
    4 /
    9 rows updated.
    SQL> select * from updatable_tab;
    ID DESC1 DESC2
    1 A A
    1 A A
    1 A A
    1 A A
    2 B B
    2 B B
    2 B B
    2 B B
    2 B B
    3 m m
    Rgds.

  • Update with exists and subquery

    Hi how to pass index to this sql
    Table pap ( eqid,seq,histseq,docno,status,value1,value2)
    PK(eqid,seq,histseq) and indx (docno)
    table pa ( eqid,seq,docno,status,value1,value2)
    PK(eqid,seq) and indx (docno)
    update pa
    set (value1,value2) = (select pap.value1,pap.value2 from pap
    where pap.eqid = pa.eqid
    and pap.seq = pa.seq
    and pap.histseq =1
    and pap.docno <> pa.docno
    and pap.status = 4 )
    where pa.status=1
    and exists ( select 1 from pap
    where pap.eqid = pa.eqid
    and pap.seq = pa.seq
    and pap.histseq =1
    and pap.docno <> pa.docno
    and pap.status =4 )
    It is doing fulltable scan on pa and using hash join .
    How to write update with subquery method also ?

    There's nothing wrong with a full scan.
    Please read this explanation/example: http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:9422487749968
    If you want more explanation then please follow these guidelines below, so we have sufficient inputs:
    [When your query takes too long...|http://forums.oracle.com/forums/thread.jspa?messageID=1812597#1812597]
    [How to post a SQL statement tuning request|http://forums.oracle.com/forums/thread.jspa?threadID=863295&tstart=0]
    Remember to put the tag befor and after the examples you post.
    See http://forums.oracle.com/forums/help.jspa regarding tags.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • Update column with subquery returning more than one row

    Hi Everybody,
    Please let me know how to handle this. I am writing update statement in procedure with subquery and it is returning multiple rows. Please help me, how i can handle this :
    UPDATE TABLEA A
    SET A.ERROR_MESSAGE = (Select B.XERROR_MESSAGE from TABLEB B, TABLEA A WHERE B.id = A.id)
    WHERE A.id = (Select B.id from TABLEB B, TABLEA A WHERE B.id = A.id);
    (Select B.XERROR_MESSAGE from TABLEB B, TABLEA A WHERE B.id = A.id) --- This subquery is returning more than one rows. How i can handle this in Pl/SQL?
    Please let me know. I will be very thankful to you all.
    I will really appreciate your replies and comments.
    Thank you

    Try getting rid of tablea in your subqueries. You already have it in the UPDATE statement.
    UPDATE TABLEA A
    SET    A.ERROR_MESSAGE = (Select B.XERROR_MESSAGE
                              from   TABLEB B
                              WHERE  B.id = A.id
    WHERE  A.id = (Select B.id
                   from   TABLEB B
                   WHERE  B.id = A.id);You can also try a simple MERGE:
    merge into tablea a
    using tableb b
    on    (a.id = b.id)
    when  matched then update
          set a.error_message = b.xerror_message;

  • Update with Outer Join

    Is it possible to do an update that involves an outer join on the table being updated?
    Here's what I mean - currently, I have something like:
    UPDATE table_1 t1
    SET col_1 =
    SELECT t2.col_2
    FROM table_2 t2
    WHERE t2.t1_key = t1.t1_key
    WHERE EXISTS
    SELECT t2.*
    FROM table_2 t2
    WHERE t2.t1_key = t1.t1_key
    UPDATE table_1 t1
    SET col_1 = 0
    WHERE NOT EXISTS
    SELECT t2.*
    FROM table_2 t2
    WHERE t2.t1_key = t1.t1_key
    Yes, I could do set all of the table_1.col_1 values = 0 first, and then do the first update, but it is inefficient given the number of records in the table that would be updated twice.
    Is there a way of combining these two updates into a single update statement?

    You could simply use your first update and omit the WHERE EXISTS clause since you want to update all the rows in table_1 anyway.
    If the subquery finds a match, it will update with the selected value. Normally, a non-match would set the column to a null but you can solve that with NVL:
    SQL> select * from table_1;
                  T1_KEY                COL_1
                       1                    1
                       2                    1
                       3                    1
                       4                    1
                       5                    1
                       6                    1
                       7                    1
                       8                    1
                       9                    1
    9 rows selected.
    SQL> select * from table_2;
                  T2_KEY                COL_2
                       1                    9
                       3                    9
                       5                    9
    SQL> UPDATE table_1 t1
      2  SET    col_1 = nvl (
      3                       (SELECT t2.col_2
      4                        FROM   table_2 t2
      5                        WHERE  t2.t2_key = t1.t1_key
      6                       ),0
      7                     )
      8  ;
    9 rows updated.
    SQL> select * from table_1;
                  T1_KEY                COL_1
                       1                    9
                       2                    0
                       3                    9
                       4                    0
                       5                    9
                       6                    0
                       7                    0
                       8                    0
                       9                    0
    9 rows selected.

  • Why update with subqueries does not have cost and cardinality?

    There is update.
    update test t1 set dummy = (select dummy from test2 t2 where t1.id = t2.id);Both tables which have actual statistics. Each has 1000 rows. And column ID have values from 1 to 1000 in each table.
    This is explain plan
    | Id  | Operation          | Name  | Rows  | Bytes | Cost (%CPU)| Time     |
    |   0 | UPDATE STATEMENT   |       |  1000 | 13000 |   426   (9)| 00:00:01 |
    |   1 |  UPDATE            | TEST  |       |       |            |          |
    |   2 |   TABLE ACCESS FULL| TEST  |  1000 | 13000 |   426   (9)| 00:00:01 |
    |*  3 |   TABLE ACCESS FULL| TEST2 |     1 |    13 |   426   (9)| 00:00:01 |
    Predicate Information (identified by operation id):
       3 - filter("T2"."ID"=:B1)We can see here, that Oracle consider subquery within update as once-executed subquery.
    This is runtime plan
    | Id  | Operation          | Name  | Starts | E-Rows | A-Rows |
    |   1 |  UPDATE            | TEST  |      1 |        |      0 |
    |   2 |   TABLE ACCESS FULL| TEST  |      1 |   1000 |   1000 |
    |*  3 |   TABLE ACCESS FULL| TEST2 |   1000 |      1 |   1000 |
    Predicate Information (identified by operation id):
       3 - filter("T2"."ID"=:B1)Why first plan does not have cost in step 1?
    Does Oracle always not understand that update with subqueries will performed as NL or filter? In other words that step 3 will executed many times.
    Or it is my bug (or what?)?

    793769 wrote:
    Does Oracle always not understand that update with subqueries will performed as NL or filter? In other words that step 3 will executed many times.
    Or it is my bug (or what?)?It's not possible to say whether this is a bug or a deliberate choice.
    Because of "subquery caching" (see http://jonathanlewis.wordpress.com/2006/11/06/filter-subqueries/ ) the optimizer cannot predict how often the subquery will have to run. So possibly it shows nothing rather than showing the best or worst cases or a pure guess.
    Regards
    Jonathan Lewis

  • Cant open itunes. get error message 7 followed by error message windows error 127.     i have redownloaded new updates with luck.   window xp

    cant open itunes. get error message 7 followed by error message windows error 127.     i have redownloaded new updates with luck.   window xp

    For general advice see Troubleshooting issues with iTunes for Windows updates.
    The steps in the second box are a guide to removing everything related to iTunes and then rebuilding it, which is often a good starting point unless the symptoms indicate a more specific approach. Review the other boxes and the list of support documents further down page in case one of them applies.
    Your library should be unaffected by these steps but there is backup and recovery advice elsewhere in the user tip.
    tt2

  • I connected my external hard drive to my new imac to put all of the information from my macbook onto it. i have been working on the macbook now and want to reconnect the external hard drive so my imac is updated with the work. How is that done?

    I connected my external hard drive to my new imac to put all of the information from my macbook onto it. I have been working on the macbook now and want to reconnect the external hard drive so my imac is updated with the work. How is that done? or is it possible?

    As I said, I don't use Time Machine, so it'd be best to wait for an "expert" answer, but, with my very limited knowledge, I'd say: probably not - so, for now, I'd suggest you read up on Time Machine:
    http://web.me.com/pondini/Time_Machine/Home.html
    http://support.apple.com/kb/index?page=search&src=support_site.home.search&local e=en_US&q=time%20machine
    And, you might be attracting more knowledgeable answers if you were to post this question in the Time Machine Forum (part of the Snow Leopard forums).

  • Batch Characteristic not updating with result for linked MIC

    Hi,
    I have created a MIC and linked to a batch characteristic i.e. potency and included the characteristic in a specific batch class. This MIC is part of the inspection plan for a certain material.
    If I generate a lot of origin 01 and 09 for the material and record a result for the MIC and apply a UD, the batch characteristic is not updated with the result recorded in the batch master record..
    If I generate a lot origin 89 for the same material, when the result is recorded for the MIC and apply a UD the batch characteristic is updated with the mic value in the batch master record.
    Can you help regards?

    Ok. I am a little bit further.
    I put a trace (ST05) on the result recording and I find a difference between 4.7 and ECC6.0.
    In ECC6.0 I've an deletion in table "AUSP". This deletion isn't performed in the 4.7 system.
    Hope that this can help to solve my problem.
    Regards,
    René
    @ Mayank
    Above, all the master data and customizing is mentioned to transfer the result to the batch classification
    Edited by: Rene Fuhner on Jul 30, 2010 7:03 PM

  • How do you transfer voice memos to your pc?  I have synced my phone a number of times and everything updates with the exception of voice memos?

    How do you transfer voice memos to your pc?  I have synced my phone a number of times and everything updates with the exception of voice memos? Also, my voice memos are checked to be synced everytime my phone is synced. 

    Voice memos, once correctly synced, will appear under your Music Library.  Can you go through your music library to check?

  • Problems with software the update with Nokia 6233

    Problems with software the update with Nokia 6233
    Hello,
    I have so far some-paints tries my software to update.
    Each time the update procedure begins, sometime stands then on the telephone “test mode”.
    On the computer wars I the reference in the update manager “connection to the telephone broken off” and wants the update to again start.
    If I change my profile attitudes since that for example, then after a telephone restart again deleted.
    What there can I make?
    GreetingsMessage Edited by adisaily on 24-Aug-2007 09:03 AM

    It sounds like your 5800 has the earpiece fault that was common with 5800's made before February.
    You need to take it to a nokia care point for repair under warranty.
    Care points/service centres and repair info:
    UK • Europe • Asia-Pacific • USA •
    Canada • Middle East and Africa
    Elsewhere: Click here, select your country, go to the support section, then select repair.

  • I have a column where I have implemented writeback, its working fine. On top of this I need to show 0 as No and 1 as yes in our report, that is also done. Now I want to enter Yes in a column where it was no and I want database table to get update with 1.

    I have a column where I have implemented writeback, its working fine. On top of this I need to show 0 as No and 1 as yes in our report, that is also done. Now I want to enter Yes in a column where it was no and I want database table to get update with 1. I am not sure how to do it. SOmeone please help me out.

    Hi ,
    In your write back XML  try the below  query insert
    INSERT INTO TABLE_XYZ (attribute1)  values (SELECT CASE  WHEN @{C1}=’Yes’ then 1 when @{C1}=’No’ then 0 else null end from dual)
    Regards
    Rajagopal

  • How to get my custom controller updated with the global custom controller?

    Hi all,
    I'm new to CRM Web UI and need some advice from the expert. Currently I'm working on component ICCMP_BT_DATES and noticed something weird with this component. When this component is first launch it display the dates of a service ticket correctly. However when I navigate to another screen, save a new ticket and back, the dates are not reflected. When I went in and debug the component, I noticed that the context is still tied to the previous ticket. I think the custom controller is not updated with the latest from the global custom controller.
    My question is how do I get my custom controller updated with the latest.
    Regards,
    Ricky

    You have to bind your custom controllers context node to the event NEW_FOCUS of the collection wrapper on the global custom controller.
    Best place to do this might be the CONNECT_NODES of the context of your custom controller.
    Get the global CuCo with GET_CUSTOM_CONTROLLER() and then the appropriate context node. Now:
    SET HANDLER yourMethod for lr_global_cuco->typed_context->thecontextnode->collection_wrapper activation iv_activate.
    Of course you have to implement a method similar to ON_NEW_FOCUS as it is on many other nodes.
    cheers Carsten

Maybe you are looking for

  • On demand not updating

    there are no new episodes for ANY tv show on any network in the On Demand section since SUNDAY oct 11.   anyone have any idea what to do to fix? who to call?

  • Safari is crashing immediately after launch

    Since the tribute to Steve's memory is integrated in the main frame, Safari crasches immediately after launch. - iMac 3.06 GHz Intel Core 2 Duo - OS X 10.7.5

  • Having trouble deleting movie files from itunes

    I'm curious as to how I delete movies from itunes. I imported these clips of a movie into itunes and would now like to remove them but i can't seem to do that. I tried deleting the files off my hard drive. itunes recognizes that i deleted them but th

  • Suddenly unable to apply custom icon to system drive?

    I've loved using custom drive icons for years, and have routinely changed them to suit my moods. Today, no matter what I try, I cannot change it, and in fact the attempts to apply a new icon have removed my old one, and I now have the standard system

  • Sharing a FAT32-formatted MP3 player between two MACs

    I have an old iRiver H140 MP3 player (still going strong) that I want to mount on two mac machines (a mini and a mbp) over a network. Currently, only one machine sees it (mini that is attached to it). The mbp is able to mount the the mini's hdd but n