Select the statement to insert into a table by using a loop

Hello
create table uploadtab(
itema varchar2(3),
xtype varchar2(1),
salesa number,
margina number,
salesb number,
marginb number,
salesc number,
marginc number);

insert into uploadtab
  (itema, xtype, salesa, margina, salesb, marginb, salesc, marginc)
values
  ('abc', 'a', 100, .40, 300, .10, 450, .25);

create table testinsert(itema varchar2(3),
xtype varchar2(1),
sales number,
margin number);
what I want to do is create 3 records based on this one in a loop
so here's my desired for testinsert output
abc  a 100  .40
abc  a 300  .10
abc  a 450  .25
I don't want to use 3 insert tables if possible

any help would be greatly appreciated
Thanks in advance

Published by: DM on July 7, 2010 14:22

This gives a shot:

INSERT INTO testinsert
( itema
, xtype
, sales
, margin
)
SELECT  itema
,       xtype
,       DECODE
        ( RN
        , 1,salesa
        , 2,salesb
        , 3,salesc
        )
,       DECODE
        ( RN
        , 1,margina
        , 2,marginb
        , 3,marginc
        )
FROM            uploadtab
CROSS JOIN      (
                        SELECT ROWNUM RN
                        FROM   dual
                        CONNECT BY LEVEL <= 3
                )
;

Tags: Database

