How to update these two tables

Hello,
I have two tables (in Oracle 11g R2), and have to lock certain rows in each one of them for update...
Here is the sample data and expected result after updating, please help me for update statements.
Thanks in advance!!!
drop table t1;
drop table t2;
create table t1(
t1_id     number(5) primary key,
t1_col2   varchar2(20),
t1_col3   varchar2(10),
t2_id     varchar2(5));
create table t2(
t2_id    varchar2(5) primary key,
t2_col2  varchar2(10),
t2_col3  number(2),
t1_id    number);
insert into t1 values(1, '1 - col2', 'AB', null);
insert into t1 values(2, '2 - col2', 'AB', null);
insert into t1 values(3, '3 - col2', 'AB', null);
insert into t1 values(4, '4 - col2', 'AC', null);
insert into t1 values(5, '5 - col2', 'AC', null);
insert into t1 values(6, '6 - col2', 'AC', null);
insert into t1 values(7, '7 - col2', 'AC', null);
insert into t1 values(8, '8 - col2', 'AC', null);
insert into t1 values(9, '9 - col2', 'AC', null);
insert into t1 values(10, '10 - col2', 'AC', null);
commit;
insert into t2 values('11001', 'ABC', 12, null);
insert into t2 values('11021', 'ABC', 12, null);
insert into t2 values('11022', 'ABC', 12, null);
insert into t2 values('11023', 'ABC', 12, null);
insert into t2 values('11024', 'ABC', 12, null);
insert into t2 values('11025', 'ABC', 12, null);
insert into t2 values('11030', 'ABC', 12, null);
insert into t2 values('11035', 'ABC', 12, null);
insert into t2 values('11051', 'ABC', 12, null);
insert into t2 values('11061', 'ABC', 12, null);
insert into t2 values('11071', 'ABC', 12, null);
insert into t2 values('11081', 'ABC', 11, null);
insert into t2 values('11091', 'ABC', 11, null);
commit;
declare
  cursor c1 is select *
                 from t1
                where t1_id in(select t1_id from (select t1_id from t1 where t1_col3 = 'AC' order by t1_id) where rownum <= 5)
               for update;
  cursor c2 is select *
                 from t2
                where t2_id in(select t2_id from (select t2_id from t2 where t2_col3 = 12 order by t2_id) where rownum <= 5)
               for update;
begin
  for rec_c1 in c1 loop
  end loop;
end;
The result must look like:
     T1_ID T1_COL2              T1_COL3    T2_ID
         4 4 - col2             AC         11001
         5 5 - col2             AC         11021
         6 6 - col2             AC         11022
         7 7 - col2             AC         11023
         8 8 - col2             AC         11024
T2_ID T2_COL2       T2_COL3      T1_ID
11001 ABC                12          4
11021 ABC                12          5         
11022 ABC                12          6
11023 ABC                12          7
11024 ABC                12          8

With the help of Bencol :-)
DECLARE
   CURSOR c1
   IS
      SELECT a.t1_id, b.t2_id
        FROM t1 a CROSS JOIN t2 b
       WHERE (a.t1_id, b.t2_id) IN (SELECT t1.t1_id, t2.t2_id
                                      FROM    (SELECT t1_id
                                                    , ROW_NUMBER () OVER (ORDER BY t1_id) t1_rn
                                                 FROM t1
                                                WHERE t1_col3 = 'AC') t1
                                           JOIN
                                              (SELECT t2_id
                                                    , ROW_NUMBER () OVER (ORDER BY t1_id) t2_rn
                                                 FROM t2
                                                WHERE t2_col3 = 12) t2
                                           ON t1.t1_rn = t2.t2_rn
                                     WHERE t1.t1_rn <= 5)
      FOR UPDATE;
BEGIN
   FOR rec_c1 IN c1
   LOOP
      UPDATE t1
         SET t2_id = rec_c1.t2_id
       WHERE t1_id = rec_c1.t1_id;
      UPDATE t2
         SET t1_id = rec_c1.t1_id
       WHERE t2_id = rec_c1.t2_id;
   END LOOP;
END;
/Regards.
Al

