insert data from one table to a single column another table with no relation

mradul

I need to insert data into a table which is already having some data into it now a new column is added to the table and we need to insert/update the new column with values from another table.

create table target_tab (fname varchar2(20), acc_no number);

insert into target_tab values('Anybody',121);
insert into target_tab values('Somebody',122);
insert into target_tab values('Nobody',123);

alter table target_tab add sc_vlan varchar2(20);

create table source_tab (rsrc_nm varchar2(20));

insert into source_tab values ('2839_124');
insert into source_tab  values('2839_125');
insert into source_tab values('2839_126');
insert into source_tab values('2840_131');
insert into source_tab values('2841_132');
insert into source_tab values('2840_134');
insert into source_tab values('2840_127');

Now we need to insert the values of rsrc_nm column from source_tab to the scvlan column of the target_tab.

Note : we do not have any relationship between both tables and like we see in example that target_tab has less rows than source_tab then only row which are in target_tab should be insert/update with unique values from source_tab. We can fetch any value from source_tab.

 declare 

  cursor c_1 is select rsrc_nm 
            from source_tab                                                                                                                                                   
              where rownum <= (select count(1)                                                                                                                                                 
               from target_tab);                                                                                                                                                                

  type t_tab is table of c_1%rowtype index by pls_integer;
  l_tab t_tab;

begin

  open c_1;
  fetch c_1 bulk collect into l_tab;
    for i in 1..l_tab.count
    loop  

      update target_tab set sc_vlan = l_tab(i).rsrc_nm
      where sc_vlan is null
      and rownum = 1;
      commit;
    exit when c_1%notfound;
  end loop;
  close c_1;

EXCEPTION
  WHEN OTHERS THEN
    dbms_output.put_line(SQLERRM);
    dbms_output.put_line(dbms_utility.format_error_backtrace());    
end;
Ponder Stibbons

I think You could use merge like this:

merge into target_tab t
using (
  select s.rsrc_nm, t.rwd
    from (select row_number() over (order by acc_no)  rn, t.*, rowid rwd 
            from target_tab t) t
    join (select row_number() over (order by rsrc_nm) rn, s.* 
            from source_tab s) s
      on t.rn =s.rn
    ) s
on (t.rowid = s.rwd)
when matched then update set t.sc_vlan = s.rsrc_nm

Sort data in both tables on any columns, match them using row_number and update.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Procedure to insert data from one column into two columns in another table

From Dev

MySQL insert into column data from another table

From Dev

Transfer data from one table to another on insert

From Dev

Insert Data From One Table To Another - MySQL

From Dev

Insert data from column of a table and store to column another table

From Dev

Laravel insert data to one to one relation table

From Dev

How to select data from one table and insert it into another table with a new column

From Dev

select data from one column based on another column's condition in the same table and insert that resulting data to another table

From Dev

Procedure to insert a data from one table to another table after calculations

From Dev

Insert Data From One Table to Another Table with default values

From Dev

Insert Data From One Table to Another Table and Add New Values

From Dev

Insert id's from one table to another based on another column

From Dev

mysql insert unique values from one column to a column of another table

From Dev

Insert multiple rows with single a query from one table into another in Oracle

From Dev

insert statement one column from another table rest of the columns is values

From Dev

select multiple column from one table and insert into another as rows

From Dev

Insert INTO from one table to another and change column values

From Dev

SQL insert data dynamic column name from another table

From Dev

MSSQL Insert Data From One Table To Another With Fixed Value

From Dev

INSERT data from one table to another where ids match

From Dev

Insert Data from one table to another leaving the already existing rows

From Dev

Insert data from a table to another one in MySQL with where condition

From Dev

want to insert data from one table into another in php?

From Dev

insert data from one table to another - Not Working - possible bug?

From Dev

SQL insert data into a table from another table

From Dev

MySQL - Insert table data from another table

From Dev

SQL insert data into a table from another table

From Dev

MySQL - Insert table data from another table

From Dev

Oracle update one table in relation to entering data into another table

Related Related

  1. 1

    Procedure to insert data from one column into two columns in another table

  2. 2

    MySQL insert into column data from another table

  3. 3

    Transfer data from one table to another on insert

  4. 4

    Insert Data From One Table To Another - MySQL

  5. 5

    Insert data from column of a table and store to column another table

  6. 6

    Laravel insert data to one to one relation table

  7. 7

    How to select data from one table and insert it into another table with a new column

  8. 8

    select data from one column based on another column's condition in the same table and insert that resulting data to another table

  9. 9

    Procedure to insert a data from one table to another table after calculations

  10. 10

    Insert Data From One Table to Another Table with default values

  11. 11

    Insert Data From One Table to Another Table and Add New Values

  12. 12

    Insert id's from one table to another based on another column

  13. 13

    mysql insert unique values from one column to a column of another table

  14. 14

    Insert multiple rows with single a query from one table into another in Oracle

  15. 15

    insert statement one column from another table rest of the columns is values

  16. 16

    select multiple column from one table and insert into another as rows

  17. 17

    Insert INTO from one table to another and change column values

  18. 18

    SQL insert data dynamic column name from another table

  19. 19

    MSSQL Insert Data From One Table To Another With Fixed Value

  20. 20

    INSERT data from one table to another where ids match

  21. 21

    Insert Data from one table to another leaving the already existing rows

  22. 22

    Insert data from a table to another one in MySQL with where condition

  23. 23

    want to insert data from one table into another in php?

  24. 24

    insert data from one table to another - Not Working - possible bug?

  25. 25

    SQL insert data into a table from another table

  26. 26

    MySQL - Insert table data from another table

  27. 27

    SQL insert data into a table from another table

  28. 28

    MySQL - Insert table data from another table

  29. 29

    Oracle update one table in relation to entering data into another table

HotTag

Archive