Need to insert test data in all the tables that were present in a schema

Hello

I use Oracle 9i.

10 tables were present in my current schema. All tables are not having all the records.

I need to insert test data in each table using a single procedure. (the table name is inparameter for the procedure)

For example, I test_emp, test_sal tables.

CREATE TABLE test_emp (ename varchar2 (40), number of sal);
CREATE TABLE test_sal (it NUMBER, rank NUMBER, will designate the NUMBER);

I need to insert some test data in these tables. I shouldn't use utl_file or sql * loader for this.

I tried below way.
-- Incomplete code

CREATE OR REPLACE PROCEDURE test_proc_insertdata(p_table_name IN VARCHAR2) IS

BEGIN

  --Selected all the column_names, data_type, data_length into a collection variable from user_tab_columns.

  SELECT u.column_name, u.data_type, u.data_length
  --BULK COLLECT INTO l_col_tab
    FROM user_tab_columns u
   WHERE u.table_name = p_table_name;

  -- generated the values using below statement
  
    SELECT decode(u.DATA_TYPE,'VARCHAR2',dbms_random.string('A', u.data_length),'NUMBER',TRUNC(dbms_random.value(1, 5000)),'DATE',SYSDATE) val_list
    -- INTO collection varaible
      FROM user_tab_columns u
     WHERE TABLE_NAME = p_table_name;  
   

END;
I am not able to transmit these values to the collection dynamically to the INSERT statement. I tried many ways, but I am not satisfied with the code I've written.

Please help me if you know a better idea.



Thank you
Suri

Suri wrote:
Hello

I use Oracle 9i.

10 tables were present in my current schema. All tables are not having all the records.

I need to insert test data in each table using a single procedure. (the table name is inparameter for the procedure)

For example, I test_emp, test_sal tables.

CREATE TABLE test_emp (ename varchar2 (40), number of sal);
CREATE TABLE test_sal (it NUMBER, rank NUMBER, will designate the NUMBER);

I need to insert some test data in these tables. I shouldn't use utl_file or sql * loader for this.

I tried below way.

-- Incomplete code

CREATE OR REPLACE PROCEDURE test_proc_insertdata(p_table_name IN VARCHAR2) IS

BEGIN

--Selected all the column_names, data_type, data_length into a collection variable from user_tab_columns.

SELECT u.column_name, u.data_type, u.data_length
--BULK COLLECT INTO l_col_tab
FROM user_tab_columns u
WHERE u.table_name = p_table_name;

-- generated the values using below statement

SELECT decode(u.DATA_TYPE,'VARCHAR2',dbms_random.string('A', u.data_length),'NUMBER',TRUNC(dbms_random.value(1, 5000)),'DATE',SYSDATE) val_list
-- INTO collection varaible
FROM user_tab_columns u
WHERE TABLE_NAME = p_table_name;  

END;

I am not able to transmit these values to the collection dynamically to the INSERT statement. I tried many ways, but I am not satisfied with the code I've written.

Please help me if you know a better idea.

Thank you
Suri

With some effort, you can write dynamic insert queries. Read the data dictionary views ALL_TABLES and ALL_TAB_COLUMNS to get the data you need. You may need to limit the list to the tables you specify to hardcode the names of the tables. You can read the views for the information of table and column that you need generate the insert column orders. It will be a lot of work and it might be easier to simply hardcode values. Dynamic SQL is difficult to debug and work with: (.) Something like this (untested and totally undebugged)

declare
  cursor v_tab_col(p_table_name) is select * from all_tab_columns where table_name = p_table_name;
  v_text varchar2(32767);
  v_table_name varchar2(30);
begin
  v_table_name := 'DUAL';
  --chr(10) is a newline for readability when debugging
  v_text := 'insert into '||v_table_name||chr(10)||'(';
  --loop to build column list
  for v_tab_rec in v_tab_col loop
    --add comma if not first item in list
    if v_tab_col%rowcount > 1 then
      v_text := v_text ||',';
    end if;
    v_text := v_text || v_tab_rec.column_name;
  end loop;
  v_text := v_text ||') values ('||
  --loop to build column values
  for v_tab_rec in v_tab_cur loop
    if (v_tab_rec.datatype = 'VARCHAR2') then
      --add comma if not first item in list
      if v_tab_col%rowcount > 1 then
        v_text := v_text ||',';
      end if;
      if v_tab_rec.datatype = 'NUMBER' then
       v_text := v_text || '9';
       elsif v_tab_rec.datatype = 'VARCHAR2' then
         v_text := v_text ||'X';
      end if;
  end loop;
  v_text := v_text || ')';
  execute immediate v_text;
