Update From Select statement

I'm very new to Oracle so please bear with me.
I need to Update a table ( T1) with dates selected from another table (T2). However, because of the size of the tables (the second one has upwards of 3,000,000 rows) I have to do it incrementally, say 10k rows at a time. However, there's no field in T1 that I can use as a flag to say the row's been updated. But I do have an ascending numeric field that I can do a Max() function on. My thought was to capture the max numeric function in local variable and use it in a where clause so I only get the rows I need to update. Pseudo-ish code follows:
DECLARE
mDoc NUMBER
maxNum NUMBER
BEGIN
mDoc = 0;
SELECT MAX(doc_id_n) INTO maxNum FROM T2;
WHILE NOT mDoc > maxNum
LOOP
SELECT MAX(doc_id_n) INTO mDoc FROM T2
WHERE doc_id_n > mDoc AND rownum < 10001;
UPDATE T1 SET scan_d =
(select to_date(t.ddate,'MM/DD/YYYY') as ndate
from T2 t, T1 i
where i.stor_rep_id = 'HRPAYROLL1' and
t.doc_id_n = i.doc_id_n and rownum < 10001
AND NOT t.doc_id_n > mDoc
ORDER BY t.doc_id_n)
END LOOP;
END;

You say:
<However, there's no field in T1 that I can use as a flag to say the row's been updated.
That's fine, you needn't nor might you want to do this incrementally. Instead, what basis or values in table(T1) are you using to determine which rows are updated? For example, you might want to use a particular field as your 'filter' between the two tables. Still it's not fun :)
UPDATE T1 SET date_column = (SELECT inserted_date FROM T2 WHERE customer = 'ABC')
WHERE customer = 'ABC';
UPDATE T1 SET date_column = (SELECT inserted_date FROM T2 WHERE customer = 'XYZ')
WHERE customer = 'XYZ';
Hope this helps.

