How do I insert multiple values into different fields in a stored procedure

I am writing a Stored Procedure where I select data from various queries, insert the results into a variable and then I insert the variables into final target table. This works fine when the queries return only one row. However I have some queries that return multiple rows and I am trying to insert them into different fields in the target table. My query is like
SELECT DESCRIPTION, SUM(AMOUNT)
INTO v_description, v_amount
FROM SOURCE_TABLE
GROUP BY DESCRIPTION;
This returns values like
Value A , 100
Value B, 200
Value C, 300
The Target Table has fields for each of the above types e.g.
VALUE_A, VALUE_B, VALUE_C
I am inserting the data from a query like
INSERT INTO TARGET_TABLE (VALUE_A, VALUE_B, VALUE_C)
VALUES (...)
How do I split out the values returned by the first query to insert into the Insert Statement? Or do I need to split the data in the statement that inserts into the variables?
Thanks
GB

"Some of the amounts returned are negative so the MAX in the select statement returns 0 instead of the negative value. If I use MIN instead of MAX it returns the correct negative value. However I might not know when the amount is going to be positive or negative. Do you have any suggestions on how I can resolve this?"
Perhaps something like this could be done in combination with the pivot queries above, although it seems cumbersome.
SQL> with data as (
  2        select  0 a, 0 b,  0 c from dual   -- So column a has values {0, 1, 4},
  3  union select  1 a, 2 b, -3 c from dual   --    column b has values {0, 2, 5},
  4  union select  4 a, 5 b, -6 c from dual ) --    column c has values {0, -3, -6}.
  5  --
  6  select  ( case when max.a > 0 then max.a else min.a end) abs_max_a
  7  ,       ( case when max.b > 0 then max.b else min.b end) abs_max_b
  8  ,       ( case when max.c > 0 then max.c else min.c end) abs_max_c
  9  from    ( select  ( select max(a) from data ) a
10            ,       ( select max(b) from data ) b
11            ,       ( select max(c) from data ) c
12            from      dual ) max
13  ,       ( select  ( select min(a) from data ) a
14            ,       ( select min(b) from data ) b
15            ,       ( select min(c) from data ) c
16            from      dual ) min
17  /
ABS_MAX_A  ABS_MAX_B  ABS_MAX_C
         4          5         -6
SQL>

