Inserting comma seperated values into table

Hi,
There is a variable like this,
var= 'C0001,C0002,C0003';
I want c0001 c0002 and c0003 to get inserted into some temp table as seperate rows, how do I do that.
Thnx again ur your response.

Hi,
There is a variable like this,
var= 'C0001,C0002,C0003';
I want c0001 c0002 and c0003 to get inserted into some temp table as seperate rows, how do I do that.
Thnx again ur your response.

Similar Messages

  • Inserting comma seperated values

    I created a stored procedure that creates an order_id. Using this id, the products and their pricetag are inserted. Hereafter the garnishes are inserted for each product.
    Creating an order id and inserting the products and their pricetag are working. The problem is at the inserting of garnishes. 
    The data is received by parameters, and I have also created a user defined type: where garnishId holds the comma seperated ids.
    CREATE TYPE [dbo].[ppg] AS TABLE(
    [ProductId] [int] NULL,
    [PriceId] [int] NULL,
    [garnishId] [VARCHAR](MAX) NULL
    this is the stored procedure:
    ALTER PROCEDURE [dbo].[sp_create_order]
    (@userId uniqueidentifier, @productPriceGarnishes dbo.ppg READONLY, @comment varchar(250))
    AS
    BEGIN
    DECLARE @orderId int
    DECLARE @orderDetailId int
    INSERT INTO orders (user_id, confirmed,comment) values(@userId,0,@comment);
    --Select last inserted PK
    SELECT @orderId=SCOPE_IDENTITY()
    -- insert products and price tag using @orderId
    INSERT INTO order_detail (order_id, product_id,price_id)
    SELECT @orderId, p.ProductId,
    p.PriceId
    FROM @productPriceGarnishes p
    SELECT @orderDetailId=SCOPE_IDENTITY()
    -- insert garnishes using the orderDetailId
    INSERT INTO order_detail_garnish (order_detail_id,garnish_id)
    SELECT @orderDetailId, (SELECT * FROM SplitDelimiterString(p.garnishId))
    FROM @productPriceGarnishes p
    END
    RETURN @orderId
    I found a function that splits the string by a delimiter in this website:
    http://www.codeproject.com/Tips/586395/Split-comma-separated-IDs-to-ge
    Where you can retrieve the ids by this query:
    (SELECT * FROM SplitDelimiterString(p.garnishId)
    The problem is that I don't know how to add these ids with their corresponding orderDetailId, after each product is added.

    Unfortunately it appears you assume too much.  Presumably the reason you chose to define @productPriceGarnishes as a table is to support the creation of multiple rows. And you use these rows to insert into the order_detail table which has an identity
    column as the primary key (again, presumably).  So the question then becomes how do you get the identity values for ALL of the inserted rows?  You can't do that with SCOPE_IDENTITY - your code currently is designed with the assumption that there
    is only one row to be inserted.  To work around that limitation you need to use the output clause (examples can be found by searching). 
    Next comes another problem.  How do you know which identity value is assigned to which row of your table variable during the insert?  You need some sort of natural key to associate the inserted rows with the rows in the table variable.  Do
    you have one?  Before you think you do, you need to think about what restrictions are placed on the content of the table variable (if any).  Is it possible to have multiple rows with the same values for ProductId and PriceId?  Do not assume
    - you need to be certain that this is or is not a possibility.
    Assuming that the rows are unique (which simplifies things greatly), you associate the inserted order_detail rows to the table variable via something like:
    select ... from @ins_detail as ins inner join @productPriceGarnishes as ppg
    on ins.ProductId = ppg.ProductId and ins.PriceId = ppg.PriceId
    Note that @ins_detail is a table variable that you would declare and populate via the output clause. It will contain the identity values for each row that you inserted.  With that, you can then generate the rows that you need to insert into the garnish
    table by applying the results of your splitter function.  Which is a step that I'll skip for now since you have much reading and work to do. 
    Now some last comments.  I am suspicious of a column named PriceId.  That is not a good sign - price is an attribute of something and not the basis of a relationship between tables.  The use of identity columns (especially as primary keys)
    can be a problem - and this is one good example.  Based on what I've just written and its assumptions, the natural key for order_detail is (order_id, product_id, price_id) - why do you need an identity column?  Again, searching will find past
    discussions about identity columns and their usage.

  • Inserting the column values into table from view through procedure!!!

    below is my example code
    Create or replace procedure test_proc
    is
    cursor test_cur is
    select a.col1 , b.col2, c.col3, d.col4, e.col5
    from tableA a, tableB b, tableC c, tableD d, tableE e;
    test_cur_rec test_cur%rowtype;
    Begin
    insert into test_stg ( col1, col2, col3, col4, col5, col6, col7)
    Values ( test_cur_rec. col1,
    test_cur_rec .col2,
    test_cur_rec .col3,
    test_cur_rec .col4,
    test_cur_rec .col5
    -- col6, col7 );
    create view test2 (
    select f.col6, g.col7 from tableF f,tableG g);
    I m trying to insert values into the table test_stg but for col6 and col7 i need to get the data from the view test2.
    In simple word, all i need to do is get the 2 columns data from view and insert into a table through procedure. The above code is the example which looks very much like my actual code.
    How do i accomplish this task ??

    well there is joining condition for a, b,c ,d, e tables, i have not mentioned here. The where condition has nothing to do with the other two columns of the view.
    There is no relation ship between the these tables the view. The col 6 and 7 i m inserting from view is sysdate timestamp and the job_id number.
    so its pretty straight forward insertion into the table from view columns. These two columns has just one row with id number and timestamp. so i need to insert these data into the the table when i run a procedure.
    Thank you so much!!!

  • How to insert comma delimited values into a table

    I have a variable holding values like,
    'A,Ba,Ccc' OR
    '1,2,3,4'
    I want to know how to convert and insert these values in oracle table as seperate rows like,
    A
    Ba
    Ccc[ OR ]
    1
    2
    3
    4
    Thanks,
    Previn

    Answered (I guess) in this dbms_utility.comma_to_table.
    Cheers, the ever helpful APC

  • Have a column which is having comma seperated values.. and i am trying to s

    Have a column which is having comma seperated values.. and i am trying to seperate it.
    I am trying to create a stored procedure ..Version 11g xe. .and apex version 4..
    Here is the part of code..I cant make a procedure. .cause my statement consist with clause..
    create or replace procedure ins_adv_invoice
    (mmagazine_no number,madvtno number,missueno number,msection varchar2,mpagenumber varchar2,msalesman_code varchar2)
    is
    cursor cx is with testa as
    (select pagenumber name from advertiser)
    select regexp_substr(pagenumber, '[^,]+', 1, rownum) result
    from advertiser
    connect by level <= length(regexp_replace(pagenumber, '[^,]+')) + 1
    where advt_no = madvtno;
    begin
    for xxx in cx
    loop
    insert into adv_invoice (magazine_no,advtno,adissue,section,page_number,salesman_code)
    values (mmagazine_no,madvtno,missueno,msection,xxx.result,msalesman_code);
    end loop;
    end;
    But when i run this .. it doesnt create procedure. .I get he following error..
    LINE/COL ERROR
    5/14 PL/SQL: SQL Statement ignored
    9/69 PL/SQL: ORA-00933: SQL command not properly ended
    SQL>
    Still not able to figure it out..Can any one help..
    Thanks

    Change the query to swap the last two lines:
    cursor cx is with testa as
    (select pagenumber name from advertiser)
    select regexp_substr(pagenumber, '[^,]+', 1, rownum) result
    from advertiser
    where advt_no = madvtno
    connect by level <= length(regexp_replace(pagenumber, '[^,]+')) + 1;
    ...

  • How to insert a default value into MS server in java - help please

    hi
    suppose if i have a table and one of the column has a default value when the table was created. how can i insert the default value into this column? assuming that the column is the second column and i can't specify the column name when inserting. thanks.

    thanks for ur response, so if i have insert statement as follow
    insert into someTable values (1,'val1', 'val2', 'val3', 'val4')
    and column 3 has default value = DEFAULT
    then, to insert default value, my insert statement will look like this
    insert into someTable values (1,'val1', 'val3', 'val4')
    but the number of values will not match the number of columns.

  • How to insert an upper value into a column?

    Hi!,
    I need to insert an upper value into a column.
    The data is on a flat file and it could be lowercase or uppercase?
    Do I have to create a trigger to solve this problem?
    How could the trigger be?
    Thanks,
    Alex

    If you are using SQL*Loader to load the data from flat file into the db table, then you can achieve inserting the UPPER Case of column value in your sqlloader ctl file itself as in below example
    LOAD DATA
    INFILE *
    APPEND INTO TABLE XXX
    ( field1 position(1:7) char "UPPER(:field1)",
    field2 position(8:15) char "UPPER(:field2)"
    BEGINDATA
    Phil Locke
    Jason Durbin
    So in this case the Names Phil Locke and Jason Durbin will be inserted as PHIL LOCKE and JASON DURBIN into the target table.
    Regards,
    Murali Mohan

  • How CallableStatement in JSP use setDate() method to insert the date value into DB?

    Dear all,
    I met a strange error message when i insert a date value into DB via JSP call PL/SQL procedures.
    The error seems caused by the setDate(index, date) method with CallableStatement.
    The message is: Can not find the setDate(int, java.util.Date) method in the CallableStatement interfaces.
    Any ideas?
    Thanks advanced.

    Thank you!:)
    I solved it using this:
    String name="david";
                stmt = con1.createStatement();
                String prikaz1 = "INSERT INTO table (id,age,surname,name) IN 'C:\\Users\\David\\Desktop\\db.mdb' SELECT id,age,surname,' " + name + " ' FROM table2";
                stmt.executeUpdate(prikaz1);

  • How to store Comma Seperated Value in a File with Jsp

    Hai friends,
    I need to get all the filed values like Empname,salary ,location..........
    and at the end of jsp page i have to put a browse button when i choose a file using theis browse button the total content in the form shoud store in that file as comma seperated values.

    1. Create JSP
    2. When you click the button call the event that saves the data in CSV format
    Your question is so broad, I don't have exact answer for you.
    What have you done so far?
    The man with blues.

  • Insert result of sql into table

    Hi all, how do I insert result of sql into table marco_ttmp ?
    I tried this, but it doesn't work, it returns "ORA-00928: missing SELECT keyword"
    insert into marco_ttmp (var_id,arcdate,contragentid,lpnd,lusd,lkk)
    with
    rrk as (
    select '01112010' as mydate FROM dual d
    union all
    select '01122010' as mydate FROM dual d
    rru as (
    select '33' contragentid from dual d
    union all 
    select '56' contragentid from dual d
    b as (
    select '01112010' as arcdate, '33' as contragentid, 'tt' as t from dual
    union all
    select '01122010' as arcdate, '56' as contragentid, 'un' as t from dual
    union all
    select '01122010' as arcdate, '33' as contragentid, 'kp' as t from dual
    union all
    select '01112010' as arcdate, '56' as contragentid, 'ur' as t from dual
    d as (
    select '01112010' as arcdate, '33' as contragentid, 'dr' as y from dual
    union all
    select '01122010' as arcdate, '56' as contragentid, 'rh' as y from dual
    union all
    select '01122010' as arcdate, '33' as contragentid, 'tr' as y from dual
    union all
    select '01112010' as arcdate, '56' as contragentid, 'wn' as y from dual
    v as ( select '555' as kkt from dual ),
    kkt as ( select count(*)+5 kkt from v )
    --insert into marco_ttmp (var_id,arcdate,contragentid,lpnd,lusd,lkk)
    select substr(rrk.mydate,4,2) || '.' || rru.contragentid var_id,
    rrk.mydate as arcdate,
    rru.contragentid as contragentid,
    count(b.arcdate) lpnd,count(d.arcdate) lusd,
    kkt.kkt lkk
    from kkt cross join rrk cross join rru
    left outer join b on b.arcdate = rrk.mydate and b.contragentid = rru.contragentid
    left outer join d on d.arcdate = rrk.mydate and d.contragentid = rru.contragentid
    group by rrk.mydate, rru.contragentid, kkt.kkt

    Moved "insert into" to the beginning of statement, everything works.
    insert into marco_ttmp (var_id,arcdate,contragentid,lpnd,lusd,lkk)
    with
    rrk as (
    select '01112010' as mydate FROM dual d
    union all
    select '01122010' as mydate FROM dual d
    rru as (
    select '33' contragentid from dual d
    union all 
    select '56' contragentid from dual d
    b as (
    select '01112010' as arcdate, '33' as contragentid, 'tt' as t from dual
    union all
    select '01122010' as arcdate, '56' as contragentid, 'un' as t from dual
    union all
    select '01122010' as arcdate, '33' as contragentid, 'kp' as t from dual
    union all
    select '01112010' as arcdate, '56' as contragentid, 'ur' as t from dual
    d as (
    select '01112010' as arcdate, '33' as contragentid, 'dr' as y from dual
    union all
    select '01122010' as arcdate, '56' as contragentid, 'rh' as y from dual
    union all
    select '01122010' as arcdate, '33' as contragentid, 'tr' as y from dual
    union all
    select '01112010' as arcdate, '56' as contragentid, 'wn' as y from dual
    v as ( select '555' as kkt from dual ),
    kkt as ( select count(*)+5 kkt from v )
    select substr(rrk.mydate,4,2) || '.' || rru.contragentid var_id,
    rrk.mydate as arcdate,
    rru.contragentid as contragentid,
    count(b.arcdate) lpnd,count(d.arcdate) lusd,
    kkt.kkt lkk
    from kkt cross join rrk cross join rru
    left outer join b on b.arcdate = rrk.mydate and b.contragentid = rru.contragentid
    left outer join d on d.arcdate = rrk.mydate and d.contragentid = rru.contragentid
    group by rrk.mydate, rru.contragentid, kkt.kkt

  • How to query the comma seperated values stored in Database

    Hi,
    I have a strange scenario, I am storing the specific data as a comma seperated value in the database. I want to query the DB with the comma seperated value as a Bind variable against the comma seperated value stored in DB.
    For eg : The data stored in DB has
    Row1 - > 1,2,3,4,5,6,7,8
    Row2 - > 4,5,6,7,8,9,10
    When I pas the Bind variable as '4,8' I should get Row1 and Row2 .
    Quick help his highly appreciated.. Thanks in Advance

    Oh, and if you actually wanted the data returned rather than just the row primary keys....
    SQL> ed
    Wrote file afiedt.buf
      1  with t as (select 1 as rw, '1,2,3,4,5,6,7,8' as txt from dual union all
      2             select 2, '4,5,6,7,8,9,10' from dual union all
      3             select 3, '1,6,7,9' from dual)
      4  -- end of test data
      5      ,r as (select '4,8' as req from dual)
      6  -- end of required data
      7      ,split_t as (select rw, regexp_substr(txt, '[^,]+', 1, rn) as val
      8                   from t, (select rownum rn from dual connect by rownum <= (select max(length(regexp_replace(t.txt, '[^,]'))+1) from t))
      9                   where regexp_substr(txt, '[^,]+', 1, rn) is not null
    10                  )
    11      ,split_r as (select regexp_substr(req, '[^,]+', 1, rownum) as val
    12                   from r
    13                   connect by rownum <= length(regexp_replace(req, '[^,]'))+1
    14                  )
    15  --
    16  select distinct t.rw, t.txt
    17  from   split_t st join split_r sr on (st.val = sr.val)
    18                    join t on (t.rw = st.rw)
    19* order by 1
    SQL> /
            RW TXT
             1 1,2,3,4,5,6,7,8
             2 4,5,6,7,8,9,10
    SQL>

  • Insert comma seperator in a currency

    Hello All,
    I need to condense an amount.So i am assigning it to a char value and condensing it.As I am assigning it to a char value,the thousand seperator (i.e ',' ) is removed from the value.I want that back.
    Here is the sample code am using ...
    data : rwbtr type reguh-rwbtr ,
           value type string .
    rwbtr = 1000.
    value = rwbtr .
    condense value no-gaps.
    write / rwbtr .
    write / value  .
    What next i need to do so that i will get a condensed comma seperated value.
    Thanks,
    nsp .

    Use WRITE rwbtr TO value CURRENCY <your currency>.
    Regards,
    John.

  • Inserting the Comma Separated Strings into Table

    Hi Seniors,
    i had two string and i want to insert the records in the Table COMMENT . In this way.
    would u please give some programe to insert the records.
    The Data and the Table
    ( 901,902,903,904 )
    ( 'hai','nice','good & mail is [email protected] ','excellent and the phone 011-235323' )
    comm_id loc_id company_name comments
    1      10 901      Hai
    2      10 902      nice
    3 10      903      good & mail is [email protected]
    4      10 904      excellent and the phone 011-235323
    Thanks
    Seenu

    Hi, Seenu,
    In Oracle 10 (and up) you can easily split a comma-delimited list using REGEXP_SUBSTR.
    INSTR and SUBSTR can do the same thing in any version, but it's more complicated.
    See the general instructions below:
    /*     How to Split a Delimited String
    This shows how to take a single row with a delimited string, such as
         Animal     amoeba,bat,cedusa,dodo
    and transform it into multiple rows:
         Animal     1     amoeba
         Animal     2     bat
         Animal     3     cedusa
         Animal     4     dodo
    PROMPT     ==========  -1. sep_char parameter  ==========
    VARIABLE     sep_char     VARCHAR2 (10)
    EXECUTE     :sep_char := ',';
    SELECT     :sep_char     AS sep_char
    FROM     dual;
    PROMPT     ==========  0. string_test table  ==========
    DROP TABLE     string_test;
    CREATE TABLE     string_test
    (     grp_name     VARCHAR2 (10)
    ,     list_txt     VARCHAR2 (50)
    INSERT INTO string_test (grp_name, list_txt) VALUES ('Animal',     'amoeba,bat,cedusa,dodo');
    INSERT INTO string_test (grp_name, list_txt) VALUES ('Date',     '15-Oct-1582,16-Oct-2008');
    INSERT INTO string_test (grp_name, list_txt) VALUES ('Nothing',     NULL);
    INSERT INTO string_test (grp_name, list_txt) VALUES ('Place',     'New York');
    INSERT INTO string_test (grp_name, list_txt) VALUES ('Skip',     'Hop,,Jump');
    SELECT     *
    FROM     string_test
    ORDER BY     grp_name;
    PROMPT     ==========  Q1.  Oracle 11 Query  ==========
    WITH     cntr     AS          -- Requires Oracle 9
    (     -- Begin sub-query cntr, to generate n (1, 2, 3, ...)
         SELECT     LEVEL     AS n     -- Requires Oracle 9
         FROM     dual
         CONNECT BY     LEVEL     <= 1 +     (
                             SELECT     MAX ( REGEXP_COUNT (list_txt, :sep_char) )     -- Requires Oracle 11
                             FROM     string_test
    )     -- End sub-query cntr, to generate n (1, 2, 3, ...)
    SELECT     grp_name
    ,     n
    ,     REGEXP_SUBSTR     ( list_txt     -- Requires Oracle 10
                   , '[^' || :sep_char || ']'     -- Anything except sep_char ...
                        || '+'               -- ... one or more times
                   , 1
                   , n
                   )     AS item_txt
    FROM     string_test
    JOIN     cntr                                   -- Requires Oracle 9
    ON     n     <= 1 + REGEXP_COUNT (list_txt, :sep_char)     -- Requires Oracle 11
    ORDER BY     grp_name
    ,          n;
    /*     Notes:
         REGEXP_SUBSTR (s, '[^,]+', 1, n)
    returns the n-th item in a comma-delimited list s.
    If there are fewer than n items, it returns NULL.
    One or more consecutive characters other than comma make an item, so
    'Hop,,Jump' has two items, the second one being 'Jump'.
    The sub-query cntr produces a list of integers 1, 2, 3, ..., w
    where w is the worst-case (the largest number of items in any list).
    This actually counts separators, not items, (e.g., it counts both
    commas in 'Hop,,Jump', even though), so the w it produces may be
    larger than is really necessary.  No real harm is done.
    PROMPT     ==========  Q2. Possible Problems Fixed  ==========
    WITH     cntr     AS
    (     -- Begin sub-query cntr, to generate n (1, 2, 3, ...)
         SELECT     LEVEL     AS n
         FROM     dual
         CONNECT BY     LEVEL     <= 1 +     (
                             SELECT     MAX ( REGEXP_COUNT (list_txt, :sep_char) )
                             FROM     string_test
    )     -- End sub-query cntr, to generate n (1, 2, 3, ...)
    SELECT     grp_name
    ,     n
    ,     REGEXP_SUBSTR     ( list_txt
                   , '[^' || :sep_char || ']'     -- Anything except sep_char ...
                        || '+'               -- ... one or more times
                   , 1
                   , n
                   )     AS item_txt
    FROM     string_test
    JOIN     cntr          ON n     <= 1 + NVL     ( REGEXP_COUNT (list_txt, :sep_char)     -- Problem (1)
                                  , 0
    WHERE     REGEXP_SUBSTR     ( list_txt     -- Problem (2)
                   , '[^' || :sep_char || ']'     -- Anything except sep_char ...
                        || '+'               -- ... one or more times
                   , 1
                   , n
                   )     IS NOT NULL
    OR     list_txt     IS NULL          -- Problems (1) and (2) together
    ORDER BY     grp_name
    ,          n;
         (Possible) Problems and Fixes
    (1) If list_txt IS NULL, then REGEXP_COUNT (list_txt, :sep_char)
         returns NULL, the join condition fails, and the output
         contains nothing corresponding to the row from string_test.
         If you want a NULL item to appear in the results, use
         NVL to make sure the expression returns 0 instead of NULL.
    (2) If list_txt contains multiple consecutive sep_chars (or if it
         begins or ends with sep_char, then the original query
         will return NULL items.  To suppress these, add a WHERE
         clause to test that the item_txt to be displayed IS NOT NULL.
    PROMPT     ==========  Q3. Oracle 8.1 Query  ===========
    SELECT     grp_name
    ,     n
    ,     SUBSTR     ( list_txt
              , begin_pos
              , end_pos - begin_pos
              )     AS item_txt
    FROM     (     -- Begin sub-query to compute begin_pos and end_pos
         SELECT     grp_name
         ,     n
         ,     list_txt
         ,     INSTR     ( :sep_char || list_txt
                   , :sep_char
                   , 1
                   , n
                   )     AS begin_pos
         ,     INSTR     ( list_txt || :sep_char
                   , :sep_char
                   , 1
                   , n
                   )     AS end_pos
         FROM     string_test
         ,     (     -- Begin sub-query cntr, to generate n (1, 2, 3, ...)
              SELECT     ROWNUM     AS n
              FROM     all_objects
              WHERE     ROWNUM     <= 1 +     (
                             SELECT     MAX     ( LENGTH (list_txt)
                                       - LENGTH (REPLACE (list_txt, :sep_char))
                             FROM     string_test
              )     -- End sub-query cntr, to generate n (1, 2, 3, ...)
              cntr
         WHERE     n     <= 1 +     ( LENGTH (list_txt)
                        - LENGTH (REPLACE (list_txt, :sep_char))
         )     -- End sub-query to compute begin_pos and end_pos
    ORDER BY     grp_name
    ,          n;
    /*     Version-Dependent Features and Work-Arounds
    The code above, Q3, runs in Oracle 8.1.
    The following changes were made to Q1:
    (11) REGEXP_COUNT was introduced in Oracle 11.
         In earlier versions, to find the number of sep_chars in list_txt,
         see how much the LENGTH changes when sep_chars are removed.
    (10) REGEXP_SUBSTR was introduced in Oracle 10.
         In earlier versions, use INSTR to find where the sep_chars are,
         and use SUBSTR to get the sub-strings between them.
         (Using this technique, 'Hop,,Jump' still contains three items,
         but now item 2 IS NULL and item 3 is 'Jump'.)
    (9.a) The WITH-clause was introduced in Oracle 9
         In earlier versions, use in-line views.
    (9.b) "CONNECT BY LEVEL < constant" doesn't work in Oracle 8.
         Use ROWNUM from any sufficiently large table or view instead.
    (9.c) ANSII join notation (JOIN table_name ON ...) was introduced in Oracle 9
         In earlier versions, join condition go in a WHERE-clause.
    */

  • Compare String in a table and insert the common values into a New table

    Hi all,
    Anyone has idea on how to compare a string value in a table.
    I have a Students Table with Student_id and Student_Subject_list columns as below.
    create table Students( Student_id number,
    Student_Subject_list varchar2(2000)
    INSERT INTO Students VALUES (1,'Math,Science,Arts,Music,Computers,Law,Business,Social,Language arts,History');
    INSERT INTO Students VALUES (2,'Math,Law,Business,Social,Language arts,History,Biotechnology,communication');
    INSERT INTO Students VALUES (3,'History,Spanish,French,Langage arts');
    INSERT INTO Students VALUES (4,'History,Maths,Science,Chemistry,English,Reading');
    INSERT INTO Students VALUES (5,'Math,Science,Arts,Music,Computer Programming,Language arts,History');
    INSERT INTO Students VALUES (6,'Finance,Stocks');
    output
    Student_id     Student_Subject_list
    1     Math,Science,Arts,Music,Computers,Law,Business,Social,Language arts,History
    2     Math,Law,Business,Social,Language arts,History,Biotechnology,communication
    3     History,Spanish,French,Langage arts
    4     History,Maths,Science,Chemistry,English,Reading
    5     Math,Science,Arts,Music,Computer Programming,Language arts,History
    6     Finance,Stocks
    I need help or some suggestion in write a query which can compare each row string value of Student_Subject_list columns and insert the
    common subjects into a new table(Matched_Subjects).The second table should have the below colums and data.
    create table Matched_Subjects(Student_id number,
    Matching_studesnt_id Number,
    Matched_Student_Subject varchar2(2000)
    INSERT INTO Matched_Subjects VALUES (1,2,'Math,Law,Business,Social,Language arts,History');
    INSERT INTO Matched_Subjects VALUES (1,3,'History,Langage arts');
    INSERT INTO Matched_Subjects VALUES (1,4,'History,Maths,Science');
    INSERT INTO Matched_Subjects VALUES (1,5,'Math,Science,Arts,Music,Language arts,History');
    INSERT INTO Matched_Subjects VALUES (2,3,'History,Langage arts');
    INSERT INTO Matched_Subjects VALUES (2,4,'History,Maths');
    INSERT INTO Matched_Subjects VALUES (2,5,'Math,Language arts,History');
    INSERT INTO Matched_Subjects VALUES (3,4,'History');
    INSERT INTO Matched_Subjects VALUES (3,5,'Language arts,History');
    INSERT INTO Matched_Subjects VALUES (4,5,'Math,Science');
    output:
    Student_id      Match_Student_id     Matched_Student_Subject
    1     2     Math,Law,Business,Social,Language arts,History
    1     3     History,Langage arts
    1     4     History,Maths,Science
    1     5     Math,Science,Arts,Music,Language arts,History
    2     3     History,Langage arts
    2     4     History,Maths
    2     5     Math,Language arts,History
    3     4     History
    3     5     Language arts,History
    4     5     Math,Science
    any help will be appreciated.
    Thanks.
    Edited by: user7988 on Sep 25, 2011 8:45 AM

    user7988 wrote:
    Is there an alternate approach to this without using xmlagg/xmlelement What Oracle version are you using? In 11.2 you can use LISTAGG:
    insert
      into Matched_Subjects
      with t as (
                 select  student_id,
                         column_value l,
                         regexp_substr(student_subject_list,'[^,]+',1,column_value) subject
                   from  students,
                         table(
                               cast(
                                    multiset(
                                             select  level
                                               from  dual
                                               connect by level <= length(regexp_replace(student_subject_list || ',','[^,]'))
                                    as sys.OdciNumberList
      select  t1.student_id,
              t2.student_id,
              listagg(t1.subject,',') within group(order by t1.l)
        from  t t1,
              t t2
        where t1.student_id < t2.student_id
          and t1.subject = t2.subject
        group by t1.student_id,
                 t2.student_id
    STUDENT_ID MATCHING_STUDESNT_ID MATCHED_STUDENT_SUBJECT
             1                    2 Math,Law,Business,Social,Language arts,History
             1                    3 Language arts,History
             1                    4 Science,History
             1                    5 Math,Science,Arts,Music,Language arts,History
             2                    3 Language arts,History
             2                    4 History
             2                    5 Math,Language arts,History
             3                    4 History
             3                    5 History,Language arts
             4                    5 History,Science
    10 rows selected.
    SQL> Prior to 11.2 you can create your own string aggregation function STRAGG - there are plenty of example on this forum. Or use hierarchical query:
    insert
      into Matched_Subjects
      with t1 as (
                  select  student_id,
                          column_value l,
                          regexp_substr(student_subject_list,'[^,]+',1,column_value) subject
                    from  students,
                          table(
                                cast(
                                     multiset(
                                              select  level
                                                from  dual
                                                connect by level <= length(regexp_replace(student_subject_list || ',','[^,]'))
                                     as sys.OdciNumberList
           t2 as (
                  select  t1.student_id student_id1,
                          t2.student_id student_id2,
                          t1.subject,
                          row_number() over(partition by t1.student_id,t2.student_id order by t1.l) rn
                    from  t1,
                          t1 t2
                    where t1.student_id < t2.student_id
                      and t1.subject = t2.subject
      select  student_id1,
              student_id2,
              ltrim(sys_connect_by_path(subject,','),',') MATCHED_STUDENT_SUBJECT
        from  t2
        where connect_by_isleaf = 1
        start with rn = 1
        connect by student_id1 = prior student_id1
               and student_id2 = prior student_id2
               and rn = prior rn + 1
    STUDENT_ID MATCHING_STUDESNT_ID MATCHED_STUDENT_SUBJECT
             1                    2 Math,Law,Business,Social,Language arts,History
             1                    3 Language arts,History
             1                    4 Science,History
             1                    5 Math,Science,Arts,Music,Language arts,History
             2                    3 Language arts,History
             2                    4 History
             2                    5 Math,Language arts,History
             3                    4 History
             3                    5 History,Language arts
             4                    5 History,Science
    10 rows selected.SY.

  • Sqlloader: how to insert -ve value into table

    hi..
    i had problem during loading. The error is Record 1: Rejected - Error on table FILE01, column AMOUNT.
    ORA-01722: invalid number
    For AMOUNT, the datatype is number(20,2).
    here is my .ctl
    LOAD DATA
    INFILE 'file1bp0103.txt'
    BADFILE 'file01.bad'
    APPEND
    INTO TABLE file01
    acct_no POSITION(01:13),
    amount POSITION(14:28),
    description POSITION(29:32)
    my .dat file
    A500000030401- 32.74PYMT
    A500000320106- 46.95PYMT
    A500000520408- 63.95PYMT
    A500000610301- 12.99PYMT
    A500001720110- 56.21PYMT
    A500001800103- 55.65PYMT
    A500002000109- 27.25PYMT
    A500002000305- 53.35PYMT
    A500002080210- 75.04PYMT
    A500002250106- 103.38PYMT
    A500002500104- 60.69PYMT
    A500002620902- 509.77PYMT
    A500002621010- 398.69PYMT
    what is the problem? is it because of my datatype?
    please help me..
    TQ

    Is there a need to use dynamic sql here?
    at the very least, try using bind variables instead of concatenating values like that.
    EXECUTE IMMEDIATE 'insert into your_table (column_list) values (:val1, :val2, ...)'
    USING l_val1, l_val2, l_val3 ... ;

Maybe you are looking for

  • How to set Oracle 11g as Primary Oracle Home

    Hi All, I've Windows 7 operating system & have Oracle 11g client ( enterprise edition), Oracle 10g XE and Oracle Developer Suite 10g. I want to know how can I set Oracle_11g_home as my primary oracle home. Note: Currently, the home of Oracle Develope

  • JMS destination

    I´m new to JMS and i still don´t understand how can i use a JMS queue to access APIs or webservice. Given an example where i have an API or WebService exposed to enter customer orders in some system, how can i integrate my JMS queue with it? * Do i h

  • ICloud and third party mail collection

    I've been using MobileMe to pick up a different POP account with another server. I'd like to move it to another IMAP service I have but since I moved to the iCloud I can't find the settings for that. It used to be on the web-based MovileMe settings u

  • Wi fi does not work after installing ios7

    when i installed ios7 , my wi fi does not work . plese help my what can  i do ?

  • RMAN A-Z documentation

    Hi all, i am a amateur DBA. I have been doing export and cold backups but have never done a RMAN backup. So before i start , it would have been of big help if somebody had a good documentation of RMAN methodology including the concepts. Please recomm