Similar Messages

  • How to find the number of fetched lines from select statement

    Hi Experts,
    Can you tell me how to find the number of fetched lines from select statements..
    and one more thing is can you tell me how to check the written select statement or written statement is correct or not????
    Thanks in advance
    santosh

    Hi,
    Look for the system field SY_TABIX. That will contain the number of records which have been put into an internal table through a select statement.
    For ex:
    data: itab type mara occurs 0 with header line.
    Select * from mara into table itab.
    Write: Sy-tabix.
    This will give you the number of entries that has been selected.
    I am not sure what you mean by the second question. If you can let me know what you need then we might have a solution.
    Hope this helps,
    Sudhi
    Message was edited by:
            Sudhindra Chandrashekar

  • How to pass rowtype argument to a function from select statement?

    Hi all!
    I have function that takes mytable%rowtype as in parameter. can I pass entire row of mytable to the function from select statement? kind of
    select myfunction(mytable.*) from mytable where ....
    Thanks in advance

    The function can be used in a SQL statement only if it accepts SQL types and returns SQL type. %ROWTYPE being PL/SQL construct and not a SQL datatype, can not be used in this context.
    http://download-west.oracle.com/docs/cd/B14117_01/server.101/b10743/datatype.htm#i2093

  • Hint in Update or Select Statement in an inline query ??

    Hi ,
    I had an update statement that will get the data from the inline select statement,now where can i can keep the hint ,either in update statement or in Select statement...
    Please let me know if my sample script is wrong or any better way to approach...Please assume that Salary table had millions of employee's salary records.
    update emp
    set salary = salary + (select /*+ full(a) parallel(a,4)  */ salary from Salary
                             where   experience > 5  and empno = 55 )
    where empno = 85Thanks
    Rede

    You would put the hint in the select statement as you have it, but it won't work until you alias your table name to "a" since that is how you are referencing it in the hint:
    update emp
    set salary = salary + (select /*+ full(a) parallel(a,4)  */ salary from Salary a
                             where   experience > 5  and empno = 55 )
    where empno = 85
    NOTE I added the "a" right after the Salary table name AND you can only do parallel updates on partitioned table, but the select will run in parallel to at least get the data for you quickly.
    To answer your other question about syntax, it is incorrect and there are a lot of ways to write it but a straight forward way would be to use the merge statement
    merge into emp e
    using (select /*+ full(s) parallel(s,4)  */ s.salary from salary s where s.experience > 5) s
    on (e.empno = s.empno)
    when matched then update
    set salary = s.salary;

  • For update in select statement

    i have a table which store data in a clob. sometimes user forget to add and extra carriage return after last line. so i am doing the following to modify the clob
    SELECT file_content INTO lv_lob FROM files WHERE id = p__id FOR UPDATE;
    dbms_lob.writeappend(lv_lob,2,C_NEW_LINE);
    C_NEW_LINE CONSTANT VARCHAR2(2) := Chr(13)||Chr(10);
    'for update' should only lock that specific row since id is a pk key in the table.
    i have two front end screen where people upload file. a unique id is generated for each file and the data with id inserted in the table. user can upload file from the two different screen at same time.
    the problem is that i am getting a 'resource busy and acquire with NOWAIT specified' error. i don't know why if every file i upload belongs to different row.
    anybody knows why i would get this error and how can i solve it?

    How are you generating the unique ID? Are you using a sequence? Or are you using something that forces serialization?
    Where in the program flow are you adding the extra carriage return?
    Is it this SELECT statement that is throwing the error? Or is it being thrown somewhere else?
    Justin

  • Snapshot too old ORA-01555 from select statement (discoverer)??

    Hi All
    Am I loosing the plot .. but we have a select statement run from discoverer which is causing the famous snapshot too old error.
    My understanding is that undo is generated from select/insert/update.
    So why is the following discoverer Select statement causing the error?
    from alert
    ORA-01555 caused by SQL statement below (SQL ID: gk0wxgqmx66sh, Query Duration=3866 sec, SCN: 0x001e.089cf3f9):
    SELECT  ( ROUND(( TO_DATE(SYSDATE)-o101038.HIRE_DATE )/365,2) ) as
      "  bla bla
    ORDER BY o101020.SUB_ORGANIZATION_NAME ASC
    Thanks in Advance

    simon.9999 wrote:
    Hi All
    Am I loosing the plot .. but we have a select statement run from discoverer which is causing the famous snapshot too old error.
    My understanding is that undo is generated from select/insert/update.
    So why is the following discoverer Select statement causing the error?
    from alert
    ORA-01555 caused by SQL statement below (SQL ID: gk0wxgqmx66sh, Query Duration=3866 sec, SCN: 0x001e.089cf3f9):
    SELECT  ( ROUND(( TO_DATE(SYSDATE)-o101038.HIRE_DATE )/365,2) ) as
      "  bla bla
    ORDER BY o101020.SUB_ORGANIZATION_NAME ASC
    Thanks in Advance
    The SELECT statement is the victim.
    Some session is doing DML against the same table against which the SELECT occurs & is likely doing COMMIT inside a LOOP.

  • Using Hint in Update or Select Statement...

    Hi ,
    I had an update statement that will get the data from the inline select statement,now where can i can keep the hint ,either in update statement or in Select statement...
    Please let me know if my sample script is wrong or any better way to approach...Please assume that Salary table had millions of employee's salary records.
    update emp
    set salary = salary + (select /*+ full(a) parallel(a,4)  */ salary from Salary
                             where   experience > 5  and empno = 55 )
    where empno = 85Thanks
    Rede

    The better approach would be to determine why you need a hint in the first place. If you're using a hint, that implies that the optimizer is choosing the incorrect plan when left to its own devices. That, in turn, implies that the statistics on your objects, the statistics on your system, your optimizer parameters, your session's optimizer settings or some other variable is incorrect. You're generally better off fixing the root problem than hinting every query.
    In this specific case, the parallel hint is invalid because there is no table aliased A. The full hint is invalid for similar reasons. Plus, with the query you provided, if A was intended to be the SALARY table, it seems exceptionally unlikely that you really want to do a full scan on the salary table to look for what had better be a single row (otherwise your query would throw a "too many rows returned" exception).
    Justin

  • Storing variable from select statement

    I need to store a variable from a select statement into a session. Before I was taken what was passed from another page and throwing it into a session.--
    String Password= request.getParameter("Password");
    String sql = "Select WEB_USER_ID FROM Web_User WHERE PASSWORD = '" + Password +"'";
    session.setAttribute("USER",Password);
    Now though I need to put in Web_User_Id and cannot find the proper syntax. Can someone help
    session.setAttribute("User",???????);

    WEB_USER_ID is unidentified because it is a column name, not a variable name. You should first assign its value to a variable and then put the variable name into the setAttribute statement instead.
    Your query may return more than one result based on the way it is written. It sounds like you are trying to find all users with a password that is equal to the value of Password. If this is correct, then are you trying to create an attribute for each user?
    If your intention is to locate a user, then the user id and password should have been provided. A variable should be defined for both the password and user id. You should then add the user id to the where clause. After a successful search, the attributes can be created using the same syntax.

  • Dynamic internal table from SELECT - statement ?

    Hi, is it possible to define an internal table just after a select statement is executed so that this internal table holds all the data that come back from the statement ?
    thanks in advance,

    Check the link -
    Re: Create Dynamic internal table
    Regards,
    Amit

  • Calling PL/SQL code from Select statement

    Hi
    I have a PL/SQL function to calculate a value.
    create or replace procedure "SR_GROSS_MARGIN"
    (netsales IN NUMBER,
    margin IN NUMBER,
    GM OUT NUMBER)
    is
    BEGIN
        IF NETSALES = 0 THEN
           GM := 0;
        ELSIF
           NETSALES < 0 THEN
           GM := 0;
        ELSE
           GM := NETSALES / MARGIN;
        END IF;
    END;How do I call this from a SELECT statement?
    Regards
    Adam

    here you go:
    create or replace function SR_GROSS_MARGIN
    (netsales IN NUMBER,
    margin IN NUMBER)
    return number
    is
    gm number;
    BEGIN
        IF NETSALES = 0 THEN
           GM := 0;
        ELSIF
           NETSALES < 0 THEN
           GM := 0;
        ELSE
           GM := NETSALES / MARGIN;
        END IF;
    return gm;
    END;then you can:
    select gm(2500,20) from dual;

  • Passing values to OnDemandProcess from Select Statement on Reports Page

    I think I'm just missing some of the proper syntax, Can somebody please help me.
    As I've come to understand this process of passing the values,
    I recognize it as (3) steps.
    1. Referencing the "javascript" in the Select Statement of the Reports Page.
    2. Including the "javascript" in the Page Header region.
    3. Defining the Process as an OnDemand Application Process.
    My problem is:
    a. I can't seem to get the Value passed to the Application Process.
    b. I don't know how to reference the Value passed once I get it passed.
    Here's what I've got done so far:
    1. (The Select Statement)
       onClick="javascript:ORDER_CONNECT_SEQUENCE('||ID||')
    {code}
    2. (The javascript)
    {code}
    function ORDER_CONNECT_SEQUENCE('+ID+')
    var get = new htmldb_Get(null,$x('pFlowId').value,'APPLICATION_PROCESS=ORDER_CONNECT_SEQUENCE',0);
    get.add('+ID+');
    gReturn = get.get('+ID+');
    {code}
    3. (The Application Process)
    {code}
       BEGIN
       Select '+ID+' into THIS_CIRCUIT from dual;
    {code}
    I Know that the Application Process is being referenced and that the OnClick routine is working properly,
    because if I plug a Value into the Application Process, instead of using '+ID+',
    Then I get all of my desired results.
    Any help you can give me here is greatly appreciated.
    Thanks- Gary                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

    Dimitri, Thanks for your efforts here, but its still not working for me.
    Here's what I understand from your suggestions:
    1. (changes to the javascipt)
       a. replace function ORDER_CONNECT_SEQUENCE('+ID+')
           with
           function ORDER_CONNECT_SEQUENCE(pId)
       b. replace get.add('+ID+')
           with
           get.addParam('x01',pId)
       c. replace getReturn = get.get('+ID+')
          with
          getReturn = get.get()
          get = null2. (chages to the Application Process)
       a. add l_value varchar2(4000) to my list of declarations
       b. add l_value := wwv_flow.g_x01 below the begin statement
       c. I guess then I would reference the l_value like this:
           Select l_value into THIS_CIRCUIT from dual* Also, You didn't mention any changes in my Select statement in the Reports region of the page.
    FYI.
      onClick="javascript:ORDER_CONNECT_SEQUENCE('||ID||')
    {code}
      The 'ID' which I'm referencing here is an ID from the table which I'm querying.
    Am I misunderstanding you somewhere?
    Thanks- gary                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Dynamic rowsize from select statement.

    Hi, I'm trying to get the lenght if the returned row from a select statement but I'm not sure how to do this properly.
    The statement contains 530 columns of different type, mostly VARCHAR but also different number formats and timestamps.
    I've tried to use LENGTH() + LENGTH() but our interactive SQL client is not very happy with the enormous statement size.
    Thank you in advance.
    /76...

    I think you need something like this:
    select column_name "name", data_type "type",
    data_length "length", nullable "null"
    from all_tab_columns
    where table_name =' Your table_name'

  • View last 5 lines from select statement?

    Hi,
    I would like to view only the last 5 lines from a select statement, how would this be done?
    Something like:
    select * from a .................
    regards
    RobH

    Hi RobH,
    First of all: You need to identify a criterion that helps to sort your data in the desired way.
    Done that you can start to read about analytical functions, maybe ROW_NUMBER(), RANK() and / or DENSE_RANK() might be interesting.
    Edit:
    Changed rn() to row_number()

  • Can i get an exact part of data from select statement

    hi...good afternoon...
    i want to fetch the records from a table by select statement. but i dont want to fetch the as it is record. i want to fetch a particular part of eacg record in that column.
    for example..
    table_name=tab1
    column_name=col1
    the col1 contains datas like *'customer search-cs', 'get customer-gc', 'customer delete-cd'* etc etc..
    now while writing the select query, i want to get the descriptive output as get customer, customer search, customer delete....that is i dont want the symbols..
    is it possible to get this???
    plss suggest...

    You mean like these possible ways of doing it...
    SQL> ed
    Wrote file afiedt.buf
      1* select substr('customer search-cs',1,length('customer search-cs')-3) from dual
    SQL> /
    SUBSTR('CUSTOME
    customer search
    SQL> ed
    Wrote file afiedt.buf
      1* select regexp_substr('customer search-cs','^[^-]*') from dual
    SQL> /
    REGEXP_SUBSTR('
    customer search
    SQL>

  • Need to return only one record from select statement.

    Hello friends,
    I have a scenerio in which code only want to fetch one value from the SQL statement, but in some cases the statment return more then one row due to which ORA-01422 : Exact fetch return more then one row, error comes.
    Can you suggest me to write a select statement that will handel this. I am using the below mentioned select statement:
    EXECUTE IMMEDIATE
    'SELECT rsdn FROM ' || schema || '.table_name' ||
    ' WHERE a = :1 AND b = :2 AND c IN (32,33,34)'
    INTO v_rsdn USING v_a, v_b;
    One question, Can I use Rownum < 2 in Where clause to restrict the select output to only one record ? Please suggest ?
    Regards,
    Rajat

    Well, if rownum=1 could be a trash answer, it would be an issue since we don't know which record you are interested to. Is it one particular ? Is it only to workaround the issue ? Then you may want to manage some BULK COLLECT or whatever else array to receive all the rows as well.
    What are you doing with the rows returned ?
    Nicolas.

Maybe you are looking for