How to update these two tables

Hello

I have two tables (in Oracle 11 g R2) and need to lock some lines in each of them for the update...
Here is the sample data and the result after update, please help me for update instructions.
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;
/

Kind regards.
Al

Tags: Database

Similar Questions

  • How the sum of these two tables?

    Hello

    I have two tables

    DESC ACTIVITE_EXCEP_FAITE

    NUMBER OF FICHE_ID

    NUMBER OF SERVICES_ID

    NUMBER OF ACTIVITES_EXCEPTIONNELLES_ID

    NUMBER OF TIME

    NUMBER OF ACTIVITE_EXCEP_FAITE_ID

    DESC ACTIVITE_FAITE

    NUMBER OF FICHE_ID

    NUMBER OF ACTIVITES_ID

    NUMBER OF SECTEURS_ID

    LENGTH NUMBER (8.2)

    NUMBER OF ACTIVITE_FAITE_ID

    NUMBER OF TYPE_ACTIVITE_ID

    I want to add the column to the DURATION of the two tables when they get the same FICHE_ID. FICHE_ID is not a join field.

    I've tried this application, but it gives result when two recordings of the two table share the same number FICHE_ID.

    SELECT AF. FICHE_ID, SUM (NVL (AF. LENGTH, 0)) + SUM (NVL (AEF. LENGTH, 0))

    OF AF, ACTIVITE_EXCEP_FAITE AEF ACTIVITE_FAITE

    WHERE AF. FICHE_ID = AEF. FICHE_ID

    GROUP BY AF. FICHE_ID;

    Can you help me write this selection?

    Thank you very much for your help!

    Like this?

    select nvl(af.fiche_id, aef.fiche_id) fiche_id
         , sum(nvl(af.duree,0)) + sum(nvl(aef.duree,0)) duree
      from activite_faite af
      full join
           activite_excep_faite aef
        on af.fiche_id = aef.fiche_id
     group by nvl(af.fiche_id, aef.fiche_id);
    
  • How to export diferent two tables of contents to epub?

    I have been reseraching french forum of responses to the question I have but could ' t find anything for my case. I am a book of plants convert epub. The layout that gave me was the printed version of the book, I use Indesign 5.5 to convert the epub book. For the results of the tests, I use Calibre and a Kindle.

    I was n style texts and images and everything is great except for the table of contents. The author needs to a content page with all the main chapters (h1 title) and an index at the end of the book with all the names of the plants: this has two titles an eglish followed by the Latin version (h2-h3-Latin and English).

    I've created two different styles of toc, one with only the chapter headings, would be at the table of contents at the beginning and the second style is for the index of plants: with the h2 and h3 titles (I checked the alphabetical button in both to have the section index correctly).

    I have two questions: the first is when I try to place each table of contents at the beginning and at the end of the book. On the page, it looks fine, but when I export to epub, I can choose only in the export Panel, one table of contents. The resulting epub has the same table at the beginning and at the end I tried several times to change the position of the tables, export settings, but I never managed to have two different tables on the same epub.

    The second question is when I had expoorted one of these tests using the style 2 for the index, I get an epub with the two tables of the same material, the index is in alphabetical order, but instead of ordering all the plants of a to z he ordered their "pairs" as they appear on the texts (first English title plants the latin equivalent of a second). How can I do to make this index order alphabetically all the h2 and h3? I need all the a, b, c,... indendently of their position in the page.

    Hope the above makes sense.

    Thanks in advance.

    Thanks for your offer of Ariel, I'm sure is a good script, but I can't affford it right now.

    See you soon

  • How to migrate from two tables

    Hello

    I have three tables.
    Issue table 1)
    QID text
    1. What is the capital of the India?
    2 who is our PM?

    (2) table answer
    response to help qid
    1 1 Chennai
    2 1 Delhi
    3 1 Bangulore
    4 2 kamal Singh
    5 2 Sonia
    6 2 Manmohan Singh

    (3) table c
    question A B C qid
    1. What is the capital of the India? Bangulore of Delhi from Chennai
    2 who is our PM? Kamal Singh Sonia Manmohan Singh

    Please tell me how to insert data from the first two tables in the third table by using a procedure?
    I tried, I get answers in the columns...

    Thank you
    SAI

    Look at this post.

    (1) I used row_number to retrieve the columns, for which the data are intended
    (2) I used the PIVOT to convert data into columns.

    Re: How to migrate two tables

  • How to merge these two files?

    Hello Adobe Dreamweaver Forums.

    My Question:

    How to merge the two following documents of smaller.js in a document unique largest .js?


    I took action:

    Copy and paste.

    Problem:
    Scripts is no longer loading in the larger document, still charge when connected separately.

    DOWNLOAD:

    Dropbox - merge help (05/16/15) .zip

    Includes:

    "Main.js" - 51 lines, 30KB

    'Fusion-in-hand-1 Jquery.nav' - 2 lines, 1 KB

    "Fusion-in-hand-2 settings" - 2 lines, 1 KB

    Note:

    If the necessary script files can be unpacked using: Unminify JS, CSS and HTML Code

    Thanks for reading and I appreciate the help.

    Not sure what you mean "files are not can-do. In the example, I have even included the jQuery library in goldenrolls.js.

    The only problem you will have is the time it takes to load the page. Take a look at the size of the header image which is ko 5 769,21 with the dimensions of 3, 072px × 2, 304px (scaling 1, 180px × 885px).

    Once the page is loaded, everything works perfectly.

  • How to restore these backup tables

    Hello pls provide detailed instructions on how to restore such a table regarding the example tables: PSWEB backup?

    What database? What backup are you talking about?

    Nicolas.

  • How can I join two tables that belong to two different databases?

    I can build a SQL query in SQL Server Management Studio, which performs a join of a table in a database with a table in another database.  I have a good 'read' name to username/password for the two databases.

    How to do the join in ColdFusion?  The tag < cfquery > requires a unique data source.  I could do like two queries and through comparing, or maybe even some use dbtype = query, but it would be horribly less effective than a direct join.

    I could place the SQL statement in a stored procedure, but that seems like overkill - all the necessary number of fields in the "WHERE" clause will change based on the logic of the ColdFusion program.

    Does anyone know of a way to do this with < cfquery >?

    How do you take in SQL Server Management Studio?  You must do something that I'm not aware, if what you're doing here does not work in a block.

    The way I did in the past is just to fully qualify the names of the tables.  Server.databaseOwner.database.table know if I remember correctly.

    This does not require that the database connected to the name of data source is configured to be allowed to connect to the database.

  • UPDATE involving two tables...

    Hi all

    I need to write an UPDATE statement similar to the following, but I'm not sure how to do it:
    UPDATE work_desc SET work_code = 'TRAN01' WHERE work_code = 'AER01' AND reg_code = 'L' AND
      (SELECT voltage FROM Request WHERE request.region_no = work_desc.region_no) LIKE '69%' OR
      (SELECT voltage FROM Request WHERE request.region_no = work_desc.region_no) LIKE '450%' OR
      (SELECT voltage FROM Request WHERE request.region_no = work_desc.region_no) LIKE '735%'
    What I have to do is update the work_code in the work_desc table with the tension of the query table that begin with '69', ' 450 'or ' 735'.

    Any idea on how to write this update statement?

    Thank you.

    Maybe like this?

    UPDATE work_desc
    SET    work_code = 'TRAN01'
    WHERE  work_code = 'AER01'
    AND    reg_code = 'L'
    AND    exists (select 1
                   from   request
                   where  request.region_no = work_desc.region_no
                   and   (request.voltage like '69%'
                          or request.voltage like '450%'
                          or request.voltage like '735%'
                         )
                  )
    ;
    
  • How to combine these two scripts

    I have a script that displays the host name, the name of the data store and the NAAid in a csv file

    $VMhosts = get-datacenter xxxxxxxx | Get-VMHost

    $data = {foreach ($vmhost to $VMhosts)

    $vmhost | Get-Datastore.  Select @{N = "$vmhost"; E = {$vmhost. Name}},name,@{N="CanonicalNames '; E = {[string]: join (",",($_.ExtensionData.Info.Vmfs.Extent |)} %{$_. DiskName}))}}

    Get-VMHost $vmhost | Get-ScsiLun - CanonicalName $naa

    }

    $data | Export-Csv "C:\report.csv" - NoTypeInformation - UseCulture

    Then I have another script that if I put the ID NAA in a txt file it read and give me all the multitracks among other parameters

    $naaids = get-content "c:\ctluns.txt".

    {foreach ($naa to $naaids)

    Get-Cluster ctclt | Get-VMHost | Get-ScsiLun - CanonicalName $naa | Export-Csv "c:\ctpaths.csv".

    }

    I want a script that gives me all the four elements in a csv file.  Name of host, data store name, Id NAA and path.    I am struggling with getting this works specfically feeding the NAA ID from a script in the script line two that pulls the info multichannel.  Can anyone help?

    If you want only the MultipathPolicy, you can remove the rest of the attributes.

    Note: This script will take a large amount of time to run if you have to many data warehouses.

    Here it is:

    
    $VMhosts = Get-datacenter xxxxxxxxxx | Get-VMHost
    $output=@()
    foreach($vmhost in $VMhosts){
      $datastores = $vmhost | Get-Datastore
      $paths =@()
      foreach($dt in $datastores){
      $extentDatastore = $dt.ExtensionData.Info.Vmfs.Extent
      $extentDatastore| %{
            $path = $vmhost |Get-ScsiLun -CanonicalName $_.DiskName
    
            $ConsoleDeviceName = $path.ConsoleDeviceName
            $LunType = $path.LunType
            $CapacityGB = $path.CapacityGB
            $MultipathPolicy = $path.MultipathPolicy
            $RuntimeName = $path.RuntimeName
            $Model = $path.Model
            $Vendor = $path.Vendor
            $tempOutput = ""| select VMHost,DatastoreName,CanonicalNames,ConsoleDeviceName,LunType,CapacityGB,MultipathPolicy,RuntimeName,Model,Vendor
            $tempOutput.VMHost = $vmhost.Name
            $tempOutput.DatastoreName = $dt.Name
            $tempOutput.CanonicalNames = $_.DiskName
            $tempOutput.ConsoleDeviceName = $ConsoleDeviceName
            $tempOutput.LunType = $LunType
            $tempOutput.CapacityGB = $CapacityGB
            $tempOutput.MultipathPolicy = $MultipathPolicy
            $tempOutput.RuntimeName = $RuntimeName
            $tempOutput.Model = $Model
            $tempOutput.Vendor = $Vendor
            $output+= $tempOutput
    
      }}
    }
    $output| Export-Csv "c:\ctpaths.csv" -NoTypeInformation -UseCulture
    
  • Strange problem with these two tables

    Can someone tell me what is the problem here?

    1)

    Select * from table_batch where keyvar not in (select keyvar_orig from table_master);

    no selected line

    2)

    Select * from table_batch where KEYVAR = '03657795QQ ';

    RECORD_ID KEYVAR

    ---------- ----------

    2.0E + 13 03657795QQ

    3)

    Select * from table_master where KEYVAR_ORIG = '03657795QQ ';

    no selected line

    Hello

    For your query (1) try:

    Select * from table_batch where keyvar not in (select keyvar_orig from table_master WHERE KEYVAR_ORIG IS NOT NULL);

    See you soon

    Mike

  • How to combine these two queries:

    Select K.ID, K.DOWN_STATUS, K.UP_STATUS,(K.DOWN_STATUS+K.UP_STATUS) TOT
    from (select ID, SUM(CASE WHEN S_STATUS = 'DOWN' THEN 1 ELSE 0 END) DOWN_STATUS,
    SUM (CASE WHEN S_STATUS = 'UP' THEN 1 ELSE 0 END) UP_STATUS
    of test_info where CONNECT_TIMESTAMP BETWEEN ' 2012-03-27 00:00:00 ' AND ' 2012-03-27 23:59:59 '
    Group by ID) k

    Select r.ID,(r.DOWN_STATUS+r.UP_STATUS) TOTAL, (r.DEVICE_ID |) '|' || Str (r.UP_STATUS) | ' ~ ' || r.DEVICE_ID + ", ' | Str (r.DOWN_STATUS)) as deviceinfo
    from (select ID, device_id, SUM(CASE WHEN S_STATUS = 'DOWN' THEN 1 ELSE 0 END) DOWN_STATUS,
    SUM (CASE WHEN S_STATUS = 'UP' THEN 1 ELSE 0 END) UP_STATUS
    of test_info where CONNECT_TIMESTAMP BETWEEN ' 2012-03-27 00:00:00 ' AND ' 2012-03-27 23:59:59 '
    Group by ID, device_id) r

    Maybe this?

    select K.ID||'/'||K.DOWN_STATUS||'/'||K.UP_STATUS||'/'||(K.DOWN_STATUS+K.UP_STATUS) strout
    from (select ID,SUM(CASE WHEN S_STATUS = 'DOWN' THEN 1 ELSE 0 END) DOWN_STATUS,
    SUM(CASE WHEN S_STATUS = 'UP' THEN 1 ELSE 0 END) UP_STATUS
    from test_info where CONNECT_TIMESTAMP BETWEEN '2012-03-27 00:00:00' AND '2012-03-27 23:59:59'
    group by ID) k
    union all
    Select r.ID||'/'||(r.DOWN_STATUS+r.UP_STATUS)||'/'||(r.DEVICE_ID|| '|' ||STR(r.UP_STATUS)|| ' ~ ' ||r.DEVICE_ID+ ' , ' ||STR(r.DOWN_STATUS))
    from (select ID,device_id ,SUM(CASE WHEN S_STATUS = 'DOWN' THEN 1 ELSE 0 END) DOWN_STATUS,
    SUM(CASE WHEN S_STATUS = 'UP' THEN 1 ELSE 0 END) UP_STATUS
    from test_info where CONNECT_TIMESTAMP BETWEEN '2012-03-27 00:00:00' AND '2012-03-27 23:59:59'
    group by ID,device_id) r;
    
  • Join of two tables in two different schemas

    Hi all

    I have an obligation to join two tables on two different schemas. How to join these two tables in the object view.

    Thanks in advance.

    Concerning
    Kaushik Guillaumin

    You can do just that using schema name in the +' view object.table name ' + according to the query object and also grant select the another schema to this schema user

    ex
    you need schem is test and another Act you need to a view object based on the test to join a table on shcema Act

    you write semply object sql view code:

    Act.table name

    and you can also give him select statement on Bill schem table to test

    concerning

  • How to match columns from two tables with

    Hello:
    I have two tables as below:

    Table1::(Base Table)
    Country | Prefix | Prefix_Length
    Travel | 001 | 3
    CountryB. 0012 | 4
    PaysC | 00443 | 5
    CountryD | 0091 | 4

    :(Detail Table) table2
    The population | Area | Prefix
    500 | AreaA | 0015921
    1000 | AreaB | 00122
    400. AreaC. 00443743
    300. ALIS | 0091333
    100. AreaA | 001

    I need to match these two tables with prefix columns (whose length is not fixed in the two tables: but it starts with 00 in the two tables). Two different countries the prefix may be similar up to a certain length. Thus, Prefix_Length can be used to determine (exactly) how much time should be taken in the search of Table2.

    Output:
    Country | Prefix | Area | Population
    Travel | 001 | AreaA | 600
    CountryB. 0012 | AreaB | 1000
    PaysC | 00443 | AreaC. 400
    CountryD | 0091 | ALIS | 300

    Please help me with your valuable comments.

    -Tender

    Try this

    with base_table as (
                        select 'CountryA' country,'001' prefix,3 prefix_length from dual union all
                        select 'CountryB','0012',4 from dual union all
                        select 'CountryC','00443',5 from dual union all
                        select 'CountryD','0091',4 from dual
                       ),
       detail_table as (
                        select 10 no_of_call,'0015921' prefix from dual union all
                        select 3,'00122' from dual union all
                        select 50,'00443743' from dual union all
                      select 50,'00443643' from dual union all
                        select 300,'0091333' from dual union all
                        select 60,'001' from dual
                       ) 
    
    SELECT  country,
            prefix,sum(no_of_call)
       FROM (
             select  country,
            b.prefix,no_of_call,
            decode(no_of_call,lead(no_of_call,1,0) over(partition by no_of_call order by b.prefix,no_of_call),'y','n') y_or_no
      from  base_table b,
            detail_table d
      where b.prefix = substr(d.prefix,1,prefix_length))
      where y_or_no !='y'
      group by  country,
            prefix
      order by country,
            prefix;
    

    Published by: Vi on 20 February 2012 01:07

  • How to compare data from the two table column by column and connect the result compared to the separate table

    Hello experts...

    I'm new to this forum and Oracle coding complex.

    I have a task in which I want to compare two tables with the same structure, same columns. My Oracle database is 11g Release 2. I want this column-by-column comparison and save the result in a separate table. (say result_table)

    The structure of the table would be as follows:

    SQL > desc emp;

    Name                                      Null?    Type

    ----------------------------------------- -------- ----------------------------

    EMPNO NOT NULL NUMBER 4

    ENAME VARCHAR2 (10)

    JOB                                                VARCHAR2(9)

    MGR                                                NUMBER(4)

    HIREDATE DATE

    SAL                                                NUMBER(7,2)

    COMM                                               NUMBER(7,2)

    DEPTNO NUMBER (2)

    SQL > emp_comp desc;

    Name                                      Null?    Type

    ----------------------------------------- -------- ----------------------------

    EMPNO NOT NULL NUMBER 4

    ENAME VARCHAR2 (10)

    JOB                                                VARCHAR2(9)

    MGR                                                NUMBER(4)

    HIREDATE DATE

    SAL                                                NUMBER(7,2)

    COMM                                               NUMBER(7,2)

    DEPTNO NUMBER (2)

    Both of the tables EMPNO is the primary key.

    I want to compare these two tables based on the EMPNO (EMPNO even is both tables).

    The result log table would be as follows:

    SQL > create table result_table

    2 (check_sr_no, number (5),)

    table_name 3 varchar2 (30),

    ROW_ID 4 varchar2 (20).

    column_name 5 varchar2 (20).

    6 column_data varchar2 (1000).

    compared_by 7 varchar2 (10));

    Table created.

    SQL > alter table result_table

    2 Add the constraint result_table_pk

    3 primary key (check_sr_no);

    Modified table.

    The name of the table and the name of the column will not be hardcoded (based parameter by user)

    I tried to use the dbms_comparison package, but do not find comparison of column to column.

    I also used by using the query operator LESS. It's showing the difference in rank.

    I also went to https://asktom.oracle.com/pls/apex/f?p=100:11:0:P11_QUESTION_ID:2151582681236

    Your help will be very appreciated.

    Thank you

    DK

    Tom Kyte has answered this question MANY times over the years. Here are links for its solution

    https://asktom.Oracle.com/pls/Apex/f?p=100:11:0:P11_QUESTION_ID:4934799800346640847

  • How to link two tables with form

    Hello

    Here's what I want to do

    I have a table-> Student (rollno, name)

    I also have these two tables (rollno, marks) Math Physics (rollno, marks)

    now, I want to have a form in my page (in which I can navigate school records) and two tables in the adf (Math, physical). When selecting a certain record of the form, I want to see data associated with these two tables.

    I try to make the link display but works for me...

    Please help me on this.

    -Usman

    -Open the module of the application

    -Go to the model data tab

    -You will see all the your available on the left and your already added to the AM to the right

    -For the your who are 'master' for others your (in your case, student VO's master VO of Math and physics VO), you will see a (+) monkey behind it

    -Expand this node and you will see the math and physics your.

    -Math VO on the left and the VO student on the right then click on the button (>) to shuttle Math VO VO of the student child and give it a name

    -Repeat step for physics VO

    After completing the above steps and save, go back to your jsf (or jspx or...) and in the data tab control you will see this student VO is now two children, Math and physics. Use WHAT YOUR theses to create your tables for details.

    Kind regards.

Maybe you are looking for