Similar Messages

  • Inserting multiples rows into a table using function or procedure..

    How do i insert multiples rows into a table using function or procedure?
    Please provide me query..

    Use FORALL bulk insert statement...
    eg:
    procedure generate_test_data as
    type cl_itab is table of integer index by pls_integer;
    v_cl_itab cl_itab;
    type cl_vtab is table of varchar2(25) index by pls_integer;
    v_cl_vtab cl_vtab;
    type cl_dtab is table of date index by pls_integer;
    v_cl_dtab cl_dtab;
    begin
    for i in 1.. 100 loop
              v_cl_itab(i):= dbms_random.value(1,1000);
              v_cl_vtab (i):=dbms_random.string('a',20);
              v_cl_dtab (i):=to_date(trunc(dbms_random.value(2453737, 2454101)),'j');          
         end loop;
         forall i in v_cl_itab.first .. v_cl_itab.last
              execute immediate 'insert into test_order values( :n, :str , :dt ) ' using v_cl_itab(i), v_cl_vtab (i), v_cl_dtab (i);          
         commit;
    end;

  • How do I insert multiple photos into an excel document vs inserting each individually?  Insert, Picture, From FIle...now what?  it only allows me to choose 1 at a time.

    Help - How do I insert multiple photos into an excel document vs inserting each individually?  Insert, Picture, From FIle...now what?  it only allows me to choose 1 at a time.

    https://discussions.apple.com/thread/3383532?tstart=0
    Stefan

  • How to insert a value into a field of binary type?

    I've been using Oracle for a while now but never dealt with BLOB
    or long raw type before. Does anyone here know how to insert a
    record into a field of either blob or long raw type?
    Any suggestions would be very appreicated.
    Thanks.

    pls used the loadfromfile procedure which is in the DBMS_LOB
    package to insert the data from external os file into blob data
    type column.
    Kunjan

  • How do you lookup multiple values in different columns based on variable criteria?

    Essentially, I'd like to be able to do a Vlookup but instead of searching for one value only, search for multiple values in separate columns. A smaller version of my current spreadsheet as an example...
    Attack Type ->
    Fire
    Water
    Grass
    Fire
    1/2x
    2x
    1/2x
    Water
    1/2x
    1/2x
    2x
    Grass
    2x
    1/2x
    1/2x
    Fire/Water
    1/4x
    1x
    1x
    Fire/Grass
    1x
    1x
    1/4x
    Grass/Water
    1x
    1/4x
    1x
    The headers are the attack types and the list of types to the left are the receiving Pokemon. Fire does half damage (1/2x) to fire types, Water does double damage (2x) to fire types, etc. I'd like to be able to search for specific damages for each type. For example, I'd like to find a Typing that recieves half (1/2x) damage from Fire-type attacks but also recieves double (2x) from Grass-type attacks. I do want more than just two search criteria though seeing as the actual table is much, much larger.
    I've tried assigning number values to each damage multiplier and then merging all of them together for a specific typing and doing a VLOOKUP based on checkboxes determining what damage multiplier I want in a few specific types, the rest being filled in to the standard of 1x but the result isn't correct most of the time.

    Hi Mitchell,
    VLOOKUP can be set for accept either an 'exact match' or a 'close match'.  Your 17 digit 'number' is actually a 17 character text string (Numbers can handle numbers to a precision of only 15 places). Provided all 17 digits are present, sorting should be the same as for numerical values—the leftmost character is the most significant.
    As a text string, your 'number' is sorted/evaluated alphabetically. A 'close match' accepts the 'largest value that is less than or equal to the search value'.
    If your search term is 000200000 (a 9 character string), several 'wrong' answers will fit the 'close match' criteria, including all of those listed below:
    000200000 (the 'correct' match)
    0000xxxxx (x may be any of the three acceptable values)
    0001xxxxx (x may be any of the three acceptable values)
    The main problem here is that digits in a number (or characters in a text string) have decreasing significance related to their distance from the beginning of the string/number. You want a search in which each character has the same significance as each of the others when compared to the search key. To do that, you need to compare each character in the search string with the character in the same position in the similar string for each type of Pokemon, then take a count of the matches or a sum of the differences.
    Here's one approach:
    Column A contains labels.
    Column B, the 17 digit search term, created in whatever manner you wish, and the similar 17 digit string for each of the characters.
    Columns C through S contains a formula that detines, using subtraction, the difference between each digit of the search term and the corresponding digit of each character's profile.
    Column C uses SUM() to calculate the total of columns C to S for each row.
    T1 uses =MIN(T) to calculate "least different" profile.
    Column A is a Header column; Row 1 is a Header row.
    Formulas:
    C2, and filled right to S2, then down to the last row of data:
    =ABS(MID($B$1,COLUMN()-2,1)-MID($B2,COLUMN()-2,1))
    T1: =MIN(T)
    T2, and filled down column T: =SUM(C2:S2)
    The conditional formatting rule set for all body cells in column T is shown below the table.
    This may be enough to get you started. Formulas can be tweaked to produce results more closely matching what you're looking for, if necessary.
    Regards,
    Barry

  • How can I insert a value into the Oracle Date column using JDBC?

    Suppose I have a table TEST created by "create table TEST (c1 INT, c2 Date);"
    Now I want to insert some proper value into the table TEST's column c2. I couldn't do that by TRYing using Java.sql.Date, Java.Util.Date, Java.String.
    Could anyone give me some suggestions by providing the sample code?
    Many thanks for your help!
    Wendy

    What about building the java string:
    "Insert into test (c1,d1) values ("+
    Integer.toString(var)+
    ", TO_DATE("
    formatYourDateVariableWithCalendarMethods
    ",'DD-MON-YYYY')"
    and executing it ??

  • Insert multiple choices into mysql field

    I have a form that inserts a record into mysql. it's working
    fine except when i changed the pull-down menu to a list allowing
    multiple selections, it only inserts the last item selected. I know
    it should insert the valuse as comma separated. Here's the php:
    GetSQLValueString($_POST['not_reen_list'], "text"),
    and the HTML:
    <select name="not_reen_list" size="10" multiple="multiple"
    id="not_reen_list">
    <option value="Nothing selected"
    selected="selected">Please select one</option><option
    value="Cost">Cost</option>
    <option value="School could not meet our financial aid
    needs"> School could not meet our financial aid needs
    </option>
    <option value="Prefer another school">Prefer another
    school </option>
    </select>
    Any help is appreciated.

    awsweb wrote:
    > I have a form that inserts a record into mysql. it's
    working fine except when i
    > changed the pull-down menu to a list allowing multiple
    selections, it only
    > inserts the last item selected.
    That's because you're not sending the form data as an array.
    Change this:
    <select name="not_reen_list"
    to this:
    <select name="not_reen_list[]"
    At the top of the PHP script, convert the array to a
    comma-separated
    string by adding this:
    if (isset($_POST['not_reen_list'])) {
    $_POST['not_reen_list'] = implode(',',
    $_POST['not_reen_list']);
    I know it should insert the valuse as comma
    > separated. Here's the php:
    > GetSQLValueString($_POST['not_reen_list'], "text"),
    > and the HTML:
    > <select name="not_reen_list" size="10"
    multiple="multiple" id="not_reen_list">
    > <option value="Nothing selected"
    selected="selected">Please select
    > one</option><option
    value="Cost">Cost</option>
    > <option value="School could not meet our financial
    aid needs"> School
    > could not meet our financial aid needs </option>
    > <option value="Prefer another school">Prefer
    another school </option>
    > </select>
    > Any help is appreciated.
    >
    David Powers, Adobe Community Expert
    Author, "The Essential Guide to Dreamweaver CS3" (friends of
    ED)
    Author, "PHP Solutions" (friends of ED)
    http://foundationphp.com/

  • How to populate the calculated value into screen field.

    I am doing one enhancement in QM.I have added one custom screen to notification transaction ( QM01/QM02/QM03) transaction tab strip control using the enhancement QQMA0001.The Details of the calling and called screens as shown bellow
    The Calling screen: SAPLIQS0
    Screen Number: 7790
    Screen Area :USER0001
    Called Screen: SAPLXQQM
    Screen Number: 0101
    I have developed the Custom Screen in screen 0101 and called in PBO of program SAPLIQS0 7790 screen.
    The Screen in calling perfectly .The Custom screen having different fields like Raw cost, Intermediate cost, Finished cost, SCAR Cost and Sales Order Cost Etc... These fields are out put filed types. No input for these screens.. I have few doubts regarding this
    How to populate the calculated values in Custom screen?
    Where we wrote the code to populate the calculated values in custom screen?
    You have any idea please guide me
    Thanks & Regards,
    Samantula

    As your screen fields should be global variables in SAPLXQQM, you may initialize them by implementing function module EXIT_SAPMIWO0_008 which also belongs to SAPLXQQM (Customer Exit: Transfer Notification Data to User Screen)

  • How can I insert multiple pictures into a "diary" document?

    We started traveling in an RV.  I'm keeping several journals but want to put everything into one document.  I'd like to either insert a slide show at each location I write about, or just insert a number of pictures easily!  the first time I selected about 8 pictures and they just laid over each other.  I couldn't figure out if text in front, around, and they all come in the same size, too big.  I'm looking for an easy fix as I have 22 events to write up and insert pictures in.
    Are pages the best way to go? 

    Hi billdeb411,
    Pages is certainly one way to go for creating documents such as you describe. You might want to look at this page for a better idea of its capabilities -
    Apple - Pages for Mac
    http://www.apple.com/mac/pages/
    Another possibility, particularly if you are trying to create a physical book as your end product, would be to use iPhoto. See its product page here -
    Apple - iPhoto for Mac
    http://www.apple.com/mac/iphoto/
    With it you can create a photo book with your computer and have Apple print the book out for you
    Apple - Print Products for Mac
    http://www.apple.com/mac/print-products/
    Have fun creating your diary!
    Best,
    Brett L

  • How to do Cost Cenetr Translation into Different fields

    Hi Gurus,
    I have a requirement wherein we need to translate Cost Centers and Cost element combinations to some specific values. And these values might chnage over a period of time for eg. CC-CE XYZ-123 goes to Process name ABC in 2011 but in 2012 the smae combination goes to Process DEF. How can I achieve it.
    Important point- We cannot change the old data so in 2012 if someone wnats to run the report for this combination it should show process ABC only and not Process DEF?
    Can anyone please suggest something.
    Regards!

    You mean the user enters comma separated data into a single APEX item (let's say PXX_COLOR) and your report is built on a SQl statement like
    SELECT ....
    FROM MYTABLE
    WHERE ((COLOUR1 = :PXXX_COLOR1)
    AND (COLOUR2 = :PXX_COLOR2)
    AND (COLOUR3 = :PXX_COLOR3)
    AND ...)
    where XX is your APEX page number?
    If so, you need a PL/SQL procedure that splits the PXX_COLOR item at the commas into separate pieces and sets each of the PXX_COLOR fields in turn. I have a standard procedure I use for getting the nth delimited item from a string (get_token) - code can be found here, but it's easy to write your own.
    Using the get_token function you'd have a PL/SQL process like:
    DECLARE
      l_color VARCHAR2(50);
      l_index INTEGER := 1;
    BEGIN
      l_color := get_token(:PXX_COLOR,l_index);
      WHILE (l_color IS NOT NULL)
      LOOP
        apex_util.set_session_state('PXX_COLOR' || TO_CHAR(l_index),l_color);
        l_index := l_index + 1;
        l_color := get_token(:PXX_COLOR,l_index);
      END LOOP;
    END;This sets each of the separate hidden APEX items which should then be available in the SQL WHERE clause when the page reloads.
    Roger

  • Get multiple values into one field

    I want to get following two rows into one record.
    Thank you in advance!
    ID     Name     Category
    109     John     C1
    109     John     D8
    Result:
    ID     Name     Category
    109     John     C1, D8

    hi, i used centinul suggestion to create this is query.
    WITH tablet AS
         (SELECT '109' AS ID, 'John' AS NAME, 'C1' AS CATEGORY
            FROM DUAL
          UNION ALL
          SELECT '109' AS ID, 'John' AS NAME, 'D8' AS CATEGORY
            FROM DUAL)
    SELECT     ID, NAME,
               LTRIM
                  (MAX (SYS_CONNECT_BY_PATH (CATEGORY, ','))KEEP (DENSE_RANK LAST ORDER BY ID),
                  ) AS CATEGORY
          FROM (SELECT ID, NAME, CATEGORY,
                       ROW_NUMBER () OVER (PARTITION BY ID ORDER BY CATEGORY)
                                                                          AS curr,
                         ROW_NUMBER () OVER (PARTITION BY ID ORDER BY CATEGORY)
                       - 1 AS prev
                  FROM tablet)
      GROUP BY ID, NAME
    CONNECT BY prev = PRIOR curr AND ID = PRIOR ID
    START WITH curr = 1;or
    WITH tablet AS
         (SELECT '109' AS ID, 'John' AS NAME, 'C1' AS CATEGORY
            FROM DUAL
          UNION ALL
          SELECT '109' AS ID, 'John' AS NAME, 'D8' AS CATEGORY
            FROM DUAL)
    SELECT     ID, NAME, SUBSTR (SYS_CONNECT_BY_PATH (CATEGORY, ','),
                                 2)CATEGORY
          FROM (SELECT ID, NAME, CATEGORY, COUNT (*) OVER (PARTITION BY ID) cnt,
                       ROW_NUMBER () OVER (PARTITION BY ID ORDER BY CATEGORY) seq
                  FROM tablet)
         WHERE seq = cnt
    START WITH seq = 1
    CONNECT BY PRIOR seq + 1 = seq AND PRIOR ID = ID;Edited by: DeepakDevarapalli on Nov 12, 2009 2:45 PM
    Edited by: DeepakDevarapalli on Nov 12, 2009 2:46 PM

  • How to insert column values into database as rows

    Hi,
    I have 8 columns and some not null columns. Based on not null columns I want to insert into table as rows. The 8 columns may contain values or no value. If the first column contains data, then I have to insert into one row. if the second column contains data I have to insert a row and in second column. respectively...So How can I insert column values into rows. Can I write 8 insert statements. (OR) is it possible to insert data from columns using where clause.
    Please help me out....
    Thanks in Advance

    Lines Table:
    line_id, orcl_bank_account_num, product_type, service_type, lease_type,
    funding_type, cpi, billing_frequency_unit_cd , annual_due_date ,
    pricing_start_date, pricing_end_date, install_date, contract_end_date ,
    prdct_replacement_cost_amt, cradle_replacement_amt, supranet_contract,
    issuance_fee, board_inactive_date, header_id, creation_date, last_modified_date,
    created_by_nam, modified_by_nam, activeinactive_flg, prdct_bill_amt_yr1,
    prdct_bill_amt_yr2, prdct_bill_amt_yr3, prdct_bill_amt_yr4, prdct_bill_amt_yr5,
    prdct_bill_amt_yr6, prdct_bill_amt_yr7, prdct_bill_amt_yr8, activation_fee_yr1,
    activation_fee_yr2, activation_fee_yr3, activation_fee_yr4, activation_fee_yr5,
    activation_fee_yr6, activation_fee_yr7, activation_fee_yr8,
    In this table the columns structure is :
    -- PRDCT_BILL_AMT_YR (1 to 8) NUMBER(14,4)
    -- ACTIVATION_FEE_YR (1 to 8) NUMBER(8,2)
    I have one more table:
    PRDCT_INS_AMT               NUMBER(14,4)
    ACTIVATION_FEE_AMT          NUMBER(14,4)
    I want to insert prdct_bill_amt_yr (1 to 8) columns data into PRDCT_INS_AMT column. similarly activation_fee (1 to 8) columns data.
    But the data should be inserted based product_type, service_type, lease_type columns values. (These 3 columns may contain upto 45 combinations).

  • Insert multiple records into a table(Oracle 9i) from a single PHP statement

    How can I insert multiple records into a table(Oracle 9i) from a single PHP statement?
    From what all I've found, the statement below would work if I were using MySQL:
         insert into scen
         (indx,share,expire,pitch,curve,surface,call)
         values
         (81202, 28, 171, .27, 0, 0, 'C' ),
         (81204, 28, 501, .25, 0, 0, 'C' ),
         (81203, 17, 35, .222, 0, 0, 'C' ),
         (81202, 28, 171, .27, 2, 0, 'C' ),
         (81204, 28, 501, .20, 0, 1, 'C' ),
         (81203, 28, 135, .22, 1, 0, 'C' )
    The amount of records varies into the multiple-dozens. My aim is to utilize the power of Oracle while avoiding the i/o of dozens of single-record inserts.
    Thank you,
    Will

    You could look at the INSERT ALL statement found in the documentation here:
    http://download.oracle.com/docs/cd/B10501_01/server.920/a96540/statements_913a.htm#2133161
    My personal opinion is that you probably won't see any benefit because under the hood I think Oracle will still be doing single row inserts. I could be wrong though.
    The only way to confirm was if you did a test of multiple inserts vs an INSERT ALL, that is if the INSERT ALL met your requirements.
    HTH.

  • To populate values into single field in an internal table

    Hi Friends,
    How we need to populate values into single field in an internal table.
    E.g itab consits of single field ( name)
           i need to assign values to this field name .like
          peter,
          john,
          abrahm,
          daneyal
    Pls tell me i how i need to code for this
    Thanks ,
    Parnith

    Hi,
    Please look at the below code :
    DATA : BEGIN OF itab OCCURS 0 ,
             name(20) TYPE c,
           END OF itab.
    START-OF-SELECTION.
      itab-name = 'Peter'.
      APPEND itab.
      CLEAR itab.
      itab-name = 'John'.
      APPEND itab.
      CLEAR itab.
      itab-name = 'Abrahm'.
      APPEND itab.
      CLEAR itab.
      itab-name = 'Daneyal'.
      APPEND itab.
      CLEAR itab.
      LOOP AT itab.
        WRITE : / itab.
      ENDLOOP.
    Thanks,
    Sriram Ponna.

  • How to insert or update multiple values into a records of diff fields

    Hai All
    I have to insert or update or multiple values into a single records of diff fields from one to another table.
    Table1 has 3 fields
    Barcode bardate bartime
    0011 01-02-10 0815
    0022 01-02-10 0820
    0011 01-02-10 1130
    0022 01-02-10 1145
    0011 01-02-10 1230
    0022 01-02-10 1235
    0011 01-02-10 1645
    0022 01-02-10 1650
    these are the times that comes in at 0815 and goes break at 1130 and comes in at 1230 and goes home at 1645
    from these table i have to insert into another table called table2
    and the fields are barcode, date,timein intrin,introut,tiomout
    And the output want to like this
    barcode timein intrin introut timeout date
    0011 0815 1130 1230 1645 01-02-10
    0022 0820 1145 1235 1650 01-02-10
    If any give some good answer it will be help full..
    Thanks & Regards
    Srikkanth.M

    SQL> with table1 as (
      2  select '0011' Barcode,'01-02-10' bardate,'0815' bartime from dual union
      3  select '0022','01-02-10','0820' from dual union
      4  select '0011','01-02-10','1130' from dual union
      5  select '0022','01-02-10','1145' from dual union
      6  select '0011','01-02-10','1230' from dual union
      7  select '0022','01-02-10','1235' from dual union
      8  select '0011','01-02-10','1645' from dual union
      9  select '0022','01-02-10','1650' from dual
    10  )
    11  select barcode, bardate,
    12         max(decode(rn,1,bartime,null)) timein,
    13         max(decode(rn,2,bartime,null)) intrin,
    14         max(decode(rn,3,bartime,null)) introut,
    15         max(decode(rn,4,bartime,null)) timeout from (
    16                          select barcode, bardate, bartime,
    17                                 row_number() over (partition by barcode, bardate
    18                                                    order by bartime) rn
    19                            from table1)
    20  group by barcode, bardate;
    BARC BARDATE  TIME INTR INTR TIME
    0011 01-02-10 0815 1130 1230 1645
    0022 01-02-10 0820 1145 1235 1650Max
    http://oracleitalia.wordpress.com

Maybe you are looking for