Similar Messages

  • How to deal these two scenarios in SAP.

    Hi All,
    My company is Software company.My company is having many clients.
    1)Whenever we require some materials for client project he provides us required materials and our company uses them for job, there is no change in state/value of tool, we  send back the tool after usage
    2)Our vendor will place the material at our place and we will pay
    the rent for every month or quarterly.
    how to deal these two scenarios in SAP.
    Regards,
    Purush.

    hi
    create new material type for this
    >1)Whenever we require some materials for client project he provides us required materials and our >company uses them for job, there is no change in state/value of tool, we send back the tool after usage
    u can just tick for quantity update for that material type in OMS2
    then u can take it using 501 Q
    >2)Our vendor will place the material at our place and we will pay
    >the rent for every month or quarterly.
    for this u can make quantity update as above and give the rent payment from FI side using FB60
    regards
    kunal
    updating

  • How to update field in table FAGLFLEXA/FAGLFLEXT

    Hi Gurus,
         we are stay in ECC 5.00. The new g/l have activated but we have not assigned the relevant scenario to the ledger (Refer note no 990612)
         now we have assinged following field:
         FIN_CCA: RCNTR (Cost center), SCNTR (Sender cost center)
         FIN_CONS: RASSC (Trading partner), RMVCT (Transaction Type)
         FIN_GSBER: RBUSA (Business area), SBUSA (Trading partner's business area)
         FIN_PCA: PRCTR (Profit Center), PPRCTR (Partner Profit Ctr)
         FIN_SEGM: SEGMENT (Segment), PSEGMENT (Partner segment)
         FIN_UKV: RFAREA (Functional Area), SFAREA (Partner Functional Area)
         our problem is the data was posted before that does not update fields in table FAGLFLEXA/FAGLFLEXT,How to update field in table FAGLFLEXA/FAGLFLEXT?
    Thanks,

    Hello Jenjira,
    Since this was a question asked few years back, I think you can share your experience with activation of a Scenario subsequent to Go-Live of new GL functionality (We are contemplating activation of one more scenario for Functional Area updation in new GL. 4 update scenarios were activated at time of New GL Implementation 4 years ago).
    Please share following:
    1. Impact of this subsequent activation on Open documents in the live system.
    2. How you addressed the issues arising on Point 1.
    3. Any support that you got from SAP Support Team on issues thereof.
    Any inputs will be of great help.

  • In ER DIagram using toad( how can i link two tables)

    Hi,
    Any Toad expert can guide me that how can i link two tables in Toad in ER Diagram to show the relationship between two tables.
    Thanks

    Hi,
    I hope there is foreign key relationship in tables, if so then you can click on "Find table dependencies" icon which will draw lines between them.
    ~Vinod

  • How to query for two tables

    Hi,
    I have this problem, How can query a two table?
    Table A ->  Table B
    id               table-a_id
    name          table_b_name
    the relationship is one-to-many
    How can I get the result?
    Hope my question make sense
    cheers.
    thanks a lot.

    I bet you have more luck looking for an answer in a SQL forum.

  • HT1338 I am unable to update Pages and Numbers  because I changed my Apple ID, being I could not remember my password and security questions. How can I now update  these two applications?

    I am unable to uodate Pages and Numbers sice I changed my Apple ID, because I forgot my password and other security information. How I do I now update these applications. The system is asking for my Old Apple ID.
    Also  i am unable to activitate icloud. I did not receive a verication email. How can this issue be addressed?
    Regards,
    P.S. my email address is [email protected]

    https://iforgot.apple.com/cgi-bin/WebObjects/DSiForgot.woa/wa/iforgot?app_type=e xt&app_id=93&returnURL=https://appleid.apple.com/cgi-bin/WebObjects/MyAppleId.wo a&language=US-EN

  • How to update a fact table when a dimension table is reloaded

    We have implemented BI Apps 796. Insertion into W_EMPLOYEE_D table which stores all the employee information had stopped one year back as some company security policy restricted the informatica worklfows to pick up the data. (PER_ALL_PEOPLE_F was a HRMS table and it contained sensitive information line SSN and salary, was inaccessible to the user which informatica uses and the SDE mapping used to return 0 rows).
    Now we have the approval to see those rows and the dimension table is loaded with some 100 new employees who joined in last one year.
    The ROW_WID of W_EMPLOYEE_D is referenced in lot of fact tables and for all those missing employees the WID in the fact table is 0.
    Now that we have all employees, how to make the FACT table point to the correct WID and not store 0. Has anyone faced this problem before?? Writing an update statement will be a tedious task as there are so many fact tables that join to w_employee_d. Also our company uses Sales, Procurement, Finance modules of OB Apps (which constitutes atleast 20 fact tables)
    Any guidance is appreciated. Thanks in advance

    Hello Kostis,
    thank you for your answer. I don't fully understand you. Can you show me short example, please? I create alias table for time dimension on Physical Layer - original table is TimeDayDim and I create aliases TimeDayDim1, TimeDayDim2, TimeDayDim3, TimeDayDim4. Then I create foreign key Fact.Time1 -> TimeDayDim1, Fact.Time2 -> TimeDayDim2, Fact.Time3 -> TimeDayDim3, Fact.Time4 -> TimeDayDim4. And what now? Must I create these table api Bussines Model and create new time dimensions at bussiness model????
    I need in Answers ONE Time dimension. I think I must split my fact table to four tables ... (time1, place1 ...) (time2, place2 ...) (time3 place3...) (time4 place4...) then link those tables to Time dimension (but I dont know where I can split those tables - on Physical Layer or on Bussines Layer).
    I suppose that I will have in Answers one time dimension and four facts tables and I will be able to query them. (for example: Time.Days, Fact1.Place1, Fact3.Speed, Fact4.Count Criteria: Time.Year = 2008)
    Best Regards Vlada

  • How do I join two tables in the same database and load the result into a destination table in a SSIS package

    Hi,
    I have a query that joins two tables in the same database, the result needs to be loaded in a destination DB table.  How do I do this in SSIS package?
    thank you !
    Thank You Warmest Fanny Pied

    Please take a look at these links related to your query.
    http://stackoverflow.com/questions/5145637/querying-data-by-joining-two-tables-in-two-database-on-different-servers
    http://stackoverflow.com/questions/7037228/joining-two-tables-together-in-one-database

  • How to update a adf table??

    Hi All,
    I have made a table in adf.
    I have set the beans, the data is displaying fine.
    Now I want to update these beans depending on some condition.
    How can I do that???
    Thanks
    Pooja

    Hi,
    so you want to update data in a table cell ? You can do this by replacing the outputText component of the table sell with an inoput text component. All you need to handle - unless using ADF BC - is the usecase in which users update multiple rows.
    Alternatively - and most common on the web - you create an edit page to where you navigate. If this is based on teh same model then the selected table data marks the current row for the edit form.
    Frank

  • How to join these two queries

    Hi experts,
    I need to join two queries but not sure how:
    select id from test_table1;
    select *
      from table(f_function(null
                           ,null
                           ,1 -- the id
                           ,sysdate);One query has IDs needed to run the second query. Is there a way to join those two?
    The result should be all columns from test_table1 + all columns from f_function.
    Best regards,
    Igor
    Edited by: Igor S. on Mar 8, 2013 5:18 AM

    Hi,
    Igor S. wrote:
    select  *
    from  test_table1,
    table(
    f_function(
    null,
    null,
    id,
    sysdate
    select  *
    from  test_table1,
    table(
    f_function(
    null,
    null,
    id,
    sysdate
    ) xyz
    where test_table1.id = xyz.id
    /So these two queries are the same?Try it and see.
    You'll find that the 2nd one produces an error. But if you change it to
    select  *
      from  test_table1,
            table(
                  f_function(
                             null,
                             null,
                             id,
                             sysdate
                            )      -- No alias here
                 ) xyz             -- Alias here, instead
    where test_table1.id = xyz.id
    /Then, assuming f_function produces a column called id, it will work.
    Whether it produces the same results or not depends on what the function returns, and whether either id is NULL.
    If the id column that the function returns is the same as the id value that you pass to it, and is never NULL, then the 2 queries will produce the same results.
    Either way, each row of test_table1 will be joined to each row that the function produces with the argument(s) from that row. The column names produced by the function and the values in those columns are determined by the function; they do not need to have anything in common with any table. In practice, a function like f_function will usually not return an column that is always identical to any of its inputs, since that value is already available from the input.

  • Using ADF BC how to insert into two table ?

    I'm working with ADF BC and JSP.
    In my project, I have :
    1- each COURSE have one SYLLABUS .
    2- each SYLLABUS have one or many WEEK.
    3- showCourses.jsp to show all courses and a button "create a new course" .
    4- createCourse.jsp to create or update the course.
    In "createCourse.jsp" , I want to fill all the data of COURSE , SYLLABUS and WEEK using the BC.
    may you kindly help me !

    No response ?!
    I have put data control of the two tables : COURSE and SYLLABUS on createCourse.jsp as Input Form drag and drop. Because there are two forms , I have remove one of them and keep one with the Commit button.
    Whene I Commit the form the course record that is created early in showCourse.jsp getting updat well .
    The problem is :
    there is no record in Sysllabus table to be update . So , How to create a record in Syllabus just when I create a record in Course ? And how to update it in the same form with course ?
    my project is stoping here !
    Are u hear me !

  • How to insert in two tables

    Hello,
    I have a form that should be insert/update data in two tables. in the context.
    the form is based on a GPV_REASSIGN table,then i want to insert into this table and also into an audit table named GPV_AUDIT_REASSIGN each time i make a commit form.
    2) iI would like also to read the GPV_REASSIGN table everyday to find out if there are new records, if so take these records and update two others tables. notice i want to do this transaction everynight.
    I know i have to write a procedure to that, but i don't really know the concept behind this kind of transaction.
    could someone help me, please?

    1) There are two apporaches to this kind of auditing information:
    - Use databasebase triggers to do the appropiate insert into the audit-table
    SQL> DROP TABLE TEST;
    Tabelle wurde gel÷scht.
    SQL> DROP TABLE TEST_HIST;
    Tabelle wurde gel÷scht.
    SQL>
    SQL>
    SQL> CREATE TABLE  TEST
      2     (       ID NUMBER,
      3     DATA VARCHAR2(20)
      4     )
    5  /
    Tabelle wurde erstellt.
    SQL>
    SQL> CREATE TABLE  TEST_HIST
      2     (       ID NUMBER,
      3     DATA VARCHAR2(10),
      4     DATE_CHANGED DATE,
      5     ACTION VARCHAR2(1),
      6     USER_CHANGED VARCHAR2(30)
      7     )
      8  /
    Tabelle wurde erstellt.
    SQL> CREATE OR REPLACE TRIGGER  TRG_TABLE_ARIU
      2  AFTER INSERT OR UPDATE ON TEST
      3  FOR EACH ROW
      4  DECLARE
      5   vcAktion VARCHAR2(1);
      6  BEGIN
      7    IF INSERTING THEN
      8      vcAktion:='I';
      9    ELSE
    10      vcAktion:='U';
    11    END IF;
    12    INSERT INTO TEST_HIST (
    13      ID,
    14      DATA,
    15      DATE_CHANGED,
    16      ACTION,
    17      USER_CHANGED
    18    ) VALUES (
    19      :NEW.ID,
    20      :NEW.DATA,
    21      SYSDATE,
    22      vcAktion,
    23      USER
    24    );
    25  END;
    26  /
    Trigger wurde erstellt.
    SQL> INSERT INTO TEST (ID, DATA) VALUES (1, 'Mein Text');
    1 Zeile wurde erstellt.
    SQL> UPDATE TEST SET DATA='Dein Text' WHERE ID=1;
    1 Zeile wurde aktualisiert.
    SQL> SELECT * FROM TEST;
            ID DATA
             1 Dein Text
    SQL> SELECT * FROM TEST_HIST;
            ID DATA       DATE_CHA A USER_CHANGED
             1 Mein Text  08.12.08 I HR
             1 Dein Text  08.12.08 U HR
    SQL>Advantage of this method is that it is always executed regardless form the insert comes.
    - Use a POST-INSERT-trigger and POST-UPDATE-trigger on the client-side in the form itself to do the appropiate insert into the audit-table
    POST-INSERT-trigger
    INSERT INTO TEST_HIST (
      ID,
      DATA,
      DATE_CHANGED,
      ACTION,
      USER_CHANGED
    ) VALUES (
      :BLOCK.ID,
      :BLOCK.DATA,
      SYSDATE,
      'I',
      USER
    );POST-UPDATE-trigger
    INSERT INTO TEST_HIST (
      ID,
      DATA,
      DATE_CHANGED,
      ACTION,
      USER_CHANGED
    ) VALUES (
      :BLOCK.ID,
      :BLOCK.DATA,
      SYSDATE,
      'A',
      USER
    );2) Create a DB-procedure to do your task (example-logic):
    CREATE OR REPLACE PROCEDURE PR_MYPROC IS
    BEGIN
      INSERT INTO TABLE2 (
        IDCOLUMN,
        COL2,
        COL3
      ) (SELECT IDCOLUMN,
                VAL2,
                VAL3
           FROM MYBASETABLE OUTER
          WHERE NOT EXISTS (SELECT 1
                              FROM TABLE2 INNER
                             WHERE INNER.IDCOLUMN=OUTER.IDCOLUMN
    END;
    /then you can use DBMS_SCHEDULER-package to create a job which runs every night.

  • How to update whole external table(in ABAP dictionary) from internal table at once

    Hi,
    How can I update the content of the external table (in ABAP dictionary) from the content of internal table data at once. I created the internal tables with out the header line, with the work area. I tried UPDATE TARGET_EXTERNAL_TABLE FROM TABLE INTERNAL_TABLE. But it returns 4 for sy-subrc and did not work.
    Thank you
    Regards.
    CHINTHAKA

    Hi Feiyun Wu,
    Thank you very much. Your code worked. Is there any way to replace the whole content of the external table from internal table data ?
    Regards,
    Chinthaka

  • Install NW7.0 ABAP Trial SP12 on Windows. how use/unpack these two files

    Hello,
    I tried to install the NW7.0 ABAP Trial SP12 on Windows XP. The download work properly I hope.
    I extracted the part1-file to separate folder.
    I have no idea how the second file can be extracted (part2).
    So I started the setup and it worked well, but the programm to get the hardware key isn't on in the file system "saplicense.exe".
    now my question is how do I have to use these two download files (extract, join together, how?) to get a complete installation source?
    Can anybody help me?
    Thanx in advance
    Hans

    Hi Hans,
    When you extract the part1 the others are extracted too.
    I use the program Winrar and works fine.
    What program are you using to extract?

  • How to query in two tables

    Hi all
    How to query in two different tables? Should i use the following
    SET_BLOCK_PROPERTY('ins1', ONETIME_WHERE, 'serial = ''' || :GLOBAL.CODE || ''''); this is working but for one table if i want for both tables and and it display the related information in main form , eg..tables are referenced by primary key constraint and foreign key.
    sarah
    Edited by: SarahSarahSarah on Sep 1, 2009 4:59 AM

    For the primary forign key tables no need to create two forms use the master detail functionality of forms.
    create a Master/Detail form using the DEPARTMENT, EMPLOYEE tables.
    DEPARTMENTwill be the Master block and EMPLOYEE will be the Detail block. The join condition between the two tables is where the deptno column of DEPARTMENT is equal to the deptno column of EMPLOYEE .
    The following is an outline of the steps:
    Create the DEPARTMENT data block and include the all the fields on the form.
    Use the Form layout and only display one record at a time.
    Go back to the Object Navigator and select the "Data Blocks" tree. Make sure the DEPARTMENT block is not selected and run the Data Block Wizard again.
    Create the EMPLOYEE data block and include all of the columns.
    For the master/Detail relationship, de-select the "auto" option, use a Join relationship, choose the DEPARTMENT data block as the Master and set EMPLOYEE.DEPTNO= DEPARTMENT.DEPTNO for the join relationship.
    In the Layout Wizard for the Dependents data block, display all of the fields
    Choose a Tabular style and display 4 up to records at once with 0 space between each record.
    Save the form and run the form.
    Thanks

Maybe you are looking for