end;

This code is not effective, but shows how to do what you described. You'll have to code for all data types you need and find a way to take into account the primary/foreign keys, yourself. I leave it to you to find a way to avoid selecting the data twice ;)
Good luck!

Published by: riedelme on July 24, 2012 13:58

Tags: Database

Similar Questions

  • I bought creative cloud of surfspot.nl and I got a redemption code so I cloud go to the version of track to the version of the membership. I did all the steps that were needed, but my creative cloud is still in trail version. I have a windows computer.

    I bought creative cloud of surfspot.nl and I got a redemption code so I cloud go to the version of track to the version of the membership.

    I did all the steps that were needed, but my creative cloud is still in trail version.

    I have a windows computer.

    I hope someone can help me because I have really bad need for my study.

    If you tried the steps mentioned in the link below, you will need to contact the Support from Adobe and check with the subscription once account.

    https://helpx.Adobe.com/Creative-Suite/KB/trial--1-launch.html

    Click always need Help (help), and then select the Chat option:

    Contact the customer service

  • How to list all the tables that have no data

    Hi all


    I want to list all the tables that have no data. (registration number is 0)

    Someone help me.

    Thank you
    Thiensu2810

    Select owner, table_name from dba_tables where num_rows = 0;

    This assumes that the table statistics are COMMON.

  • Query to find all the tasks that were running for a particular user

    Hi all

    When you see the 'history of resource' of a particular resource for a user, we can see all the tasks that have been completed for the user.

    I have a requirement which is, I need to display the same information via a database query.

    Any help in this regard will be highly appreciated.

    Kind regards.

    OSI table contains the final task instance data.  If you see a task rejected 5 times, it will be so only one entry of osi.

    Select usr.usr_login, obj.obj_name, mil.mil_name, sch.sch_status

    OSI, mil, CHS, orc, pkg, usr, obj

    where osi.mil_key = mil.mil_key

    and osi.sch_key = sch.sch_key

    and osi.orc_key = orc.orc_key

    and orc.usr_key = usr.usr_key

    and osi.pkg_key = pkg.pkg_key

    and pkg.obj_key = obj.obj_key

    and upper (usr.usr_login) = upper(:usr_login)

    and upper (obj.obj_name) = upper(:obj_name)

    You can provide the user login name and resources, and this will provide you with all their tasks given.

    -Kevin

  • I transferred data from backup, but have not all the books that were on my Kindle App. How tdo I get back them?

    I upgraded to the iPad 2 Air after all iCloud backup data transfer that I found I was missing most of the books from my kindle app that were supposed to be in the cloud. How can I get the my new iPad?

    If they were purchased through the Kindle books (i.e. Amazon) you can open the Kindle application on your new iPad, if not already in the tap of the library the 3 bars on the high left and click library, and then tap cloud down, tap and hold any book in your library and then on download. All purchases are stored by Amazon and can be downloaded at any time.

  • User accounts - I created a user account, but now I can't seem to find all the files that were on the pc before I put it in place... what happened to them?

    I never had an aaount usser put on the pc and had a large number of backup files.  Then I set up a new user account and it auto saet as administrator, but all the files are not there.  There is an another user account on the pc and it is called comments but the files aren't there either.  Is there a way to recover the missing data?

    Hi ColleenBrown-Hickey,
     
    Setting up a new user account does not remove the files. Search if the file is present on the computer by using Windows Search.
     
    For more information, see:
  • Why the Firefox 4 is not restore all the sessions that were open at the time? Before the update, I've been invited either ("SAVE AND QUIT") or even if she closed UNEXPECTEDLY (crushed), he AGAIN restored. New version does not offer "save and exit".

    Question
    Why don't Firefox4 restore any sessions that were open at the time? Before the update, I've been invited either ('SAVE AND QUIT'), or even if it closed UNEXPECTEDLY (crash etc) it STILL restored everything. New version does not offer "save and exit".

    Now, Firefox always stores the old session which you can access from the history menu by selecting "restore previous Session."

    If you always open the last set of tabs, another approach is the following:

    1. Click on the orange button Firefox, then select options to open the options window
    2. Go to the general Panel
    3. Change the setting "When Firefox starts" to "show my windows and tabs from last time '.
  • Trying to find lost MyPicture directory along will be all the photos that were in the directory

    Help, please

    Thank you in advance for any help.

    Recently, I tried to organize my photos in my image directory. I don't know what happened, but in a Flash all my pictures have disappeared. I went looking for them, but I couldn't find even my image directory. I looked in the trash to find anything, so I tried to use the search function. I found some files that I had recently deleted, then when I clicked on them to restore, I received a message that the shortcut refers to a file that has been changed or moved. Does anyone know where they could. I tried to use the program smart undelete but did not find a thing. I'm out of ideas.

    Help, please

    Thank you in advance for any help.

    Recently, I tried to organize my photos in my image directory. I don't know what happened, but in a Flash all my pictures have disappeared. I went looking for them, but I couldn't find even my image directory. I looked in the trash to find anything, so I tried to use the search function. I found some files that I had recently deleted, then when I clicked on them to restore, I received a message that the shortcut refers to a file that has been changed or moved. Does anyone know where they could. I tried to use the program smart undelete but did not find a thing. I'm out of ideas.

    =============================
    "Changed or moved" may be a hint...

    Is it possible that the folder my pictures missing
    dragged / dropped accidentally in a different folder?

    Try doing a search for *.jpg

    Good luck. John Inzer - MS - MVP - Digital Media Experience - Notice_This is not tech support_I'm volunteer - Solutions that work for me may not work for you - * proceed at your own risk *.

  • Media Player deletes all the songs that were added just for the external drive

    I am using Windows 7 media player. I start the media player and my music list is empty.  Media Player begins to add songs to my portable media player. After adding songs it removes so all of the songs added.  What I can do to keep the songs is the library?

    Hello

    1. have you tried to play the songs of the external hard drive?
    When you remove the external hard drive, you will lose the library of the media player library list, it's the design of the media player library and this is how it works in the location list monitoring.

    However, if you want to keep the music files in the library even after having remove the external HARD drive, you do not delete the location in the location tracking list. You can add the location to avoid the problem.

    For more information, please visit:

    Hope this helps
  • Where are all the apps that were on my Apple TV?

    I just bought the newest gen Apple TV 4 and was surprised to see that my old Apple Tv apps were not available. No Netflix, National Geographic, Smithsonian, etc... They were part of the original Tv App. They are not part of the new TV App or am I missing something? I asked Siri to find Netflix and she said that I only had the app. Can I download everything I'm missing to have at my disposal? Looks like they would have kept the apps they had and included on the new Apple TV.

    Applications to use on ATV4 must be downloaded through the App Store you can find on the home screen of your ATV4. The ATV4 operating system is completely different from the operating system on ATV3.

  • Privilege for the creation and deletion of the indexes on all the tables/diagrams

    Hello

    I have a database running on the HPUnix bit ia64 platform. The version of the db is 10.2.0.3
    I have a schema named DPYBATCH.

    I need grant creation/deleting indexes on all the tables/diagrams.
    I know create a clue will solve the purpose of the creation. But I am confused with deleting indexes.

    If I give-> GRANT CREATE ANY INDEX OF DPYBATCH, it will not solve the purpose. He will have the privilege to be able to delete/move the index as well?
    If this is not the case, what is the privilege to grant to delete/move the index. Please let me know!

    Thank you!

    You must use DROP ALL INDEX: http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_8015.htm#i2066885.

  • SQL statement to select the tables that are updated today...

    Hi guys,.

    I need to find the names of all the tables that contain rows that are inserted/updated on a given time stamp...

    Below the statement gives me the list of all tables in the database...
    select t.table_name from all_tables t;
    All these tables in the database have a rowversion column which gives the date of update/insertion of a record. I need to write a select statement (probably dynamic) that will give me the names of the tables that contain the rowversion value 24/01/2013...


    Any help is appreciated...

    Napster says:
    Hi kitsoukou,

    Thanks for your reply...

    But when I run your select statement I get an error indicating that the table SYS. DBMS_XMLGEN does not exist.

    Probably something wrong in my environment?

    If I'm not mistaken there are DBMS_XMLGEN 9i.

    The source code is available under

    karthick% ls -lrt $ORACLE_HOME/rdbms/admin/dbmsxml.sql
    -rw-r--r--   1 oracle   dba         7977 Mar 23  2004 /home/oracle/product/10.2.0.5/rdbms/admin/dbmsxml.sql
    

    You can take this and run to install DBMS_XMLGEN.

  • Need to create a new line in the table with the same data as the primary key, but new PK

    Hello gurus,

    I have a table with a column as a primary key, I need to create a new row in the table, but with the same data in one of the lines, but with different primary keys, focus a double row with key primary diferent...

    Any ideas of how it can be done without much complication?

    Thanks in advance for your answer.

    Agress,
    Karim idrissi

    user9970447 wrote:
    Hello gurus,

    I have a table with a column as a primary key, I need to create a new row in the table, but with the same data in one of the lines, but with different primary keys, focus a double row with key primary diferent...

    Any ideas of how it can be done without much complication?

    Thanks in advance for your answer.

    Agress,
    Karim idrissi

    something like

    insert into mytable values ('literal for new pk',
                                           select non-pk-1,
                                                    non-pk-2,
                                                    non-pk-n
                                           from mytable
                                           where pk-col = 'literal for existing pk')
    
  • How do to know all the tables on peoplesoft (on Oracle 8.1) with data

    I need to know all the tables on peoplesoft (on Oracle 8.1) < strong > with < facilities > data
    and ignore the tables without data.

    It takes to write a pl/sql procedure or is there an easy solution?
    I wrote an example of code, but I am disconnected programming
    for the long and I need help.
    Thanks in advance
    Pls answer
    _______________
    Cursosr curtab is select table_name from dba_tables;
    curtab() loop
    Select count (*) x from curtab;
    If x & gt; 0
    UTL_FILE. Write ('curtab')
    on the other
    null;
    endif;
    end loop;
    ____________________

    Julia wrote:
    I need to know all the tables on peoplesoft (on Oracle 8.1) with data
    > & ignore tables without data.
    >
    > It takes to write a pl/sql procedure or is there an easy solution?
    > I wrote a code example, but I am disconnected programming
    > for a long time and I need help.
    > Thanks in advance
    > Pls answer
    > _______________
    > Cursosr curtab is select table_name from dba_tables;
    > curtab() loop
    > select count (*) x from curtab;
    > If x > 0
    > utl_file.write ('curtab')
    > other
    > null;
    > endif;
    > end of loop;
    > ____________________

    Consider using the user_tables and it works the relevant schema rather than dba_tables breast or of all_tables as these allows you to specify the schema name too will require.

    set serveroutput on;
    
    DECLARE
      cursor curtab is
        select table_name
        from   user_tables;
      --from   all_tables where owner = '';
      v_cnt NUMBER;
    BEGIN
      FOR t IN curtab
      LOOP
        EXECUTE IMMEDIATE 'select count(*) from '||t.table_name INTO v_cnt;
        IF v_cnt > 0 THEN
          DBMS_OUTPUT.PUT_LINE(t.table_name);
        END IF;
      END LOOP;
    END;
    /
    

    Or...

    Ensure that statistics were collected for the schema and examine the value of num_rows in the user_tables view. ;)

  • How to install all the drivers that I need for my dell inspiron 1525 when I can't get on the internet?

    I have a dell inspiron 1525 with windows xp sp 2 on it and I had to wipe and reinstall xp on it b/c I got the BSOD. Now, I can not configure a network connection, or get all the drivers that I need and I can't go on the internet. Is it possible to use another computer to get the drivers I need for this? Do you need a specific driver to even set up a network connection?

    AngelaGraves,

    The drivers you need are on your disk drivers that came with your computer.

    Here's the order of installation of the drivers. How to download and install drivers in the correct order

    If you do not have the drivers disk, you can download the drivers from Dell drivers & downloads, then copy on a USB key or drive and install from there.

    Rick

Maybe you are looking for

  • Please can I have an answer to this

    Hello there I have a white macbook 2009 and... Is my macbook completely deposited and if yes is it I can trade in my current macbook for a better, or give an update of the graphics card? then I can play better in it also this macbook will receive the

  • Hearty errors «CGContext...» »

    Just installed OS X Yosemite, 33 of Firefox running and get tons of errors 'CGContext. "... ', such as these: CGContextTranslateCTM: invalid context 0x0 CGContextScaleCTM: invalid context 0x0 CGContextSetAlpha: invalid context 0x0 CGContextSetShouldA

  • Missing camera problem - cannot export (share options greyed out)

    I'm under El Capitan 10.11.3 10.2.3 FCPX I7 MacBook retina (2012) 8 GB RAM I worked on my calendar, creating several minutes of content.  In the background as well as media files import and transcoding, I noticed that all my stock options were grayed

  • Difficulty of printer

    Difficulty of printer

  • Equium A200-1VO - slow start with the Vista patch

    My Equium A200 takes a long time to start. I remember downloading a hotfix to fix this and it worked, but it has slowed down now to the top once again and I can't find the patch. Can someone help me please? I use Vista Home Premium