Update statement with nested selects and alias usage

Hello, we are trying to build an update statement like this...
update table1 t1
set attr1 = ( select someattr
from (nested select 1) as aux1
(nested select 2 (nested select 2.1 + nested select 2.2)) ) as aux2
where some_join_clauses
where t1.attr2 = 123
and t1.attr3 = 'abc'
Alias t1 shound be known at the level of nested select 1,2 and 2.1, 2.2? We are receiving an error message saying that t1.someattr is an invalid identifier. Can you help? Thanks!

mauramos wrote:
Hello, we are trying to build an update statement like this...
update table1 t1
set attr1 = ( select someattr
from (nested select 1) as aux1
(nested select 2 (nested select 2.1 + nested select 2.2)) ) as aux2
where some_join_clauses
where t1.attr2 = 123
and t1.attr3 = 'abc'
Alias t1 shound be known at the level of nested select 1,2 and 2.1, 2.2? We are receiving an error message saying that t1.someattr is an invalid identifier. Can you help? Thanks!You can only reference elements nested 1 level deeper in a correlated update statement.
I'd suggest you try with the MERGE statement, or alternatively, post some sample data (insert statements) and table DDL (create statements), with an 'expected output' description and we can try to help you build a suitable statement.

Similar Messages

  • Update Statement w/ Order By and other goodies

    this is a continuation of my previous post : Re: create update statement with joins/select Please see it for table structure and sample data.
    Ok so this is building on my previous SQL question that was resolved by you brilliant SQLers out there. the below code will do exactly what i need it to do, but I was wanting less user interaction:
    update offender_case_identifiers ociq
       set Identifier_type = 'ARN2'
    where ociq.offender_book_id in
          (select offender_book_id from v_header_block
            where offender_id_display = :ppid)
        and ociq.comment_text != :parn
       AND ociq.identifier_type = 'ARN'I was only wanting the user to have to enter the ":pid" and have the script take care of the rest. I have found the best way to ensure that I'm finding the most recent "ARN" from the offender_case_identifiers (which is then going to need to be set to ":parn") is to run the following script:
    select oci.identifier_type
    , vhb.offender_id_display
    ,oci.comment_text
    ,vhb.offender_book_id
    ,oci.offender_book_id
    from   offender_case_identifiers oci, v_header_block vhb
    where
    oci.offender_book_id = vhb.offender_book_id
    and vhb.offender_id_display = :ppid
    and oci.identifier_type like 'ARN'
    order by comment_text descThe above code was the original starting point for my previous final SQL statement, listed first in this post, however, I have added the "ORDER BY" at the bottom of it. For additional sample data, here is the result of the above statement:
    IDENTIFIER_TYPE     OFFENDER_ID_DISPLAY     COMMENT_TEXT     OFFENDER_BOOK_ID     OFFENDER_BOOK_ID_1
    ARN     *0000382183*     01550228     *1,011,683*     1,011,683
    ARN     *0000382183*     01550228     *1,011,683*     1,011,683
    ARN     *0000382183*     01531810     *823,281*     823,281
    ARN     *0000382183*     01531810     *823,281*     823,281
    ARN     *0000382183*     01531810     *823,281*     823,281
    ARN     *0000382183*     01531810     *823,281*     823,281
    ARN     *0000382183*     01529717     *804,301*     804,301
    ARN     *0000382183*     01525871     *759,982*     759,982
    ARN     *0000382183*     01525871     *759,982*     759,982
    ARN     *0000382183*     01516263     *688,100*     688,100
    ARN     *0000382183*     01516263     *688,100*     688,100
    ARN     *0000382183*     01516263     *688,100*     688,100
    ARN     *0000382183*     01515789     *682,680*     682,680
    ARN     *0000382183*     01510809     *624,140*     624,140
    ARN     *0000382183*     01507681     *590,261*     590,261
    ARN     *0000382183*     01507681     *590,261*     590,261
    ARN     *0000382183*     01507681     *590,261*     590,261
    ARN     *0000382183*     01484993     *454,927*     454,927
    ARN     *0000382183*     01484993     *454,927*     454,927
    ARN     *0000382183*     01484993     *454,927*     454,927
    ARN     *0000382183*     01477592     *448,231*     448,231
    ARN     *0000382183*     01477592     *448,231*     448,231
    ARN     *0000382183*     01456544     *428,902*     428,902
    ARN     *0000382183*     01456544     *428,902*     428,902
    ARN     *0000382183*     01429778     *403,175*     403,175
    ARN     *0000382183*     01427677     *401,055*     401,055
    ARN     *0000382183*     01427677     *401,055*     401,055
    ARN     *0000382183*     01427677     *401,055*     401,055
    So, as you can see, I want the query to select from the above results, the top/1st record of "Comment_Text" and set it to the ":parn" WITHOUT prompting the user to enter it. Then I want the script to change all other "Identifier_Type" to 'ARN2' that do not share the first "Comment_Text"....so in this example, I would want all 'ARN' changed to 'ARN2' that do not have "Comment_text" = 01550228.
    I hope this isnt too confusing, because it is to me. Thanks in advance for all the help.

    Hi,
    So you have a query that shows all rows, and you want to UPDATE the rows in that reuslut set that do not have the greatest value of comment_text. You can use the analytic DENSE_RANK function to number the distinct comment_texts, and then discard the rows that were assigned #1. the remained will be the ones you wqant to UPDATE.
    Here's one way to do that:
    UPDATE     offender_case_idetifiers
    SET     identifier_type     = 'ARN2'
    WHERE     (offender_book_id, comment_text)
         IN (
                WITH    got_r_num    AS
                    SELECT  vhb.offender_book_id
                 ,        oci.comment_text
                 ,        DENSE_RANK () OVER (OREDER BY oci.comment_text  desc)
                                 AS r_num
                 FROM    offender_case_identifiers oci
                 ,         v_header_block           vhb
                 WHERE   oci.offender_book_id      = vhb.offender_book_id
                 AND         vhb.offender_id_display   = :ppid
                 AND        oci.identifier_type           = 'ARN'
                SELECT  offender_nook_id
                ,       comment_text
                FROM    got_r_num
                WHERE   r_num     > 1
    AND     identifier_type     = 'ARN'
    I hope this answers your question.
    If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables involved, and also post the results you want from that data.
    If you're asking about a DML statement, such as UPDATE, the sample data will be the contents of the table(s) before the DML, and the results will be state of the changed table(s) when everything is finished.
    Explain, using specific examples, how you get those results from that data.
    Always say which version of Oracle you're using (e.g., 11.2.0.2.0).
    See the forum FAQ {message:id=9360002}

  • Creating a Dynamic Update Statement based on Select

    hi,
    i'm trying to create a dynamic update statement based on select statement
    my requirment is to query a joint tables and get the results then based on the results i need to copy all the data and create an update statement for each row
    for ex
    the update statement should look like this
    update iadvyy set SO_SWEEP_CNT = '1' where inst_no = '003' and memb_cust_no = 'aaaaaaaaaaaaaaaa';
    and the select statement like the following
    select substr(key_1,11,9) account_no,sord_mast SO_SWEEP_CNT from
    select acct_no,count(*) sord_mast from
    (select from_acct_no acct_no,update_mast
    from sord where FROM_SYS in ('DEP','INV') and TERM_DATE > 40460
    union all
    select to_acct_no acct_no,update_mast
    from sord where TO_SYS in ('DEP','INV') and TERM_DATE > 40460)
    group by Acct_no)
    right outer join
    invm
    on
    key_1 = '003'||acct_no
    where sord_mast > 0;
    so taking the above two columns from the above select statement and substitue the values as separate update statement.
    is that doable , please share your knowledge with me if poosible
    thanks in advanced

    is that doable , please share your knowledge with me if poosibleyes
    The standard advice when (ab)using EXECUTE IMMEDIATE is to compose the SQL statement in a single VARCHAR2 variable
    Then print the SQL before passing it to EXECUTE IMMEDIATE.
    COPY the statement & PASTE into sqlplus to validate its correctness.

  • Update statement with joining other tables

    Hi ,
    I have two table one is containing xml file , basically i need to read from those xml file then update to another table based on some condition.
    UPDATE TRCB_XBRL_STG_2 STG
    SET PROFIT =
      case when xbrl.isconsolidatedacc='Y' and EXTRACTVALUE(XBRL.XBRLFILE,'//PROFIT ', 'xmlns:acra="..."') is not null
      THEN EXTRACTVALUE(XBRL.XBRLFILE,'//PROFIT ', 'xmlns:acra="..."')
      WHEN XBRL.ISCONSOLIDATEDACC='N' AND EXTRACTVALUE(XBRL.XBRLFILE,'//PROFIT ', 'xmlns:acra="..') IS NOT NULL
      THEN extractValue(XBRL.xbrlfile,'//PROFIT ', 'xmlns:acra=".."')
      ELSE STG.PROFIT
      END,
      SET REVENUE=
      case when xbrl.isconsolidatedacc='Y' and EXTRACTVALUE(XBRL.XBRLFILE,'//REVENUE', 'xmlns:acra="..."') is not null
      THEN EXTRACTVALUE(XBRL.XBRLFILE,'//REVENUE.', 'xmlns:acra="..."')
      WHEN XBRL.ISCONSOLIDATEDACC='N' AND EXTRACTVALUE(XBRL.XBRLFILE,'//REVENUE', 'xmlns:acra="..') IS NOT NULL
      THEN extractValue(XBRL.xbrlfile,'//REVENUE', 'xmlns:acra="REVENUE"')
      ELSE STG.REVENUE
      END,
      ... (around 100 columns)
    FROM  TRCB_XBRL xbrl ,TRCB_XBRL_STG_2 STG
    WHERE STG.XBRL_ID = XBRL.XBRL_ID Number of columns are around 100 , please anyone suggest how to use update statement with joining two tables.

    Hi,
    If all the values needed to update a given row of table_x are coming from the same row of table_y (or from the same row of a result set of a query involving any number of tables), then you can do something like this:
    UPDATE  table_x  x
    SET     (col1, col2, col3, ...)
    =     (
             SELECT  NVL (y.col1, x.col1)
             ,         NVL (y.col2, x.col2)
             ,         NVL (y.col3, x.col3)
             FROM    table_y  y
             WHERE   x.pkey   = y.expr
             AND         ...
    WHERE   ...
    ;If the WHERE clause depends on the same row of table_y, then it will probably be simpler and more efficient to use MERGE instead of UPDATE.
    I hope this answers your question.
    If not, post 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 {message:id=9360002}

  • Update statement with inner join

    Hello everyone. I am am trying to do an update statement with an inner join. I have found several examples of SQL statements that work with Sql server and mysql but they don't work in Oracle. Does anyone know the proper way in Oracle 10G? I am trying to update all fields in one table from fields in another table.
    for example:
    UPDATE table3
    SET
    TL3.name = TL2.name,
    TL3.status = TL2.status,
    TL3.date = TL2.date
    FROM table3 TL3 JOIN table2 TL2
    ON (TL3.unique_id = TL2.unique_id);
    any help will be appreciated.

    Hi,
    You can also use MERGE, like this:
    MERGE INTO  table3     dst
    USING   (
             SELECT  unique_id
             ,         name
             ,         status
             ,         dt          -- DATE is not a good column name
             FROM    table2
         )          src
    ON     (dst.unique_id     = src.unique_id)
    WHEN MATCHED THEN UPDATE
    SET     dst.name     = src.name
    ,     dst.status     = src.status
    ,     dst.dt          = src.dt
    ;Unlike UPDATE, this lets you avoid essentially doing the same sub-query twice: once in the SET clause and then again in the WHERE clause.
    Like UPDATE, you don't acutally join the table being changed (table3 in this case) to the other table(s); that is, the FROM clause of the suib-query does not include table3.
    Riedelme is right; you'll get better response to SQL questions like this in the SQL and PL/SQL forum:
    PL/SQL

  • My macbook is stuck on a grey screen with language select and every time i click english it brings me back to the same gray page what do i do?

    My macbook Pro is stuck on a grey screen with language select and every time i click english it brings me back to the same gray page with language select what do i do? Also i dont have enough monet to go to the apple store to get it fix and another thing this happen after i tried to factory reset it without the disck uising coomans i found on youtube.Lastly befor doing the comands my macbook would open any applications and the finder would just blink on and off on the dock.
    Heres the link http://www.youtube.com/watch?v=Q5e5thk0O9o

    Shut down your computer and disconnect all peripherals (keyboard & mouse if pertinent) from your computer.  Now reboot.
    If the Mac starts up normally, shut it down again and then plug in one of the peripherals (keyboard or mouse first) and start up your computer again.  If it does so successfully repeat the process, adding one peripheral at a time until your Mac acts up.  At that point, disconnect the last peripheral you added, reboot your Mac and search the peripheral vendor's website for an updated driver. 
    If no driver exists or the problem remain after installing the new driver, try a different cable or a different port on your Mac.
    If none of the above works, again disconnect all peripherals from your Mac, hold down the "shift" key to start up in "Safe Boot" mode. 
    If the Mac starts up correctly, restart without pressing the "shift" key.
    If your computer still does not start up properly, shut it down and restart it while holding down the Apple+Option-P-R keys; keep holding "all 4 keys" down until you hear the startup sound "twice."
    If none of the above work Disconnect all peripherals from your computer. Boot from your install disc & run Repair Disk from the utility menu. To use the Install Mac OS X disc, insert the disc, and restart your computer while holding down the C key as it starts up.
    Select your language.
    Once on the desktop, select Utility in the menu bar.
    Select Disk Utility.
    Select the disk or volume in the list of disks and volumes, and then click First Aid.
    Click Repair Disk.
    (If Disk Utility cannot repair, you will need a stronger utility (3rd party) - Diskwarrior or Techtool PRO)
    Restart your computer when done.
    Repair permissions after you reach the desktop-http://docs.info.apple.com/article.html?artnum=25751 and restart your computer.
    Remove any 3rd party ram.
    Reinstall Leopard - This will install a "fresh" copy Leopard without archiving old system files but leaves the rest of your files in place.
    If you still want to restore your computer to factory level...
    Start up from your install disc, go to Disk Utility and select the disk and click erase - to securely erase data click Security Options and Erase Free Space which will entirely wipe your disk, overwriting it with zeros so that no data is recoverable.
    To restore read the instructions in the Mac OS X v10.5 Leopard - Installation and Setup Guide  PDF

  • Acrobat 7 mandatory update failing with error 16820 and blocking the use of the program

    Acrobat 7 mandatory update failing with error 16820 and blocking the use of the program

    Sorry I can't get the screen print to upload
    It says
    Mandatory Update Available
    A Mandatory version of this application is available for download. Application can't continue without installing this version.
    Application:     Acrobat_com
    Installed version: 2.0.0.0
    Update Version : 2.3.0.0
    Download now                    Quit
    Release notes ( this is blank)
    After selecting 'Download Now' I get the following message;
         Download failed
         There was an error downloading the update. Error #16820
         Close

  • Update Statement with group by query

    I need to an update statement that updates the records return from this query:
    SELECT
    GROUP,
    SECTION,
    MAX(ASSIGNED_DATE)
    FROM
    ASSIGNMENTS
    GROUP BY GROUP, SECTION
    I need to update all those records and set HISTORY = 1
    I'm not sure how to form the update statement.
    UPDATE ASSIGNMENTS SET HISTORY = 1 WHERE (SELECT
    GROUP,
    SECTION,
    MAX(ASSIGNED_DATE)
    FROM
    ASSIGNMENTS
    GROUP BY GROUP, SECTION) ?
    Edited by: user9179751 on Apr 18, 2011 11:23 AM

    Just doing a select query: (these are the records that were updated when I ran the update statement you posted)
    SELECT * FROM ASSIGNMENS WHERE (GROUP, SECTION, ASSIGNED_DATE) IN (SELECT GROUP,
    SECTION, MAX(ASSIGNED_DATE)FROM ASSIGNMENTS GROUP BY GROUP, SECTION)
    Records:
    GROUP SECTION    ASSIGNED_DATE
    0010        050098       20011108
    0334    000003       19980116
    0505        320000       20000712
    0676        380000       19980914
    0676        067931       20011221
    0801        600500       19910409
    0801    601701       20090903These are the records I want updated:
    Which I would get using:
    SELECT GROUP, SECTION, MAX(ASSIGNED_DATE)FROM ASSIGNMENTS GROUP BY GROUP, SECTION
    GROUP SECTION ASSIGNED_DATE
    0002                  20100519
    0003                  20090724
    0005          
    0007       360000     
    0007                  20040913
    0009                  19970814
    0010       050098     20011108
    0010                  20000926
    0011          I'm assuming it skips over null values?

  • My ipod touch 5 wount let me back in after trying to update ios with my computer and now says connect to itunes. it still wount work when i plug it in and click restore for it?

    my ipod touch 5 wount let me back in after trying to update ios with my computer and now says connect to itunes. it still wount work when i plug it in and click restore for it?

    it went through the extracting software stage but then an error message saying that it encountered problems came up and it didn't restore.

  • Update statement with CASE and banding

    Hello Folks,
    I am stuck and am going nowhere.
    I have two tables
    MinuteCategory
    Minute
    MinuteCategory
    MinuteLookup
    MinuteCategoryId (surrogate key)
    MinuteLow
    MinuteHigh
    MinuteCategory
    Sample from Minute Lookup table ..
    1 1
    15 <15 min
    2 16
    30 <30 min
    3 31
    45 <45 min
    4 46
    60 <60 minutes
    5 61
    75 <1 hour 15 minutes
    I am trying to update the MinuteCategory column in the MinuteCategory table using the MinuteLookup table.
    For example if the minute = 33 then the corresponding lookup value would be 3 (from the lookup table), because the 
    33rd minute falls in between 31 min and 45 min and the corresponding surrogate key is 3
    Is this possible with an Update statement in SQL? The only thing i can think of is use case statement as the join key between
    the two tables but i don't think it's going to work :(
    UPDATE
    SET MinuteCategory = ml.MinuteCategoryId
    FROM MinuteCategory mc join MinuteLookup ml on mc.Minute = 
    CASE WHEN mc.Minute between ml.MinuteLow and ml.MinuteHigh THEN ml.MinuteCategoryId ELSE NULL
    END
    Would appreciate any help.
    SS

    Please post DDL, so that people do not have to guess what the keys, constraints, Declarative Referential Integrity, data types, etc. in your schema are. Learn how to follow ISO-11179 data element naming conventions and formatting rules. Temporal data should
    use ISO-8601 formats. Code should be in Standard SQL as much as possible and not local dialect. 
    This is minimal polite behavior on SQL forums. 
    >> I have two tables <<
    Have you read any book on data, so you have some idea what a “<something>_category” means? THINK!! A minute is unit of temporal measurement. By definition, it cannot be a category. 
    There is no such thing as a “<something>_category_id”; a data element is a “<something>_category” or a “<something>_id”, as per basic data modeling. 
    Use a table of time slots set to one more decimal second of precision than your data. You can now use temporal math to add it to a DATE to TIME(1) get a full DATETIME2(0). Here is the basic skeleton. 
    CREATE TABLE Timeslots
    (slot_start_time TIME(1) NOT NULL PRIMARY KEY,
     slot_end_time TIME(1) NOT NULL,
     CHECK (start_time < end_time));
    INSERT INTO Timeslots  --15 min intervals
    VALUES ('00:00:00.0', '00:14:59.9'),
    ('00:15:00.0', '00:29:59.9'),
    ('00:30:00.0', '00:44:59.9'),
    ('00:45:00.0', '01:00:59.9'), 
    ('23:45:00.0', '23:59:59.9'); 
    Here is the basic query for rounding down to a time slot. 
    SELECT CAST (@in_timestamp AS DATE), T.start_time
      FROM Timeslots AS T
     WHERE CAST (@in_timestamp AS TIME)
           BETWEEN T.slot_start_time 
               AND T.slot_end_time;
    --CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
    in Sets / Trees and Hierarchies in SQL

  • Update Statement With Decode and Business Dates

    Hello everyone,
    I need to write a UPDATE statement using business date rule. In general, the due date is 30 days from the checkout date. If the due date falls on a Saturday or Sunday then the loan period is 32 days.
    I know that to test for a weekday, I'd need to use the to_char function in Oracle with the format of ‘D’. I did some research and found that to test which weekday November 12, 2007 falls on, I'd need to use the expression to_char(’12-NOV-2007’,’D’). This function returns a number between 1 and 7. 1 represents Sunday, 2 Monday, …,7 Saturday.
    What I really would need to do is write one UPDATE statement using an expression with the Decode function and the to_Char function:
    UPDATE book_trans SET due_dte = expression
    These are the transactions that will need to be updated:
    ISBN 0-07-225790-3 checked out on 15-NOV-2007 by employee 101(book_trans_id=1)
    ISBN 0-07-225790-3 checked out on 12-NOV-2007 by employee 151(book_trans_id=2)
    ISBN 0-201-69471-9 checked out on 14-NOV-2007 by employee 175(book_trans_id=3)
    ISBN 0-12-369379-9 checked out on 16-NOV-2007 by employee 201(book_trans_id=4)
    I manually calculated the due-dte and wrote update statement for each book_trans_id:
    UPDATE book_trans SET due_dte = '17-dec-07' WHERE book_trans_id = 1;
    UPDATE book_trans SET due_dte = '12-dec-07' WHERE book_trans_id = 2;
    UPDATE book_trans SET due_dte = '14-dec-07' WHERE book_trans_id = 3;
    UPDATE book_trans SET due_dte = '18-dec-07' WHERE book_trans_id = 4;
    As you can see, it's very cumbersome and I'd just like to know how to incorporate everything in one Update statement:
    UPDATE book_trans SET due_dte = expression
    so that if due date falls on Saturday or Sunday, the loan period is 32 days; weekday, loan period is 30 days.
    Any tips or help will be greatly appreciated. Thanks!

    Hi,
    882300 wrote:
    Hello everyone,
    I need to write a UPDATE statement using business date rule. In general, the due date is 30 days from the checkout date. If the due date falls on a Saturday or Sunday then the loan period is 32 days. That's equivalent to saying that the due date is normally 30 days after the checkout date, but if the checkout date falls on a Thursday or Friday, then the due date is 32 days after the checkout date. I used this equivalent in the statement below.
    I know that to test for a weekday, I'd need to use the to_char function in Oracle with the format of ‘D’. I did some research and found that to test which weekday November 12, 2007 falls on, I'd need to use the expression to_char(’12-NOV-2007’,’D’). This function returns a number between 1 and 7. 1 represents Sunday, 2 Monday, …,7 Saturday.That's just one way to find out the weekday, and it's error-prone because it depends on your NLS_TERRITORY setting. A more reliable way is to use 'DY' or 'DAY' as the 2nd argument to TO_CHAR. That depends on NLS_DATE_LANGUAGE, but you can explicitly set the language in the optional 3rd argument to TO_CHAR, which means your code will work the same no matter what the NLS settings happen to be. It's also easier to debug: you may have to think whether '1' means Sunday or Monday, but it's easy to remember that 'SUN' means Sunday.
    What I really would need to do is write one UPDATE statement using an expression with the Decode function and the to_Char function:
    UPDATE book_trans SET due_dte = expressionHere's one way:
    UPDATE  book_trans
    SET     due_dte = checkout_dte + CASE
                                      WHEN  TO_CHAR ( checkout_dte
                                             , 'fmDAY'
                                     , 'NLS_DATE_LANGUAGE=ENGLISH'
                                     ) IN ('THURSDAY', 'FRIDAY')
                             THEN  32
                             ELSE  30
                                  END
    WHERE   book_trans_id      IN (1, 2, 3, 4)
    ;This assumes that checkout date is a column in the same table.

  • Dynamic UPDATE statement with parameters for column names.

    Hello,
    On this* website I read "The SQL string can contain placeholders for bind arguments, but bind values cannot be used to pass in the names of schema objects (table or column names). You may pass in numeric, date, and string expressions, but not a BOOLEAN or NULL literal value"
    On the other hand, in this Re: execute immediate with dynamic column name update and many other
    posts people use EXECUTE IMMEDIATE to create a dynamic UPDATE statement.
    dynSQL:='UPDATE CO_STAT2 CO SET CO.'||P_ENT_B_G_NAME||' = '||P_ENT_E_G_WE||'
    WHERE ST IN
    (SELECT ST FROM STG_CO_STAT2_TEST CO WHERE
    '||P_ST||' = CO.ST AND
    CO.'||P_ENT_E_G_NAME||' > '||P_ENT_E_G_WE||' AND
    CO.'||P_ENT_B_G_NAME||' < '||P_ENT_E_G_WE||');';
    EXECUTE IMMEDIATE dynSQL ;
    Since this statement is part of a Stored Procedure, I wont see the exact error but just get a ORA-06512.
    The compiling works fine and I use Oracle 11g.
    http://psoug.org/definition/EXECUTE_IMMEDIATE.htm

    OK I extracted from all of your posts so far that I have to use "bind-variables with :"
    From all the other tuorials and forums posts, I assume using the pipe is correct so I added those as well into the script:
    set serveroutput on format wraped;
    DECLARE
    dynSQL VARCHAR2(5000);
    P_ENT_E_G_NAME VARCHAR2 (100) :='test1'; P_ENT_E_G_WE VARCHAR2 (100) :='01.02.2012'; P_ENT_B_G_NAME VARCHAR2 (100) :='01.01.2012';
    P_ST                VARCHAR2 (100) :='32132';
    BEGIN
    dynSQL:= 'UPDATE CO_STAT2 CO SET CO.'||:P_ENT_B_G_NAME||' = '||:P_ENT_E_G_WE||'
                  WHERE ST IN (SELECT ST FROM STG_CO_STAT2_TEST CO WHERE
                  '||:P_ST||'                           = CO.ST                  AND
                  CO.'||:P_ENT_E_G_NAME||'     > '||:P_ENT_E_G_WE||' AND
                  CO.'||:P_ENT_B_G_NAME||'    
    < '||:P_ENT_E_G_WE||')';
    --this is somehow missing after the last < '||:P_ENT_E_G_WE||')';
    dbms_output.enable;
    dbms_output.put(dynSQL);
    --EXECUTE IMMEDIATE dynSQL;    
    END;Problem:I think I figured it out, the dates that I parse into the query need additional '

  • Performance Problem with Nested Select

    I have an urgent performance question, I run a nested select like this one
      SELECT tabname
               INTO ls_dd02l
               FROM dd02l
               WHERE tabname LIKE c_feld
               AND   buffered IS NOT NULL.
          SELECT tabname fieldname keyflag position
                 INTO CORRESPONDING FIELDS OF ls_dd03l
                 FROM dd03l
                 WHERE tabname   = ls_dd02l-tabname
                 AND   fieldname IN ('MANDT', 'CLIENT', 'CLNT')
                 AND   keyflag   = 'X'.
            IF ( sy-subrc EQ 0 ).
              wa-tabname   = ls_dd03l-tabname.
              wa-fieldname = ls_dd03l-fieldname.
              wa-keyflag   = ls_dd03l-keyflag.
              wa-position  = ls_dd03l-position.
              APPEND wa TO itab.
            ENDIF.
          ENDSELECT.
        ENDSELECT.
    It is taking about 20sec, which is much too long.
    How can I improve the performance.
    Points rewarded!
    S.B.

    Hi Siegfried,
    As Vinay said  u can use INNER JOIN to get the data from both database tables
    or u can use FOR ALL ENTRIES statement.
    You can use the option FOR ALL ENTRIES to replace nested select loops by operations on internal tables. This can significantly improve the performance for large sets of selected data.
    The above statement is from Library. Please check this link
    http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb3a1f358411d1829f0000e829fbfe/content.htm
    Tabular Conditions sub heading
    SELECT a~tabname
           b~fieldname
           b~keyflag
           b~position
        FROM dd02l as a INNER JOIN dd03l as b
          ON a~tabname = b~tabname
         INTO TABLE itab
       WHERE a~tabname LIKE c_feld
         AND buffered IS NOT NULL
         AND fieldname IN ('MANDT', 'CLIENT', 'CLNT')
         AND keyflag   = 'X'.
    OR
    SELECT tabname
           INTO TABLE lt_dd02l
           FROM dd02l
           WHERE tabname LIKE c_feld
           AND   buffered IS NOT NULL.
    IF NOT lt_dd02l IS INITIAL.
          SELECT tabname fieldname keyflag position
                 INTO CORRESPONDING FIELDS OF TABLE itab
                 FROM dd03l
                 FOR ALL ENTRIES IN lt_dd02l
                 WHERE tabname   = lt_dd02l-tabname
                 AND   fieldname IN ('MANDT', 'CLIENT', 'CLNT')
                 AND   keyflag   = 'X'.
    ENDIF.
    Please check this link
    /people/rob.burbank/blog/2007/03/19/joins-vs-for-all-entries--which-performs-better
    You have to look at the primary keys..
    MANDT
    STLTY
    STLNR
    STLAL
    STLKN
    STASZ
    MANDT
    STLTY
    STLNR
    STLKN
    STPOZ
    You need the stlty for both tables stas and stpo
    The on condition between stas and stpo is obvious, will help a
    bit.
    It would be much better, if you could add it to the WHERE Condition.
    Do you know it?
    If not, then as the BOM categories are not so many,
    Try
    select distinct stlty
    from stas.
    and add the values to you where condition as
    AND s1-stlty in ( ....... )
    This should improve the performance.
    I don't kknow whether the BOM Categories can change in future.
    instead of a fixed in, you could add a subselect from a customizing table.
    I am quite confident that the solution I proposed will be much faster than the FOR ALL ENTRIES.
    But with more complicated joins you really have to try it.
    There is no general rule about the number of tables in a join, there are perfectly running joins with 5 tables
    and there are porblems with 2 tables.
    You must analyze the indexes, it it is clear which index will support the accesses then it will work fine.
    The FOR ALL ENTRIES is by construction much slower!
    Best regards,
    raam

  • How to combine update stmts with different filters and multi-columns?

    Hi,
    I found these following update stmts in one of our long running jobs. I'd like to see if it is possible to combine it in one update or merge stmt so I can reduce the scan time on the same table from 4 times to 1 time. But I'd need some expert suggestions for how to do that. Here are the stmts:
    UPDATE table1 c
    SET (polnum,polren,polseq,vehnum,
    compgrp,prodgrp,uwsys,comp,
    state,prod,rrdate,brand,
    agycode,AGYCLASS,valid)=(SELECT DISTINCT polnum,polren,polseq,vehnum,
    compgrp,prodgrp,uwsys,comp,
    state,prod,rrdate,brand,
    agycode,AGYCLASS,'A'
    FROM table2 v
    WHERE v.polnum=c.pol# AND
    v.polren=c.ren# AND
    v.polseq=c.seq# AND
    v.vehnum=c.veh#)
    WHERE c.polnum IS NULL;
    UPDATE table1 c
    SET (polnum,polren,polseq,vehnum,
    compgrp,prodgrp,uwsys,comp,
    state,prod,rrdate,brand,
    agycode,AGYCLASS,valid)=(SELECT DISTINCT polnum,polren,polseq,vehnum,
    compgrp,prodgrp,uwsys,comp,
    state,prod,rrdate,brand,
    agycode,AGYCLASS,'B'
    FROM table2 v
    WHERE v.polnum=c.pol# AND
    v.polren=c.ren# AND
    v.polseq=c.seq# AND
    v.mainveh='Y')
    WHERE c.polnum IS NULL;
    UPDATE table1 c
    SET (polnum,polren,polseq,vehnum,
    compgrp,prodgrp,uwsys,comp,
    state,prod,rrdate,brand,
    agycode,AGYCLASS,valid)=(SELECT DISTINCT polnum,polren,polseq,vehnum,
    compgrp,prodgrp,uwsys,comp,
    state,prod,rrdate,brand,
    agycode,AGYCLASS,'C'
    FROM table2 v
    WHERE v.polnum=c.pol# AND
    v.polren=c.ren# AND
    v.polseq=0 AND
    v.mainveh='Y')
    WHERE c.polnum IS NULL;
    UPDATE table1 c
    SET (polnum,polren,polseq,vehnum,
    compgrp,prodgrp,uwsys,comp,
    state,prod,rrdate,brand,
    agycode,AGYCLASS,valid)=(SELECT DISTINCT polnum,polren,polseq,vehnum,
    compgrp,prodgrp,uwsys,comp,
    state,prod,rrdate,brand,
    agycode,AGYCLASS,'D'
    FROM table2 v
    WHERE v.polnum=c.pol# AND
    v.polren=0 AND
    v.polseq=0 AND
    v.mainveh='Y')
    WHERE c.polnum IS NULL;
    Table1 has 800000 rows, Table2 has 200 million rows. In table1, about 12.5% rows has a null polnum, so 12.5% data will be updated.
    Thank you in advance with all your suggestions!

    the reason I never specified the joins is because I thought the joins were there to derive the Valid indicator, in which case they are not required if you are using the CASE statement. The join, "V.Polnum = C.Pol#" is still required as this is across all rows! If that is the case, then it can be simplified even more so;
    update   Table1 C
    set      (Polnum,Polren,Polseq,Vehnum,Compgrp,Prodgrp,Uwsys,Comp,State,Prod,Rrdate,Brand,Agycode,Agyclass,Valid) =
                ( select   distinct Polnum
                                   ,Polren
                                   ,Polseq
                                   ,Vehnum
                                   ,Compgrp
                                   ,Prodgrp
                                   ,Uwsys
                                   ,Comp
                                   ,State
                                   ,Prod
                                   ,Rrdate
                                   ,Brand
                                   ,Agycode
                                   ,Agyclass
                                   ,case
                                      when (Polren + Polseq=0) and (Mainveh='Y') then
                                            'D'
                                      when (Polren=0) and (Polseq>0) and (Mainveh='Y') then
                                            'C'
                                      when (Polren>0) and (Polseq>0) and (Mainveh='Y') then
                                            'B'
                                      when (Polren>0) and (Polseq>0) and (Mainveh!='Y') then
                                            'A'
                                    end as Valid
                 from      Table2 V
                 where     V.Polnum = C.Pol#
    where    C.Polnum is null;However if you are returning more rows than what you are expecting from the above update statement because the joins are being used to limit the data as well, then this might be more appropriate:
    update   Table1 C
    set      (Polnum,Polren,Polseq,Vehnum,Compgrp,Prodgrp,Uwsys,Comp,State,Prod,Rrdate,Brand,Agycode,Agyclass,Valid) =
             ( select v2.PolNum
                   ,v2.Polren
                   ,v2.Polseq
                   ,v2.Vehnum
                   ,v2.Compgrp
                   ,v2.Prodgrp
                   ,v2.Uwsys
                   ,v2.Comp
                   ,v2.State
                   ,v2.Prod
                   ,v2.Rrdate
                   ,v2.Brand
                   ,v2.Agycode
                   ,v2.Agyclass
                   ,case
                      when (v2.Polren + v2.Polseq=0) and (v2.Mainveh='Y') then
                            'D'
                      when (v2.Polren=0) and (v2.Polseq>0) and (v2.Mainveh='Y') then
                            'C'
                      when (v2.Polren>0) and (v2.Polseq>0) and (v2.Mainveh='Y') then
                            'B'
                      when (v2.Polren>0) and (v2.Polseq>0) and (v2.Mainveh!='Y') then
                            'A'
                    end as Valid_Flag
                ( select   distinct Polnum
                                   ,Polren
                                   ,Polseq
                                   ,Vehnum
                                   ,Compgrp
                                   ,Prodgrp
                                   ,Uwsys
                                   ,Comp
                                   ,State
                                   ,Prod
                                   ,Rrdate
                                   ,Brand
                                   ,Agycode
                                   ,Agyclass
                                   ,Mainveh
                 from      Table2 V
                 where     V.Polnum = C.Pol#
                 ) v2
             where (v2.Polren = C.Pol# or v2.Polren = 0)
             and   (v2.Polseq = C.Seq# or v2.Polseq = 0)
    where    C.Polnum is null;As before, both remain UNTESTED!

  • UPDATE statement based on query and return multiple value

    Hi everybody,
    I have two tables: temp3 and temp
    One of them (temp3) should be updated based on the query where clause (id and flag columns).
    I run this query but it updates all of the records in the temp3. I want to update only records with 'UPDATED' flag.
    update temp3 t3
    set (id,name,address) = (select t.id, t.name, t.address from temp t, temp3 t3
    where t.id = t3.id and t.flag = 'UPDATED');
    Does any body know how I can do it?
    I appreciate your help
    Thx

    Hello
    Basically you're missing a where clause on your update statement to restrict rows in t3 to the ones that have a corresponding row in t1.
    SQL> select * from dt_test_t1;
            ID NAME                 ADDRESS              ROW_STATUS
             1 Joseph Bloggs        Some street          UPDATED
             1 Joe Bloggs           Old street           OLD
             2 Fredrick Bloggs      New street           UPDATED
             2 Fred Bloggs          Some street          OLD
             3 Robert Bloggs        Better street        UPDATED
             3 Bob Bloggs           Some street          OLD
           100 Barry Bethel         Some street          UPDATED
           200 Giles Brandreth      Some street          UPDATED
    8 rows selected.
    SQL> select * from dt_test_t3;
            ID NAME                 ADDRESS              ROW_STATUS
             1 Joe Bloggs           Old street
             2 Fred Bloggs          Some street
             3 Bob Bloggs           Some street
             4 Joe Smith            Some street
             5 John Doe             Some street
    --this update as it stands does not work as it updates rows to have
    --null name and address where there is not a proper matching row in
    --t1
    SQL> UPDATE
      2     dt_test_t3 t3
      3  SET
      4     (       t3.name,
      5             t3.address,
      6             t3.row_status
      7     ) = (SELECT
      8             t1.name,
      9             t1.address,
    10             t1.row_status
    11          FROM
    12             dt_test_t1 t1
    13          WHERE
    14             t1.id = t3.id
    15          AND
    16             t1.row_status = 'UPDATED'
    17         );
    5 rows updated.
    SQL> select * from dt_test_t3;
            ID NAME                 ADDRESS              ROW_STATUS
             1 Joseph Bloggs        Some street          UPDATED
             2 Fredrick Bloggs      New street           UPDATED
             3 Robert Bloggs        Better street        UPDATED
    4
    5
    SQL> rollback;
    Rollback complete.
    --Now add in the where clause to make sure we're only updating rows in
    --t3 that have a corresponding row in t1:
    SQL> UPDATE
      2     dt_test_t3 t3
      3  SET
      4     (       t3.name,
      5             t3.address,
      6             t3.row_status
      7     ) = (SELECT
      8             t1.name,
      9             t1.address,
    10             t1.row_status
    11          FROM
    12             dt_test_t1 t1
    13          WHERE
    14             t1.id = t3.id
    15          AND
    16             t1.row_status = 'UPDATED'
    17         )
    18  WHERE
    19     EXISTS( SELECT
    20                     NULL
    21             FROM
    22                     dt_test_t1 t1
    23             WHERE
    24                     t1.id = t3.id
    25             AND
    26                     t1.row_status = 'UPDATED'
    27             );
    3 rows updated.
    SQL> select * from dt_test_t3;
            ID NAME                 ADDRESS              ROW_STATUS
             1 Joseph Bloggs        Some street          UPDATED
             2 Fredrick Bloggs      New street           UPDATED
             3 Robert Bloggs        Better street        UPDATED
             4 Joe Smith            Some street
             5 John Doe             Some streetHTH
    David

Maybe you are looking for

  • How do I get the date of a file in an FTP?

    As stated in the subject. I am at wits end trying to get the date of a file from FTP(Stuck for days). I tried the "URLConnection" Class but could only get me the size of the file with "getContentLength()" "getContentDate()" & "getLastModified()" both

  • Nokia 2330 classic - USB Cable connection.

    Hello all, i have nokia 2330 classic and i want to know that can it be connected to PC via usb cable ???

  • Action method getting called multiple times.

    Hi All, The problem that I am facing is pretty weird. The issue is the i have an input field that takes date as input field. This field is inside a data taable FYI: I tried converter still same problem. So when ever I enter wrong data as "asda" insid

  • Daily Business Intelligence- DBI

    Hi , Iam newbie to DBI. I have gone through the DBI implementation guide and tried to create a new report. Created a report and published it. In the process of creating Initial Request set (in Daily Business Intelligence Administrator), could not fin

  • Random backup is taking a lot of my system space.

    When I look at my 'about my mac' storage settings I see that I have 125gb of backups where can I find this data and possible delete it because it seems like an awful lot of space.