Copy and update data from one table to the new table by using the Merge command

Hello

Can someone tell me please how to do this?
I have a table: EMP (ename, sal, empno (pk), dept) another table is EMP_CLASS ((pk) empno, ename)

I want to insert corresponding lines of emp to table emp_class if this line does'nt exists in emp_class
otherwise it will be update info by corrsponding emp_class table as well as remove of emp_class according to the emp delete operation.






Thank you
Madam.

Published by: Nico on August 12, 2010 22:59

Have changed. PLS, run once more...

MERGE INTO emp_class
With the HELP of emp
WE (emp.empno = emp_class.empno)
WHEN MATCHED THEN
UPDATE
SET emp_class.ename = emp.ename,
emp_class. DEPTNO = emp.deptno,
emp_class. Class = decode(emp.deptno,10,1,20,2,30,3,40,4)
DELETE
WHERE emp_class.empno = emp.empno
WHEN NOT MATCHED THEN
INSERT (emp_class.empno, emp_class.name, emp_class.deptno, emp_class.class)
VALUES (emp.empno, emp.ename, emp.deptno, decode(deptno,10,1,20,2,30,3,40,4));

P.S. Not tested

Thank you
Florian...

Published by: Florian on August 13, 2010 01:53

Tags: Database

Similar Questions

  • Inserting data from one table to another table

    Hello

    I have the following SQL where I am updating a table by adding new data from another table, but without success.

    INSERT INTO
    () TOP_PROSPECTS
    COMMON_ID
    DATE_ADDED
    REVIEW_RANK
    EVAL_DATE
    PM_ASSIGN
    WHY_NOTES)
    SELECT
    t.COMMON_ID
    t.DATE_ADDED
    t.REVIEW_RANK
    t.EVAL_DATE
    t.PM_ASSIGN
    t.WHY_NOTES
    Of
    TEMP_IVAN_MARY t
    WHERE
    COMMON_ID <>t.COMMON_ID

    Any suggestions?

    Thank you.

    Published by: user13822709 on August 14, 2012 09:14

    Published by: user13822709 on August 14, 2012 09:15

    Is that what you're trying to do with the insert. I think there may be a sign {noformat}<{noformat}{noformat}>{noformat} missing in the where clause. This site eat those, so you need to use the equivalent! = post here.

    If I'm wrong about the missing trader, then it looks like you want to insert rows in temp_ivan_mary that are not already in top_prospects. If Yes, then you need something like:

    insert into top_prospects
       (common_id, date_added, review_rank, eval_date, pm_assign,
        why_notes)
    select t.common_id, t.date_added, t.review_rank, t.eval_date,
           t.pm_assign, t.why_notes
    from temp_ivan_mary t
    where t.common_id not in (select common_id from top_prospects
                              where common_id is not null)
    

    Function index and data available volumnes etc. then a mergr can be more effective. Something like:

    merge into top_prospects p
       using (select common_id, date_added, review_rank, eval_date,
                     pm_assign, why_notes
              from temp_ivan_mary) t
       on (p.common_id = t.common_id)
       when not matched then
          insert (common_id, date_added, review_rank, eval_date, pm_assign,
                  why_notes)
          values (t.common_id, t.date_added, t.review_rank, t.eval_date,
                  t.pm_assign, t.why_notes)
    from temp_ivan_mary t
    

    John

  • Can I select and copy and paste text from a browser with the pencil of Apple?

    With the pencil of Apple on a new iPad Pro, I am not able to select, copy and paste text from a web page in Chrome or Safari. Y at - it a special order? A related question (not with a pencil): how to save files in PDF format.

    Well, it seems to be workin now. Never mind.

  • Copy data from one table to the new table

    Hello

    I copy data from columns of a table to another table and my query is
    INSERT INTO TestTable (empno, empName, deptno)
    SELECT empno, empname, deptno
    WCP

    and it works fine but I want to insert a data column in testTable based on emp.deptno
    as if emp.deptno = 10 it will add 1 to the testtable.class column,
    If emp.deptno = 20 it will add 2-column testtable.class,
    If deptno = 30 it will add 3 to the testtable.class column,


    TestTable structure is
    EMPNO NUMBER (20)
    ENAME VARCHAR2 (50)
    DEPTNO NUMBER (20)
    CLASS NUMBER (20)

    Can someone please help me write this procedure.
    Thank you

    You can use a case statement as

    INSERT INTO TestTable (empno, empName,deptno,class_column )
    SELECT empno, empname,deptno, case when emp.deptno=10  then 1
                                       when emp.deptno=20  then 2
                                       when emp.deptno=30  then 3
                                  end
    FROM emp
    

    See you soon
    Katia

  • Load data from one table to another

    Hi all

    Please help me for my sous condition.

    Oracle version: Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod

    Rules
    -------
    (1) first two city must be loaded in city_1 and city_2.
    (2) if there are more than two cities, only two must be loaded first.
    (3) if only a city_1 is set, it must be loaded in city_1.

    drop table seven_cities;
    
    create table seven_cities
    (id     number,
     city_1  varchar2(100),
     city_2  varchar2(100),
     city_3  varchar2(100),
     city_4  varchar2(100),
     city_5  varchar2(100),
     city_6  varchar2(100),
     city_7  varchar2(100)
     );
     
     
    insert into seven_cities(id,city_1,city_2,city_3,city_4,city_5,city_6,city_7)
    values (1,'New York',null,null,null,'Chicago',null,null);
    
    insert into seven_cities(id,city_1,city_2,city_3,city_4,city_5,city_6,city_7)
    values (2,null,'Hyderabad','Delhi','Mumbai',null,null,null);
    
    
    insert into seven_cities(id,city_1,city_2,city_3,city_4,city_5,city_6,city_7)
    values (3,null,null,null,null,null,null,'London');  
    
    insert into seven_cities(id,city_1,city_2,city_3,city_4,city_5,city_6,city_7)
    values (4,null,null,null,'Sydeny','Melbourne','Paris','Berlin');
    
    commit;
    
    select * from seven_cities;
    
    drop table two_cities;
    
    create table two_cities
    (id number,
     city_1  varchar2(100),
     city_2  varchar2(100));
     
     Expected data to be loaded
     ----------------------------
    
    id       city_1          city_2
    1     New York             Chicago
    2     Hyderabad             Delhi
    3     London          null
    4     Sydney          Melbourne
    Thank you
    Rambeau

    The one I have already posted in response to your message duplicate (next time please do not submit duplicate messages):

    select  id,
            coalesce(city_1,city_2,city_3,city_4,city_5,city_6,city_7) city_1,
            nvl2(
                 city_1,
                 coalesce(city_2,city_3,city_4,city_5,city_6,city_7),
                 nvl2(
                      city_2,
                      coalesce(city_3,city_4,city_5,city_6,city_7),
                      nvl2(
                           city_3,
                           coalesce(city_4,city_5,city_6,city_7),
                           nvl2(
                                city_4,
                                coalesce(city_5,city_6,city_7),
                                nvl2(
                                     city_5,
                                     coalesce(city_6,city_7),
                                     nvl2(
                                          city_6,
                                          city_7,
                                          null
                                         )
                                    )
                               )
                          )
                     )
                ) city_2
      from  seven_cities
    / 
    
     ID CITY_1     CITY_2
    --- ---------- ----------
      1 New York   Chicago
      2 Hyderabad  Delhi
      3 London
      4 Sydeny     Melbourne
    
    SQL>
    

    And this solution, you are prompted by the solution of Tubby:

    with t as (
               select  id,
                       row_number() over(partition by id order by rownum) rn,
                       column_value city_1,
                       lead(column_value) over(partition by id order by rownum) city_2
                 from  seven_cities,
                       table(sys.OdciVarchar2List(city_1,city_2,city_3,city_4,city_5,city_6,city_7))
                 where column_value is not null
              )
    select  id,
            city_1,
            city_2
      from  t
      where rn = 1
    /
    
     ID CITY_1     CITY_2
    --- ---------- ----------
      1 New York   Chicago
      2 Hyderabad  Delhi
      3 London
      4 Sydeny     Melbourne
    
    SQL> 
    

    And if there is a concern on the use of the items in the collection without papers are read in the order behaviour, just create object & nested table types:

    create or replace
      type num_str_obj
        as object(
                  n number,
                  s varchar2(4000)
                 )
    /
    create or replace
      type num_str_list
        as table of num_str_obj
    /
    

    Now:

    with t as (
               select  id,
                       row_number() over(partition by id order by n) rn,
                       s city_1,
                       lead(s) over(partition by id order by n) city_2
                 from  seven_cities,
                       table(
                             num_str_list(num_str_obj(1,city_1),
                                          num_str_obj(2,city_2),
                                          num_str_obj(3,city_3),
                                          num_str_obj(4,city_4),
                                          num_str_obj(5,city_5),
                                          num_str_obj(6,city_6),
                                          num_str_obj(7,city_7)
                                         )
                            )
                 where s is not null
              )
    select  id,
            city_1,
            city_2
      from  t
      where rn = 1
    /
    
     ID CITY_1     CITY_2
    --- ---------- ----------
      1 New York   Chicago
      2 Hyderabad  Delhi
      3 London
      4 Sydeny     Melbourne
    
    SQL> 
    

    SY.

  • Corresponding to a set of data from one table to another

    I have a table - Table_1 with columns A, B, C, D, I have another (Table_2) table with columns B, C, D.

    I try to get all the rows in Table_1 where the values correspond to columns B, C, D.

    The catch here is that returned from TABLE_1 lines should be the same number of rows as in TABLE_2 and the value in TABLE_1 from 'A' should be the same for all these lines. So--Table_1 could return several sets of data where the VALUE of A is different for each SET but - the 'GAME' must match all other values in TABLE_2 exactly (including the number of lines for each game)...

    Any help is greatly appreciated. I hope that I said above clearly enough.

    -create tables

    create table t1 (a varchar2 (10), varchar2 (10) b, c varchar (10), varchar2 (10)) d;
    create table t2 (varchar2 (10) b, varchar2 (10) c, varchar (10)) d;

    -create data, 5 games in t1 with 2 correspondents sets

    insert into t2 values ('1 ', '2',' 3');
    insert into t2 values ('2 ', '2',' 3');
    insert into t2 values ('3 ', '2',' 3');
    insert into t1 values ('a', '1', ' 2 ', 3');
    insert into t1 values ('a', '2', ' 2 ', 3');
    insert into t1 values ('a', '3', ' 2 ', 3');
    insert into t1 values ('b', '1', ' 2 ', ' 3');
    insert into t1 values ('b', '2', ' 2 ', ' 3');
    insert into t1 values ('c', '1', ' 2 ', ' 3');
    insert into t1 values ('c', '2', ' 2 ', ' 3');
    insert into t1 values ('c', '3', ' 2 ', ' 3');
    insert into t1 values ('c', '4', ' 2 ', ' 3');
    insert into t1 values (has "', 1 ', ' 2 ', ' 3'");
    insert into t1 values (has "', 2 ', ' 2 ', ' 3'");
    insert into t1 values (has "', ' 3, ' 2 ', ' 3'");
    insert into t1 values ('e', '4', ' 2 ', ' 3');
    insert into t1 values ('e', '5', ' 2 ', ' 3');
    insert into t1 values ('e', '6', ' 2 ', ' 3');

    Select * from t1 where one (not in
    Select a from t1 where does not exist (select 1 from t2 where b = t1.b and c = t1.c and d = t1.d)
    Union
    Select one of (select one, count (*) NTC group by a t1) where NTC! = (select count (*) from t2)
    )

    A B C D
    ---------- ---------- ---------- ----------
    1 2 3
    2 2 3
    3-2-3
    1 2 3 d
    2 2 3 d
    3 2 3 d

    6 selected lines

  • Load data from one table into another table of 2 different schemes

    Hello

    We have a requirement to insert data into a table in a schema of a table that is in other schemas in ODI.

    We are able to do so by creating interfaces and mappings. But we now expect to do using SQL instead of ODI interfaces.

    Is it possible to do this using the SQL statements we have source and target data sources defined in ODI.

    should be like "Insert into src.table select * from tar.table'"

    Thank you...

    Hello

    If you try to load a db to another db using sql (sql free hand) instead of interfaces instructions.

    It is possible using procedures odi too.

    1. Select the project (designer navigator) odi mode

    2. create the new procedure. Then add the command.

    in this command on the source command on the tabs of the target.

    Enter the sql statements. See the screenshots below...

    I think this will help for you,

    Thank you best regards &,.

    A.Kavya

  • Composite planning form, copy data from one tab to the other?

    This is something I've heard of someone who did but have not actually seen. The requirement is to copy data from an existing entity to a new entity as a means of values seedlings early in the budget cycle. I was thinking about a form of 'source' and a form of 'target' within a composite shape. A calc would make a copy. After copying the Scheduler can manually adjust values, or type values in the accounts that the source had no data.

    If someone did please let me know how you did and what looked like the calc.

    Thank you!

    FA

    Hello

    Yes, this can be done in several ways. The calc script would be similar to what is mentioned above. But in regards to the copy, you can also create a menu on the source form set a rule of company on the menu and start the copy that also offer users the ability to choose several invited outside of the entity if necessary.

    Thank you

  • Insert data from one table to another

    I have two tables SALES_MASTER and SALESMAN_MASTER which is described below.

    SQL > desc SALES_MASTER;
    Name Null? Type
    ----------------------------------------- -------- ----------------------------
    NO VARCHAR2 (6)
    NAME VARCHAR2 (20)
    PIN NUMBER (8)
    CITY VARCHAR2 (20)
    VARCHAR2 (20) STATE



    SQL > desc SALESMAN_MASTER;
    Name Null? Type
    ----------------------------------------- -------- -----------------------
    SALESMANNO VARCHAR2 (6)
    SALESMANNAME VARCHAR2 (20)
    ADDRESS1 VARCHAR2 (30)
    ADDRESS2 VARCHAR2 (30)
    CITY VARCHAR2 (20)
    PIN NUMBER (8)
    VARCHAR2 (20) STATE
    SALAMT NUMBER (8.2)
    TGTTOGET NUMBER (6.2)
    YTDSALES NUMBER (6.2)
    REMARKS VARCHAR2 (60)

    I want to insert data from SALESMAN_MASTER (2nnd table) to SALES_MASTER(first table). I want to insert only two columns NO, NAME and PIN code. All of these columns are compatible. I tried this command. But it's not working. Help me.

    SQL > insert into SALES_MASTER
    2 (NO., NAME, PIN CODE)
    3 values
    4. Select SALESMANNO, SALESMANNAME, PIN code
    5 SALESMAN_MASTER;
    Select SALESMANNO, SALESMANNAME, PIN code
    *
    ERROR on line 4:
    ORA-00936: lack of expression
    INSERT INTO SALES_MASTER
       ( NO
       , Name
       , PINCODE )
       SELECT SALESMANNO
            , SALESMANNAME
            , PINCODE
         FROM SALESMAN_MASTER;
    

    Kind regards
    Christian Balz

  • Creation of interface that won't use c $ table while getting data from one table to the other.

    Hello

    That was the question asked in an interview.

    Given the scenario has been that I have two servers say s1 and s2, each containing a database with tables source1 and source2 respectively...

    And I have a target table on server 2 with name target 1.

    Cent were as

    1 can data ufetch target1 source1 without creating table C $? If Yes, how can it be achieved.

    2. how temporary will be created all to get ata in source1 and source2 to target1? Explain the same thing?

    Also I would like to know what is the actual use of the c arrays $? Why they are used.

    I would be very grateful for answers.

    Thank you and best regards,

    Mahesh

    Hello

    Look at these links.

    http://odiexperts.com/why-creating-a-separate-schema-for-temporary-tables-in-ODI/

    Also this

    http://odiexperts.com/direct-load-by-interface-with-no-work-tables/

    Concerning

  • Copy selected messages sent from one system to the other

    When I was on vacation, I used a laptop computer to send and receive e-mail. I said TBird not to delete the mail incoming server, so I was able to get it later on my home desktop system.
    But the mail that I sent is only on the laptop; is it possible to copy certain messages sent from the laptop to the folder "sent" from the desktop?

    you have got it.

  • Pass data from one page to the other

    Hi all, I'm doing an upload of CSV file in a database application, and I wonder if it is possible to transmit the data from the file field to another php file that performs the command insert the data from the csv file into the database? Thank you!!

    Wanqi

    Hi, good news! I managed to solve the problem. It turns out that the bug was the connotation of the variable. Thanks a lot for your help!

  • moving data from one table to another

    Hello

    I have a table called ABC. It is to have about 15 columns with a column having the LONG data type.

    ABC
    {
    Column1 varchar2 (10)
    Column2 number (7)
    Column3 LONG
    Column4 char (1)
    column 5...
    ...
    }

    What I want is to move all the records in another table called ABC_BACKUP where column4 of the ABC table value is * "X." Which means cutting all the records from ABC where column4 is * 'X' * and paste it into ABC_BACKUP. Can anyone suggest what is the best way to move this data? The volume of data can be maximum 400 000 records.

    I have oracle 10g.

    Thanks in advance.

    Ketan,

    Miss to ' * ' in the tips, you should be ok now. Keep us informed of your progress and I hope that helps you.

    INSERT / * + APPEND * / into ABC_BACKUP SELECT * FROM ABC WHERE ABC WHERE COLUMN = 'X '. *

    INSERT / * + PARALLEL (ABC_BACKUP, 2) * / select into ABC_BACKUP / * + PARALLEL (ABC, 2) * / * ABC WHERE column = 'X '. *
    Concerning

  • When I copy and paste data from a web site in Excel, it comes in a great folly - that is to say it is not "bounded" - and I do not see how to delimit the data in cells. In Solution Explorer, the data are defined. Clues? John

    See above

    Two options:

    1. When you paste the data in Excel using Paste special, then choose text or Unicode text.
    2. Use the extension of Dafizilla Table2Clipboard to copy data - https://addons.mozilla.org/firefox/addon/dafizilla-table2clipboard
  • Reading data from a Table by using the loop

    I have a dynamic array within the PDF form. I want to loop through the rows in the table and read the contents of the cell. I successfully get the number of rows in the table. But impossible to read the values in the cells. I put control TextField (txtName) editable in every cell and trying to read its value.

    screenshot is below.

    table_loop_error.jpg

    I use the code is:

     form1.Page1.Subform1.btnReadTable::click - (JavaScript, client)
    
    var rowCount = MyTable._Row1.count;
    app.alert("Row Count: " + rowCount);
    var i = 0;
    
    for(i=0 ; i<rowCount ; i++)
    {
        //app.alert(MyTable.Row1[i].txtName.rawValue); // NOT WORKING
        
        app.alert(MyTable.Row1.txtName.rawValue); // WORKING, But just giving the value for the first row.
    }
    

    Please tell me how can I get the value of each name in all ranks by making a loop.

    Thanks in advance.

    -

    Afonso

    Hi, Afonso,

    You must resolve the node for the particular case of i. The syntax is:

    xfa.resolveNode("MyTable.Row1[" + i  + "].txtName").rawValue;
    

    Here there is an example that will show you the loop in action:

    http://www.assuredynamics.com/index.php/category/portfolio/two-way-binding-in-tables/

    Hope that helps,

    Niall

    Ensure the dynamics

Maybe you are looking for