Similar Questions

  • insert into multiple tables by using select in stored procedure

    I use oracle 10g R2. I want to insert records in two tables by using a select query (insert into tablename select...) in a stored procedure. How can I insert records into a table two at the same time using a select statement?

    My second question is also related to the first... can I use mechanism clustering here because the two tables are based on the same structure and data? One contains data from 24 hours and there are records of 10 days.

    Thank you.

    If you find the answer of Aman as useful/correct, please mark her answer respectively and close the debate. I think his answer is correct as always / a lot of time.

    Concerning
    Girish Sharma

  • How to identify the name of the procedure that inserts into a table in the TRIGGER

    Hello
    There are several procedures in my DB that perform the INSERT operation on a main table - MYTAB say.

    I created a trigger on the table MYTAB, to follow closely, what procedure is insert data in there. Is there a function that captures the name of this object?

    For example: PROC_A, PROC_B, PROC_C insert into the table MYTAB. And my query is - the trigger on table MYTAB should document these object names where the lines fit.
    Please let me know your suggestions...

    SQL> create table t (no integer, dt timestamp, who_inserted varchar2(4000));
    
    Table created.
    
    SQL> create or replace procedure p
      2  as
      3  begin
      4    insert into t (no, dt) values (1, systimestamp);
      5  end;
      6  /
    
    Procedure created.
    
    SQL> show err
    No errors.
    
    SQL> create or replace trigger t_trig before insert on t for each row
      2  begin
      3     :new.who_inserted := dbms_utility.format_call_stack;
      4  end;
      5  /
    
    Trigger created.
    
    SQL> show err
    No errors.
    
    SQL> begin
      2    p;
      3  end;
      4  /
    
    PL/SQL procedure successfully completed.
    
    SQL> select * from t;
    
            NO DT                             WHO_INSERTED
    ---------- ------------------------------ ----------------------------------------
             1 31-OCT-14 04.58.53.668465 AM   ----- PL/SQL Call Stack -----
                                                object      line  object
                                                handle    number  name
                                              3a7ddbea0         2  KARTHICK.T_TRIG
                                              3ac979f30         4  procedure KARTHICK.P
                                              3a822deb8         2  anonymous block
    
  • Select the last row inserted into the table

    Hello
    I have an app that inserts a line in two columns (stamp that contains the time of creation of the line and a float) each seconde.1. I have another app that bed that reads lateset inserted each seconde.1. I had this on SQL Server where I just chose the DateTime max line and had surgery no clustered index on the datetime column. But since I'm moving this course at Oracle I wanted to know if there is a better way to do it. The table will keep up to 30 days of data is about 26 million lines. So I need this facility where querying for last place among the 26 million lines continue with timer deuxieme.1 of my application.

    Here's my insert:
    Insert in testlog (hz, ctime) values (: hz_value, localtimestamp (2))

    Here is my selection:
    Select hz, ctime testlog where ctime = (select max (ctime) of testlog)

    Thank you

    I would use:

    Select hz, ctime of (select hz, ctime, dense_rank() over (order by desc ctime) rnk of testlog) where rnk = 1;

    For example:

    SQL> explain plan for
      2  select * from emp where empno = (select max(empno) from emp)
      3  /
    
    Explained.
    
    SQL> @?\rdbms\admin\utlxpls
    
    PLAN_TABLE_OUTPUT
    ------------------------------------------------------------------------------------------------------------------------------------
    Plan hash value: 1674692883
    
    ---------------------------------------------------------------------------------------
    | Id  | Operation                    | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
    ---------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT             |        |     1 |    37 |     2   (0)| 00:00:01 |
    |   1 |  TABLE ACCESS BY INDEX ROWID | EMP    |     1 |    37 |     1   (0)| 00:00:01 |
    |*  2 |   INDEX UNIQUE SCAN          | PK_EMP |     1 |       |     0   (0)| 00:00:01 |
    |   3 |    SORT AGGREGATE            |        |     1 |     4 |            |          |
    |   4 |     INDEX FULL SCAN (MIN/MAX)| PK_EMP |    14 |    56 |     1   (0)| 00:00:01 |
    ---------------------------------------------------------------------------------------
    
    PLAN_TABLE_OUTPUT
    ------------------------------------------------------------------------------------------------------------------------------------
    
    Predicate Information (identified by operation id):
    ---------------------------------------------------
    
       2 - access("EMPNO"= (SELECT MAX("EMPNO") FROM "EMP" "EMP"))
    
    16 rows selected.
    
    SQL> explain plan for
      2  select * from (select e.*,dense_rank() over(order by empno desc) rn from emp e) where rn = 1
      3  /
    
    Explained.
    
    SQL> @?\rdbms\admin\utlxpls
    
    PLAN_TABLE_OUTPUT
    ------------------------------------------------------------------------------------------------------------------------------------
    Plan hash value: 2150023773
    
    ----------------------------------------------------------------------------------------
    | Id  | Operation                     | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
    ----------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT              |        |    14 |  1400 |     2   (0)| 00:00:01 |
    |*  1 |  VIEW                         |        |    14 |  1400 |     2   (0)| 00:00:01 |
    |*  2 |   WINDOW NOSORT STOPKEY       |        |    14 |   518 |     2   (0)| 00:00:01 |
    |   3 |    TABLE ACCESS BY INDEX ROWID| EMP    |    14 |   518 |     2   (0)| 00:00:01 |
    |   4 |     INDEX FULL SCAN DESCENDING| PK_EMP |    14 |       |     1   (0)| 00:00:01 |
    ----------------------------------------------------------------------------------------
    
    PLAN_TABLE_OUTPUT
    ------------------------------------------------------------------------------------------------------------------------------------
    
    Predicate Information (identified by operation id):
    ---------------------------------------------------
    
       1 - filter("RN"=1)
       2 - filter(DENSE_RANK() OVER ( ORDER BY INTERNAL_FUNCTION("EMPNO") DESC )<=1)
    
    17 rows selected.
    
    SQL> 
    

    SY.

  • when_button_pressed, inserting into a table

    I created a button 'OK' and in the when_button_pressed, I put the code to insert into a table (X for example), the values of entries in the form (ie., main block B_MAIN). The B_MAIN block is built manually and refers to the X table above.

    The OK button is in a different block called B_ACTION.

    When I run the form and click on 'OK', nothing happens. Is no error message or another. This could be it prevents insertion of data in the table?

    Thank you
    Chiru

    Published by: Megastar_Chiru on February 17, 2009 16:23

    The B_MAIN is the database block? Check the BLOCK of DATA of DATABASE property, it must be set to NO because you entered the data manually.

    Another thing, when using the pl/sql commit it is not necessary to follow by commit_form, since he initiated commit_form built-in function.

  • Insert into a table using the check boxes

    Hello

    I want to insert into a table emp when I press a button "Insert" area of employee report. the SQL query for the report is

    SELECT HTMLDB_ITEM. CheckBox(1,rowNum) "",
    x.EmpNo,
    x.Ename
    de)
    Select empno htmldb_item.hidden(2,empno),
    htmldb_item. Text(3,Ename) ename from emp) x
    order by 2

    I created the following process:

    declare
    v_test varchar (10);
    Start
    BECAUSE me in 1.HTMLDB_APPLICATION. G_F01. COUNTY
    LOOP
    v_test: = HTMLDB_APPLICATION. G_F02 (HTMLDB_APPLICATION. G_F01 (i));
    dbms_output.put_line (v_test);
    insert into emp(empno,ename,job,mgr,hiredate,sal,comm,deptno)
    values(v_test,'A','B',10,01-May-81,200,1,10)
    WHERE empno = HTMLDB_APPLICATION. G_F02 (HTMLDB_APPLICATION. G_F01 (i));
    END LOOP;
    end;

    But IT is not working properly, infact, it gives me errors like:

    ORA-06550: line 9, column 47:
    PL/SQL: ORA-00933: SQL not correctly completed command
    ORA-06550: line 8, column 1:
    PL/SQL: SQL statement ignored

    7 dbms_output.put_line (v_test);
    8 insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
    9 values(v_test,'A','B',10,01-MAY-81,200,1,10)
    10 WHERE empno = HTMLDB_APPLICATION. G_F02 (HTMLDB_APPLICATION. G_F01 (i));
    11. END OF LOOP;

    Any help?
    Additional info: table Emp is the default table in the database

    This is an example showiing how such a process must be put in place:

    http://Apex.Oracle.com/pls/OTN/f?p=31517:95

    Denes Kubicek
    -------------------------------------------------------------------
    http://deneskubicek.blogspot.com/
    http://www.Opal-consulting.de/training
    http://Apex.Oracle.com/pls/OTN/f?p=31517:1
    -------------------------------------------------------------------

  • How the values to insert into the table with the command insertion

    Dear all
    can someone tell me how the values to insert into the table with the command insert, I want to say I always use command insert behind my forms on what shutter release button press the button of my save, but today I had a form of 6i, where controls (textbox, combo, etc.) are delineated with directly the table with I guess than the Properties Windows , I created 3 columns in tand 3 text on forms fields, now kindly tell me how to do this fields to fill and do not insert command, I mean directly defined with table column



    Please help me its urgent

    Hello

    If the block is based on your database table, just committed the shape, then changes will be applied to the database.

    François

  • Create a button for inserting into a table

    What I can't do, because I've never needed, is to create, in a page (where I have an interactive report), a button is clicked that once performs a simple sql (for example, an insert into a table using the values of page element).

    How can I do?

    Thank you

    Here are some of the steps:

    (1) create the button.

    (2) then right click on the name of the button and select Create Dynamic Action

    (3) give it to DA a name, click Next

    (4) on the 'when' step, event should already be "Click" and the name of the button must be filled

    (5) for the Condition, you would have a condition when you want that the display of the button.  Say, if your page elements have a certain range of values, and then display this button.  No condition means that the button is always displayed.

    (6) for the 'real Action' stage, action, select "run the Code in PL/SQL.

    [There are other ways to do this, but it's simple for me.]

    (7) in the Code PL/SQL block, enter says:

    Begin

    INSERT INTO EMP (EMPNO, ENAME, HIREDATE)

    VALUES (: P6_EMPNO, 'MARK1970',: P6_HIREDATE);

    COMMIT;

    END;   -You'd probably all the variables page element

    (8) here the part I'm not sure 100%.

    For items to submit "Page, name used in YOUR QUERY - in my example page elements, these are P6_EMPNO, P6_HIREDATE (don't note any use of & or: or here.)

    Hmm.  Start by putting nothing in "Page of elements to return.   If that fails, then try the same elements of page P6_EMPNO, it P6_HIREDATE as well.

    [I'm too lazy to go running at present].

    (9) I think that's it.

    Howard

  • Issue when inserting into a table using sequence in Oracle 11 g Rel 2

    Hi all

    I am facing problem when I insert sequence values in my table.
    When inserting, my sequence does not begin with the beginning with the value.
    Example Script

    CREATE SEQUENCE xyz_seq
    START WITH 1
    INCREMENT BY 1
    NOMAXVALUE
    MINVALUE 1
    NOCYCLE
    NOCACHE
    ALL;

    create table abc (a number not null);

    insert into abc (xyz_seq.nextval) values;

    Select *.
    ABC;

    xyz_seq sequence of fall;

    drop table abc;

    Output

    Order of creation.
    Table created.
    1 line of creation.

    ONE
    ----------
    2
    1 selected line.
    Sequence has fallen.
    Deleted table.

    I can't understand why this is inserting value 2, when my sequence should start at 1.


    To overcome that road, I implemented a different logic.
    Example of

    CREATE SEQUENCE xyz_seq
    START WITH 1
    INCREMENT BY 1
    NOMAXVALUE
    MINVALUE 1
    NOCYCLE
    NOCACHE
    ALL;

    create table abc (a number not null);
    declare
    x number (1): = xyz_seq.nextval;
    Start


    insert into abc (x) values.
    end;
    Select *.
    ABC;

    xyz_seq sequence of fall;

    drop table abc;

    Output

    Order of creation.
    Table created.
    PL/SQL procedure successfully completed.

    ONE
    ----------
    1
    1 selected line.
    Sequence has fallen.
    Deleted table.

    However, my question remains why referring to the sequence.nextval in my insert does not return me the beginning with the value.

    Kind regards
    Rishi

    That's what says the doc ;

     If you attempt to insert a sequence value into a table that uses deferred segment creation, the first value that the sequence returns will be skipped.
    

    It ignores the value 1 and returns 2.

  • Insert into a table... with order by

    Oracle 10.2.0.3

    I want to insert into a table, records from another table ordered in a certain way. We want a specific order that we want the data grouped the disk.

    No idea why it does not work?
    create table test_a (
         id number primary key,
         value1 varchar2(20));
    
    insert into test_a (id, value1) values (1,'ccc');
    insert into test_a (id, value1) values (2,'bbb');
    insert into test_a (id, value1) values (3,'aaa');
         
    create table test_b (
         id number primary key,
         value1 varchar2(20));
    
    insert into test_b (id, value1) (select id, value1 from test_a order by value1)
                                                                   *
    ERROR at line 1:
    ORA-00907: missing right parenthesis     
    If I leave aside the order, it works fine...
    SQL> insert into test_b (id, value1) (select id, value1 from test_a);
    
    3 rows created.
    This does not work either:
    create table test_b as (select id, value1 from test_a order by value1)
                                                          *
    ERROR at line 1:
    ORA-00907: missing right parenthesis

    Lose the parenthesis.

    TUBBY_TUBBZ?create table test_b as select id, value1 from test_a order by value1;
    
    Table created.
    
    Elapsed: 00:00:00.02
    

    Or

    TUBBY_TUBBZ?
    insert into test_b select id, value1 from test_a order by value1;
    
    3 rows created.
    
    Elapsed: 00:00:00.01
    TUBBY_TUBBZ?
    
  • CFIF to the boxes being inserted into the database field car_make.

    Hello, I need help on CFIF for the boxes being inserted into the database field car_make.

    At this point, several values is inserted in the car_make field. I would like to only have one value depending on what is checked in the box.

    Thank you

    Here's what I mean. I would like to know if it is is not clear.

    CFIF
    If Honda is checked then Honda value is inserted in the field car_Make

    If if Toyota is checked then Toyota value is inserted in the field car_Make


    If If Subaru is checked then Subaru value is inserted in the field car_Make


    You CAN, of course, but not without being fluent in javascript. Is there a particular reason you want to use checkboxes versus radio buttons?

    From an interface point of view, its generally not a good idea to change the behaviours expected of HTML form elements. If users see an option button, they assume generally that it will allow only 1 choice. If they are presented with a box, they could very easily be expected they could select more than 1.

  • Select the last 10 records in a table

    Oracle9i. I am trying to select the last 10 numbers in a table with this command series
    select serial from tab_42 where employeecode='00001'  and rownum<=10 order by serial desc;
    but it's not not selecting the most recent serial number 10. How to do?

    The rownum clause is applied before the order by clause, one must use a subquery...

    select * from (select serial from tab_42 where employeecode='00001' order by serial desc) where rownum<=10;
    
  • How to select the last 20 transactions in a table

    Hi guys,.
    I have a table named INOUT with following columns

    EMPLOYEECODE VARCHAR2
    DATE OF THE RESPONDENT


    each day employees now their me 'in '. then entered came and came in this table. If we want to select the last 20 EMPLOYEECODE in this table that make their selves 'in '. How to choose that? just 20 last employees who traded. Help, please

    Hello

    This is called a Query Top - N , you want to choose elements of N (N = 20, in this case) from the top of a sorted list.
    Here's a way to do it:

    WITH     got_r_num     AS
    (
         SELECT       emloyeecode
         ,       MAX (intime)     AS latest_intime     -- If wanted
         ,       RANK () OVER (ORDER BY  MAX (intime)  DESC)
                        AS r_num
         FROM       inout
         GROUP BY  employeecode
    )
    SELECT     employeecode
    ,     latest_intime          -- If wanted
    ,     r_num               -- If wanted
    FROM     got_r_num
    WHERE     r_num     <= 20
    ;
    

    That actaully could take more than 20 people: If there be a tie, it includes any person who is entitled to be in the top 20. If you want exactly 20 people, then add the columns of tiebreaker to analytical or use ROW_NUMBER instead of RANK ORDER BY clause.

  • How to access the NextVal when inserting into the Table

    All,

    I create a type of page process: the anonymous PL/SQL block

    Source:

    Start
    insert into
    c00_reimbursement_checks
    Select
    C00_REIMBURSEMENT_CHECKS_SEQ. NEXTVAL,
    '1',
    PTP.regular_payment_date,
    DC. Paid,
    NULL,
    Pap1.person_id,
    NULL,
    Sum (DC.amount_100_pct) + Sum (DC.paid_at_80) + Sum (DC.paid_at_50),
    "Dental reimbursement plan."
    NULL,
    DC. Reference,
    NULL,
    NULL,
    DCB.batch_date,
    DC.claim_id
    Of
    hrapps.dental_claims dc,
    Apps.per_all_people_f pap,
    Apps.per_all_people_f pap1,
    hrapps.dental_claim_batch dcb,
    hrapps.dental_plans dp,
    TPP apps.per_time_periods
    where dc.plan_id =: P4_PLAN_ID
    and dc.payee <>1069
    and dc.claim_status_id = 3
    and dcb.batch_id = dc.batch_id
    and dcb.batch_date =: P4_BATCH_DATE
    and pap.person_id = dc.patient_person_id
    and dc.date_of_service between pap.effective_start_date and pap.effective_end_date
    and pap1.person_id = dc.person_id
    and dc.date_of_service between pap1.effective_start_date and pap1.effective_end_date
    and dp.plan_id = dc.plan_id
    and ptp.payroll_id = decode(dc.plan_id,1,61,67)
    and dcb.batch_date between (ptp.start_date) and (ptp.end_date)
    Group of null, '1', ptp.regular_payment_date, dc.payee, null, pap1.person_id, null, 'Dental Plan', null, dc.reference, null, null, dcb.batch_date, dc.claim_id of refund
    order of null, '1', ptp.regular_payment_date, dc.payee, null, pap1.person_id, null, 'Dental Plan', null, dc.reference, null, null, dcb.batch_date, dc.claim_id of refund;
    commit;

    End;


    I get this error:

    1 error has occurred
    ORA-06550: line 6, column 60: PL/SQL: ORA-02287: sequence number unauthorized here ORA-06550: line 3, column 1: PL/SQL: statement ignored


    How can I get access to and use the sequence of pk for my table.

    The answer surely is a trigger?

    create or replace trigger "BI_"
      before insert on 
    for each row begin if :NEW.
    is null then select .nextval into :NEW.
    from dual; end if; end;

    You need to change the SQL code to specify the columns that you insert in, but it avoids all the other faff.

    John.

  • Use the trigger and seq to insert into 2 tables

    I created a sequence.

    I have 2 tables

    I want a trigger that when I insert in the 1 table inserts the sequence number nextval, that is no problem, but also is there a way so that once that happens it also inserts this same value of sequence in another table.

    So the end result is a new record in the table has the seq value and a new record in table b with the same value of seq (and the rest of the fields blank)

    Thanks for any help

    Could do:

    SQL > create the sequence myseq;

    Order of creation.

    SQL > drop table t2 is serving;

    Deleted table.

    SQL > drop table t1 is serving;

    Deleted table.

    SQL > create table t1 (collar number);

    Table created.

    SQL > create table t2 (collar number);

    Table created.

    SQL > create or replace trigger mytrig

    2 before the Insert on t1

    3 for each line

    4 start

    case 5: new.col is null

    6. can

    7: new.col: = myseq.nextval;

    8 insert into t2 (col)

    9. Select myseq.currval

    10 double;

    11 end if;

    12 end;

    13.

    Trigger created.

    SQL > insert into values (null), t1 (col);

    1 line of creation.

    SQL > select * from t1;

    COL

    ----------

    1

    1 selected line.

    SQL > select * from t2;

    COL

    ----------

    1

    1 selected line.

  • Maybe you are looking for

    • 4512 envy: envy 4512 cartirage pigmented or tinted ink

      I would like to know if the ink is pigment based or pigmented?  Thank U

    • iPhone 6 - Windows XP - question of iTunes version 12.1.2.27

      I run Windows XP.  I'm on an older machine.  My computer works fine. I have iTunes version 12.1.2.27. iTunes cannot be updated any further on a PC running Windows XP. I just went from an iPhone 4 on the iPhone 6. iTunes version 12.1.2.27 will not wor

    • Find serial number

      I am running XP from Microsoft Vista Business OEMAct passage. HP desktop PC is used in a mode of production / offline. In order to install a new version on the software my seller is asking the XP serial number. Usually it is done by RUN > CMD > FLIGH

    • BlackBerry smartphones turn off screen top banner front new email notification

      Is it possible to disable the small yellow new email notification on the upper region of the front screen? I don't want to stop transmition network, BIS interrupt transfer or set up the firewall as I hear it coming messages. Is it possible to disable

    • Product key Windows 7 blocked after replacing hard drive.

      Hi all, I need your valuable help please. I surfed on the forums, but have not found an answer. I installed a real (bought in a London retail store) Windows 7 ultimate on my Sony labtop. All good everything worked. Then my phone fell and I had to rep