Want to construct insert statement from all_tab_columns

I am using Oracle 10GR2 and a new user for plsql
I want to write a plsql block or SQL in order to insert some dummy data.
I want to use the column list from all_tab_columns and want to construct insert statement
EX: I am trying to construct SQL as below
for i in (select column_name from all_tab_columns where table_name='EMP')
loop
v_sql := insert into emptest valuesi.column_name
could you please let me know any pointer for the same ..?
can I use any other technique for the same .. like collection, nested table etc

Am not clear for this requirement, if possible could you please elaborate.
Also specify the requirement, inputs and expected outputs with your query in code format...
This will help..

Similar Messages

  • Create multiple Insert statements from multiselect list

    I want to thank everyone in advance for the help on this one.
    I have a multiselect list that I would like to use to create multiple Insert statements. I would like to then take those statements and execute them against the database, thus inserting the records. My thoughts are below;
    1.     Customer selects multiple items in the list
    2.     Clicks the submit button
    3.     onclick event walks through the string created by the multiselect list and creates the Insert statements by walking through an array of the values.
    4.     the Insert statements get executed against the database
    5.     page redirects to the proper page after the process is done.
    I actually have already created a javascript function that will use the split command on the string to create an array. I then get the length of the array and walk through a loop creating the sql statements. I am not sure what to do from here. How can I get the sql to execute.
    I am not wed to this approach. If there is a better idea out there please do not hesitate to share it with me. I am relatively new to APEX and have not done much work in pl/sql or javascript. But I learn fast, so don’t hold back.
    Thanks again and please let me know what questions you have for me.
    Derek.

    I setup the demo app on apex.oracle.com:
    http://apex.oracle.com/pls/apex/f?p=26255:3
    Login as demo / demo
    Go to the "products" tab. There is a Test region with a multiselect list. It shows product names (with product id's) from the demo_product_info table.
    When you select multiple things from the list and click apply, it uses the following procedure to simply insert a new record for each into the product table. It increments the product_id by 1000 and prepends "DUPLICATE " to the product_name:
    declare
    cursor c_products is
    select product_name, product_id
    from demo_product_info
    where instr(':' || :P3_X || ':', ':' || product_id || ':') >= 1
    order by 1
    begin
    for r_product in c_products loop
    insert into demo_product_info (product_id, product_name)
    values (r_product.product_id + 1000, 'DUPLICATE ' || r_product.product_name);
    end loop;
    end;
    You can see that is working by using it on the duplicate entries themselves.
    -Richard

  • How to change Bulk Insert statement from MS SQL to Oracle

    Hi All,
    Good day, I would like to bulk insert the content of a file into Oracle db. May I know how to change the below MS SQL syntax to Oracle syntax?
    Statement statement = objConnection.createStatement();
    statement.execute("BULK INSERT [TBL_MERCHANT] FROM '" MERCHANT_FILE_DIR "' WITH ( FIELDTERMINATOR = '~~', ROWTERMINATOR = '##' )");
    Thanks in advance.
    cs.

    Oracle SQL Loader utility allows you to insert data from flat file to database tables.
    Go to SQL Loader links on following url to learn more on this utility
    http://otn.oracle.com/docs/products/oracle9i/doc_library/release2/server.920/a96652/toc.htm
    Chandar

  • How to prepare an INSERT statement from the resultset

    Hi Friends ,
    I have to generate CREATE and INSERT statements for the Table already present in the database .
    I can prepare CREATE statement by getting the MetaData from the database . Is it possible to create INSERT statement for the data already present in the database table if so how.
    Thanks ,
    Rajesh Reddy

    You should be able to do something like SELECT * FROM foo WHERE 1 = 2. No rows will be returned. Call ResultSet#getMetaData() to get the meta data for the column names and types. You should then be able to dynamically create your own INSERT statement.
    - Saish

  • Generate Insert Statement Script to Extract Data from Table in Oracle 7i

    Hi all, I have an old Oracle legacy system that is running for over 15 years.Every now and then we need to extract data from this table@ ORacle 7i to be imported back to Oracle 10G.
    My thoughts are to create a script of Insert statements in oracle 7 and that to be deployed back to Oracle 10G.
    I found this scripts in Google and not sure how exactly this works.Any explanation on thsi scripts , would be greatly appreciated.I find this scripst may help to generate a set of insert statements from that table to the latest table at 10G.
    <pre>
    -- Step 1: Create this procedure:
    create or replace Function ExtractData(v_table_name varchar2) return varchar2 As
    b_found boolean:=false;
    v_tempa varchar2(8000);
    v_tempb varchar2(8000);
    v_tempc varchar2(255);
    begin
    for tab_rec in (select table_name from user_tables where table_name=upper(v_table_name))
    loop
    b_found:=true;
    v_tempa:='select ''insert into '||tab_rec.table_name||' (';
    for col_rec in (select * from user_tab_columns
    where
    table_name=tab_rec.table_name
    order by
    column_id)
    loop
    if col_rec.column_id=1 then
    v_tempa:=v_tempa||'''||chr(10)||''';
    else
    v_tempa:=v_tempa||',''||chr(10)||''';
    v_tempb:=v_tempb||',''||chr(10)||''';
    end if;
    v_tempa:=v_tempa||col_rec.column_name;
    if instr(col_rec.data_type,'CHAR') > 0 then
    v_tempc:='''''''''||'||col_rec.column_name||'||''''''''';
    elsif instr(col_rec.data_type,'DATE') > 0 then
    v_tempc:='''to_date(''''''||to_char('||col_rec.column_name||',''mm/dd/yyyy hh24:mi'')||'''''',''''mm/dd/yyyy hh24:mi'''')''';
    else
    v_tempc:=col_rec.column_name;
    end if;
    v_tempb:=v_tempb||'''||decode('||col_rec.column_name||',Null,''Null'','||v_tempc||')||''';
    end loop;
    v_tempa:=v_tempa||') values ('||v_tempb||');'' from '||tab_rec.table_name||';';
    end loop;
    if Not b_found then
    v_tempa:='-- Table '||v_table_name||' not found';
    else
    v_tempa:=v_tempa||chr(10)||'select ''-- commit;'' from dual;';
    end if;
    return v_tempa;
    end;
    show errors
    -- STEP 2: Run the following code to extract the data.
    set head off
    set pages 0
    set trims on
    set lines 2000
    set feed off
    set echo off
    var retline varchar2(4000)
    spool c:\t1.sql
    select 'set echo off' from dual;
    select 'spool c:\recreatedata.sql' from dual;
    select 'select ''-- This data was extracted on ''||to_char(sysdate,''mm/dd/yyyy hh24:mi'') from dual;' from dual;
    -- Repeat the following two lines as many times as tables you want to extract
    exec :retline:=ExtractData('dept');
    print :retline;
    exec :retline:=ExtractData('emp');
    print :retline;
    select 'spool off' from dual;
    spool off
    @c:\t1
    -- STEP3: Run the spooled output c:\recreatedata.sql to recreate data.
    Source:http://www.idevelopment.info/data/Oracle/DBA_tips/PL_SQL/PLSQL_5.shtml
    </pre>

    Thanks Justin.
    I get what you are saying,i really wanted to see the output of the codes, because the furtherst i could get from that code is
    SELECT EXTRACTDATA('MYTABLE') FROM MYTABLE;
    and it generated this:
    "select 'insert into MYTABLE ('||chr(10)||'DATE1,'||chr(10)||'TIME1,'||chr(10)||'COUNTS) values ('||decode(DATE1,Null,'Null','to_date('''||to_char(DATE1,'mm/dd/yyyy hh24:mi')||''',''mm/dd/yyyy hh24:mi'')')||','||chr(10)||''||decode(TIME1,Null,'Null',TIME1)||','||chr(10)||''||decode(COUNTS,Null,'Null',COUNTS)||');' from MYTABLE;
    select '-- commit;' from dual;"
    "select 'insert into MYTABLE ('||chr(10)||'DATE1,'||chr(10)||'TIME1,'||chr(10)||'COUNTS) values ('||decode(DATE1,Null,'Null','to_date('''||to_char(DATE1,'mm/dd/yyyy hh24:mi')||''',''mm/dd/yyyy hh24:mi'')')||','||chr(10)||''||decode(TIME1,Null,'Null',TIME1)||','||chr(10)||''||decode(COUNTS,Null,'Null',COUNTS)||');' from MYTABLE;
    select '-- commit;' from dual;"
    "select 'insert into MYTABLE ('||chr(10)||'DATE1,'||chr(10)||'TIME1,'||chr(10)||'COUNTS) values ('||decode(DATE1,Null,'Null','to_date('''||to_char(DATE1,'mm/dd/yyyy hh24:mi')||''',''mm/dd/yyyy hh24:mi'')')||','||chr(10)||''||decode(TIME1,Null,'Null',TIME1)||','||chr(10)||''||decode(COUNTS,Null,'Null',COUNTS)||');' from MYTABLE;
    select '-- commit;' from dual;"
    "select 'insert into MYTABLE ('||chr(10)||'DATE1,'||chr(10)||'TIME1,'||chr(10)||'COUNTS) values ('||decode(DATE1,Null,'Null','to_date('''||to_char(DATE1,'mm/dd/yyyy hh24:mi')||''',''mm/dd/yyyy hh24:mi'')')||','||chr(10)||''||decode(TIME1,Null,'Null',TIME1)||','||chr(10)||''||decode(COUNTS,Null,'Null',COUNTS)||');' from MYTABLE;
    select '-- commit;' from dual;"
    "select 'insert into MYTABLE ('||chr(10)||'DATE1,'||chr(10)||'TIME1,'||chr(10)||'COUNTS) values ('||decode(DATE1,Null,'Null','to_date('''||to_char(DATE1,'mm/dd/yyyy hh24:mi')||''',''mm/dd/yyyy hh24:mi'')')||','||chr(10)||''||decode(TIME1,Null,'Null',TIME1)||','||chr(10)||''||decode(COUNTS,Null,'Null',COUNTS)||');' from MYTABLE;
    select '-- commit;' from dual;"
    "select 'insert into MYTABLE ('||chr(10)||'DATE1,'||chr(10)||'TIME1,'||chr(10)||'COUNTS) values ('||decode(DATE1,Null,'Null','to_date('''||to_char(DATE1,'mm/dd/yyyy hh24:mi')||''',''mm/dd/yyyy hh24:mi'')')||','||chr(10)||''||decode(TIME1,Null,'Null',TIME1)||','||chr(10)||''||decode(COUNTS,Null,'Null',COUNTS)||');' from MYTABLE;
    select '-- commit;' from dual;"
    I was expecting a string of
    insert into mytable values (19/1/2009,1,1);
    insert into mytable values (19/10/2008,5,10);
    Thanks for the explanation .

  • Insert statement executes before If statements

    Hello All,
    I am trying to validate few fields before inserting them into table.
    for example:
    IF params('txtID') IS NULL THEN
    "SOME MSG";
    ELSIF params('txtName') IS NULL THEN
    "SOME MSG";
    END IF;
    INSERT INTO TABLE T1
    VALUES(params('txtID'),params('txtName');
    What I want to do is : I want to validate two fields before inserting.
    Can anyone help me?
    Thank you,
    H.

    It directly executes insert statement. If I delete insert, it executes that IF statement.
    It is strange.Let's be clear about this, it is not strange. Your original IF statements did execute before the INSERT but they did not raise an exception. Consequently the INSERT statement was processed. Your "SOME MSG" mechanism is obviously only triggered after the procedure completed. Walter's solution worked for you because it raise exceptions, which prevented the INSERT statement from executing.
    Anyway, it is obvious that you haven't got NOT NULL enforced on your table, which is A Bad Thing. You shoudn't be enforcing such constraints through a procedure.
    Cheers, APC
    blog: http://radiofreetooting.blogspot.com
    Edited by: APC on Feb 3, 2009 5:48 PM
    Only because the OP edited their final post, and removed the lines I quoted.

  • How to assign value from insert statement to variable from a trigger

    Hi,
    I got this really annoying problem and I don't know if I am doing it correctly.
    I have a BEFORE INSERT trigger on a table. When someone executes an insert statement I want to grab the value of a column from that statement and assign it to a variable and then do stuff with it. I'm stuck on the assignment.. look below..
    CREATE OR REPLACE TRIGGER CARS.geotest2_trigger
    BEFORE INSERT ON CARS.GEO_TEST2
    FOR EACH ROW
    DECLARE
    v_chainkey nchar(32);
    v_chainkey2 nchar(32);
    not_exists EXCEPTION;
    BEGIN
    :NEW.CHAINKEY := v_chainkey;
    SELECT GEO_TEST.CHAINKEY INTO v_chainkey2 FROM GEO_TEST WHERE GEO_TEST.CHAINKEY = v_chainkey;
    IF v_chainkey2 = '' or v_chainkey2 is null THEN
    RAISE not_exists;
    ELSE
    INSERT INTO GEO_TEST2 VALUES(:NEW.CHAINKEY, :NEW.BLA, :NEW.FOO);
    END IF;
    EXCEPTION
    WHEN not_exists THEN
    RAISE_APPLICATION_ERROR(-20010, 'Chainkey does not exist in parent table GEO_TEST');
    END;
    I keep getting this error
    Error: ORA-04098: trigger 'CARS.GEOTEST2_TRIGGER' is invalid and failed re-validation
    SQLState: 42000
    ErrorCode: 4098

    It isn't assigning because v_chainkey is not at the left hand side of the assignment statement.
    test@ORA10G>
    test@ORA10G>
    test@ORA10G> declare
      2    x  number := 5;
      3    y  number;
      4  begin
      5    x := y; -- does not assign anything to y; assigns NULL to x,
      6            -- because y is NULL at this point
      7            -- so, essentially the value 5 of x is *LOST* now
      8    dbms_output.put_line('x = '||x);
      9  end;
    10  /
    x =
    PL/SQL procedure successfully completed.
    test@ORA10G>
    test@ORA10G>
    test@ORA10G>In any case, here's what you are probably looking for:
    test@ORA10G>
    test@ORA10G> --
    test@ORA10G> drop table geo_test;
    drop table geo_test
    ERROR at line 1:
    ORA-00942: table or view does not exist
    test@ORA10G> drop table geo_test2;
    drop table geo_test2
    ERROR at line 1:
    ORA-00942: table or view does not exist
    test@ORA10G>
    test@ORA10G> create table geo_test (chainkey nchar(32));
    Table created.
    test@ORA10G> insert into  geo_test (chainkey) values ('a');
    1 row created.
    test@ORA10G> insert into  geo_test (chainkey) values ('');
    1 row created.
    test@ORA10G>
    test@ORA10G> create table geo_test2 (chainkey nchar(32), bla number(1), foo number(1));
    Table created.
    test@ORA10G>
    test@ORA10G>
    test@ORA10G> CREATE OR REPLACE TRIGGER geotest2_trigger
      2  BEFORE INSERT ON GEO_TEST2
      3  FOR EACH ROW
      4  DECLARE
      5    v_chainkey2  nchar(32);
      6    not_exists   EXCEPTION;
      7  BEGIN
      8    SELECT GEO_TEST.CHAINKEY INTO v_chainkey2 FROM GEO_TEST WHERE nvl(GEO_TEST.CHAINKEY,'~') = nvl(:new.chainkey,'~');
      9    IF v_chainkey2 is null THEN
    10      RAISE not_exists;
    11    END IF;
    12  EXCEPTION
    13    WHEN not_exists THEN
    14      RAISE_APPLICATION_ERROR(-20010, 'Chainkey does not exist in parent table GEO_TEST');
    15  END;
    16  /
    Trigger created.
    test@ORA10G>
    test@ORA10G> --
    test@ORA10G> insert into geo_test2 (chainkey,bla,foo) values ('b',1,1);
    insert into geo_test2 (chainkey,bla,foo) values ('b',1,1)
    ERROR at line 1:
    ORA-01403: no data found
    ORA-06512: at "TEST.GEOTEST2_TRIGGER", line 5
    ORA-04088: error during execution of trigger 'TEST.GEOTEST2_TRIGGER'
    test@ORA10G>
    test@ORA10G> --
    test@ORA10G> insert into geo_test2 (chainkey,bla,foo) values (null,1,1);
    insert into geo_test2 (chainkey,bla,foo) values (null,1,1)
    ERROR at line 1:
    ORA-20010: Chainkey does not exist in parent table GEO_TEST
    ORA-06512: at "TEST.GEOTEST2_TRIGGER", line 11
    ORA-04088: error during execution of trigger 'TEST.GEOTEST2_TRIGGER'
    test@ORA10G>
    test@ORA10G> --
    test@ORA10G> insert into geo_test2 (chainkey,bla,foo) values ('a',1,1);
    1 row created.
    test@ORA10G>
    test@ORA10G>
    test@ORA10G>pratz
    I think the sole purpose of that "not_exists" exception is this -
    If you try to insert a NULL value for GEO_TEST2.CHAINKEY, then this trigger will throw an error even if at least one NULL value exists in GEO_TEST.CHAINKEY column.
    Not sure if that's something that you wanted.
    Message was edited by:
    pratz

  • Question on Creating Table From Insert statement

    Hi,
    I want to create a table using a DBlink from another database using the create/insert statement. Will it also transfer the constraints or do I have to do it manually afterwards? Thanks.
    Create table T1
    SELECT a,b,c from T2 from DB@dblink;

    As discussed above, the constraints, indexes, triggers are not copied. Only NOT NULL constraints are copied.
    Look at this thread Re: Copying table structure.
    for copying triggers/indexes from source table to destination table.
    Thanks,
    Navaneeth

  • I want to change screenshot location from my Solid State hard drive with OSX on it, to my 2nd storage hard drive, how do I do this?

    I want to change screenshot location from my Solid State hard drive with OSX on it, to my 2nd storage hard drive, how do I do this?
    I am aware of "defaults write com.apple.screencapture *location*" but I want to change the location to my 2nd drive which doesn't have OSX on it... everytime I've tried to insert MacintoshHD2 in there it just doesn't work... any tips?

    I'm having the same problem.  I've entered the following path in terminal:
    "defaults write.com.apple.screencapture location /Volumes/Macintosh HD 2/[my username]/Downloads"
    But it's not working.  It keeps going to the SSD Downloads folder.
    What am I doing wrong?

  • How to retrieve value from insert statement

    hi,
    how can i get a certain value from insert statement and store it in a variable.
    suppose i am inserting emp_no , emp_name , emp_salary to employee table
    and i want to store the emp_name in a variable for other processing,
    how can i accomplish this ? i'm guessing that i have to use trigger,
    but dont know the procedure .
    any help will be greatly appreciated
    thanks

    insert into <table> valiues (....) returning <expression> into <variable>
    You could and should have found this using the SQL Language reference manual
    or
    http://www.morganslibrary.org/reference/insert.html
    Sybrand Bakker
    Senior Oracle DBA

  • Insert statement does not insert all records from a partitioned table

    Hi
    I need to insert records in to a table from a partitioned table.I set up a job and to my surprise i found that the insert statement is not inserting all the records on the partitioned table.
    for example when i am using select statement on to a partitioned table
    it gives me 400 records but when i insert it gives me only 100 records.
    can anyone help in this matter.

    INSERT INTO TABLENAME(COLUMNS)
    (SELECT *
    FROM SCHEMA1.TABLENAME1
    JOIN SCHEMA2.TABLENAME2a
    ON CONDITION
    JOIN SCHEMA2.TABLENAME2 b
    ON CONDITION AND CONDITION
    WHERE CONDITION
    AND CONDITION
    AND CONDITION
    AND CONDITION
    AND (CONDITION
    HAVING SUM(COLUMN) > 0
    GROUP BY COLUMNS

  • Generate insert statement using columns from all_tab_cols view

    i am trying to generate a dynamic insert statement using the columns for a table from the all_tab_cols view. if i do a select, i get the output as rows. How do i convert the row out to columns so that i get something like this : INSERT INTO TABLE_NAME (COL1, COL2, COL3,COL4.....) .
    Any help will be appreciated!

    SQL> select * from my_test;
    no rows selected
    SQL> desc my_Test;
    Name                                      Null?    Type
    COL1                                               NUMBER
    COL2                                               NUMBER
    COL3                                               NUMBER
    SQL> declare
      2  cursor c1 is select column_name from all_tab_cols where table_name = 'MY_TEST';
      3  v_sql VARCHAR2(10000) := NULL;
      4  v_cnt NUMBER := 0;
      5  BEGIN
      6  FOR I in C1 LOOP
      7  v_cnt := v_cnt + 1;
      8  v_sql := 'INSERT INTO my_test('||I.column_name||') values('||v_cnt||')';
      9  EXECUTE IMMEDIATE v_sql;
    10  END LOOP;
    11  COMMIT;
    12  end;
    13  /
    PL/SQL procedure successfully completed.
    SQL> select * from my_Test;
          COL1       COL2       COL3
             1
                        2
                                   3
    SQL>

  • I created an envelope template and want to print envelopes inserting addresses from a contact group in address book

    I created an envelope template in Pages and want to print envelopes inserting addresses from a contact group in address book. How do I do that?

    On what word processor will that be?

  • Insert records from a single table to two related tables

    DB - 10G
    OS - XP
    We have many thousands of comma delimited records that we want to insert into a normalised table structure.
    I have created a test dataset that can be found at the end of this post.
    I have csv records that look like this;
    donald, huey
    donald, dewey
    donald, louie
    And I want the data to be inserted into two separate table like this;
    table named PARENTS
    pk parent_name
    1  donald
    Table named CHILDRENS
    pk fk child_name
    1  1 huey
    1  2 dewey
    1  3 louie
    I constructed an insert statement that looks like this;
    INSERT ALL
      INTO parents (parent_id, parent_name) VALUES (parents_seq.nextval, parent_name)
      INTO children (children_id, parent_id, child_name) VALUES(children_seq.nextval, parents_seq.nextval, child_name )
    SELECT parent_name, child_name
      FROM sources;
    And this resulted in the following;
    Table named PARENTS
    pk child_name
    1  DONALD
    2  DONALD
    3  DONALD
    Table named PARENTS
    pk fk child_name
    1  1  HUEY
    2  2  DEWEY
    3  3  LOUIE
    Would anyone have any ideas on how I could accomplish this task?
    This is some example data;
    [code]
    DROP SEQUENCE parents_seq;
    DROP SEQUENCE sources_seq;
    DROP SEQUENCE children_seq;
    DROP TABLE sources;
    DROP TABLE children;
    DROP TABLE parents;
    CREATE SEQUENCE PARENTS_SEQ MINVALUE 1 MAXVALUE 1000000000000000000000000000 INCREMENT BY 1 START WITH 1 CACHE 20 NOORDER NOCYCLE;
    CREATE TABLE PARENTS
    ( PARENT_ID      NUMBER(8) NOT NULL
    , PARENT_NAME    VARCHAR2 (50 CHAR) NOT NULL
    , CONSTRAINT     PARENTS_PK PRIMARY KEY (PARENT_ID)
    create or replace
    TRIGGER PARENTS_BI
    BEFORE
      INSERT OR UPDATE ON PARENTS
      FOR EACH ROW BEGIN
      IF INSERTING THEN
        IF :NEW.PARENT_ID IS NULL THEN
          SELECT PARENTS_SEQ.nextval INTO :NEW.PARENT_ID FROM dual;
        END IF;
      END IF;
    END;
    CREATE SEQUENCE SOURCES_SEQ MINVALUE 1 MAXVALUE 1000000000000000000000000000 INCREMENT BY 1 START WITH 1 CACHE 20 NOORDER NOCYCLE;
    CREATE TABLE SOURCES
    ( SOURCE_ID      NUMBER(8) NOT NULL
    , PARENT_NAME    VARCHAR2 (50 CHAR) NOT NULL
    , CHILD_NAME    VARCHAR2 (50 CHAR) NOT NULL
    , CONSTRAINT     SOURCES_PK PRIMARY KEY (SOURCE_ID)
    create or replace
    TRIGGER SOURCES_BI
    BEFORE
      INSERT OR UPDATE ON SOURCES
      FOR EACH ROW BEGIN
      IF INSERTING THEN
        IF :NEW.SOURCE_ID IS NULL THEN
          SELECT SOURCES_SEQ.nextval INTO :NEW.SOURCE_ID FROM dual;
        END IF;
      END IF;
    END;
    INSERT INTO SOURCES (PARENT_NAME, CHILD_NAME) VALUES ('DONALD', 'HUEY');
    INSERT INTO SOURCES (PARENT_NAME, CHILD_NAME) VALUES ('DONALD', 'DEWEY');
    INSERT INTO SOURCES (PARENT_NAME, CHILD_NAME) VALUES ('DONALD', 'LOUIE');
    Commit;
    CREATE SEQUENCE CHILDREN_SEQ MINVALUE 1 MAXVALUE 1000000000000000000000000000 INCREMENT BY 1 START WITH 1 CACHE 20 NOORDER NOCYCLE;
    PROMPT *** CREATE TABLE ***
    CREATE TABLE CHILDREN
    ( CHILDREN_ID    NUMBER NOT NULL
    , PARENT_ID      NUMBER NOT NULL
    , CHILD_NAME     VARCHAR2 (50 CHAR) NOT NULL
    , CONSTRAINT     CHILDREN_PK PRIMARY KEY (CHILDREN_ID)
    create or replace
    TRIGGER CHILDREN_BI
    BEFORE
      INSERT OR UPDATE ON CHILDREN
      FOR EACH ROW BEGIN
      IF INSERTING THEN
        IF :NEW.CHILDREN_ID IS NULL THEN
          SELECT CHILDREN_SEQ.nextval INTO :NEW.CHILDREN_ID FROM dual;
        END IF;
      END IF;
    END;
    [code]

    Looks like this is one way of achieving it:
    insert into parents (parent_name) select distinct parent_name from sources;
    select *
      from parents;
    PARENT_ID              PARENT_NAME                                       
    1                      DONALD
    insert into children (parent_id, child_name)
    select p.parent_id, s.child_name
      from sources s
      join parents p
        on (s.parent_name = p.parent_name);
    select *
      from children;
    CHILDREN_ID            PARENT_ID              CHILD_NAME                                        
    1                      1                      HUEY                                              
    2                      1                      DEWEY                                             
    3                      1                      LOUIE

  • Help needed in forming dynamic insert statement

    HI,
    I am trying to construct a insert statement as given below.
    Please help me as to how to form this and where am i going wrong?
    i want the columns names to be auto populated when a table name is given.
    output should be like:
    ==============
    insert into tablename
    col1,col2,col3..coln)
    values
    (col1, col2, col3..coln);
    declare
    v_sql varchar2(5000);
    cnt number :=0;
    col_count number;
    name varchar2(5000); 
    TYPE string_assarrtype IS TABLE OF VARCHAR2 ( 25 ) INDEX BY VARCHAR2 ( 20 );
    arr string_assarrtype;
    BEGIN
       select count(column_name) into col_count from user_tab_columns
      where
      table_name = 'PS_D_RE_BCSREGIONMAPPING';
       v_sql:='insert into tablename (' ;
         for i  in 1..arr(col_count)
          loop
           v_sql:='v_sql' ||' '||arr(i);
          DBMS_OUTPUT.PUT_lINE(v_sql);
         END LOOP;
    END;

    Below is the actual code which i am trying to write:
    Since the below trigger logic is same for around 25/30 tables i wanted to construct a generic procedure which will take input parameter as tablename and auit_table and form the insert statement which will populate the column names which are different for different tables.
    Please guide...
    CREATE OR REPLACE PROCEDURE make_trigger(  tablename  in  VARCHAR2,
                                             auditable  in   VARCHAR2                                                                                  
    AS
    v_sql varchar(10000);
    begin
    v_sql := 'DROP TRIGGER  TRD_D_AUDIT_D'||tablename;
    dbms_output.put_line (v_sql );
    v_sql:='CREATE OR REPLACE TRIGGER TRG_D_AUDIT_D_'||tablename||' '||
    'BEFORE INSERT OR UPDATE OR DELETE ON 'tablename||' '||' REFERENCING OLD AS OLD NEW AS NEW
    FOR EACH ROW';
    dbms_output.put_line (v_sql );
    v_sql:= 'DISABLE
    DECLARE
    v_Operator        psoprdefn.oprid%type;
    v_Sysdate        Date;
    v_Actn            varchar2(1);
    V_AUDIT_RECNAME    PSRECDEFN.RECNAME%type;
    BEGIN';
    dbms_output.put_line (v_sql );
    v_sql:= 'If (Inserting) Then
       v_Actn    :='||'''||'A'||'''||';'||
       'Insert into '||auditable||'
                        ('||
                       AUDIT_RECNAME,
                    AUDIT_ACTN,
                    AUDIT_OPRID,
                    AUDIT_STAMP,
                    D_USER_GROUP,
                    D_GROUP_DESC,
                    D_BCS_ID,
                    D_UBR_ID,
                    D_FILTER_ID,
                    D_NAME_ACCESS_TYPE,
                    D_GLOBAL_EMP_LIST
                        Values
                    V_AUDIT_RECNAME,
                    v_Actn,
                    v_Operator,
                    v_Sysdate,
                    :NEW.D_USER_GROUP,
                    :NEW.D_GROUP_DESC,
                    :NEW.D_BCS_ID,
                    :NEW.D_UBR_ID,
                    :NEW.D_FILTER_ID,
                    :NEW.D_NAME_ACCESS_TYPE,
                    :NEW.D_GLOBAL_EMP_LIST
    end if;
    end;Edited by: user11942774 on Oct 5, 2012 11:46 AM
    Edited by: user11942774 on Oct 5, 2012 8:09 PM
    Edited by: user11942774 on Oct 5, 2012 8:10 PM

Maybe you are looking for

  • Error while Executing from BI 7.0 (Query Designer)

    while exicutting query from Query designer i BI 7.0. I m getting following error message. Please check and reply back... The following error was encountered: Unable to determine IP address from host name for bidev.nt_dolvi The dnsserver returned: Nam

  • Explanations on Sales Documents

    Can anybody send explanations about sales documents that I have mentioned below? Inquiry     IN Quotation     QT Free-of-charge delivery     FD Standard sales order     OR Cash sale     BV Rush order     SO Quantity contract     CQ Maintenance contra

  • How do I get back to main screen on itunes from ipad screen

    Have new ipad and stuck on ipad screen in itunes; don't know how to get back to main screen to get info and support icon

  • Cannot get Vevo to work on Apple TV stuck on "accessing vevo"

    Bought a brand new apple tv, wanted to watch some music videos but i just get the "Accessing Vevo" then an error saying media not available? Everything else works, ive noticed that a few other people get the same problem but noone has a solution as y

  • Configure external monitor to be --left-of laptop screen (nouveau)

    Hi, I have problem configuring external monitor to work like nvidia's 'twinView'. Having no xorg.conf my external monitor is configured just to clone image displayed on latop screen. I can setup correct resolution in my DE (I'm using XFCE), but what