Solved

Creatting tables dynamically

Posted on 2011-09-05
7
394 Views
Last Modified: 2013-12-19
Hi

I have a data in a table as follows:

Col1-Col2-Col3-Col4-Col5-Col6-Col7-Col8-Col9-Col10>>XXX>>YYY

I need to create a table from the above table by splitting the 10 columns based on - seperator. Please suggest how to do it

Thx
Naren
0
Comment
Question by:knaren1975
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 15

Accepted Solution

by:
gplana earned 500 total points
ID: 36483275
I would create the structure of the table manually.

When created, you should create a program which reads every line of the raw data, process the text and then INSERT into new table.

You can create this program as a PL/SQL block. Something like this:

DECLARE
  myRowData VARCHAR2(4000);
  col1, col2, col3, col4, col5, col6, col7, col8, col9, col10 VARCHAR(500);
  pos, pos2 NUMBER;
BEGIN
  -- get your row data into variable myRowData (maybe with a SELECT and a cursor for each line ?

  pos = InStr(myRowData,'-');
  col1 = SubStr(myRowData,1,pos-1)

  pos2 = InStr(myRowData,'-',pos+1);
  col2 = SubStr(myRowData,pos+1,pos2-pos);
  pos = pos2

  pos2 = InStr(myRowData,'-',pos+1);
  col3 = SubStr(myRowData,pos+1,pos2-pos);
  pos = pos2

  pos2 = InStr(myRowData,'-',pos+1);
  col4 = SubStr(myRowData,pos+1,pos2-pos);
  pos = pos2

-- ... and so on
END;
/

Hope it helps.
0
 
LVL 15

Expert Comment

by:gplana
ID: 36483278
Also, of course you will need a while for every rowdata line (probably combined with a cursor) and you need the INSERT sentence after getting all cols.

Regards.
0
 

Author Comment

by:knaren1975
ID: 36483616
the no of cols will not be consistent, some time i will get 5 cols . some times 10 etc
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Expert Comment

by:gplana
ID: 36483694
In this case you can use DBMS_SQL package to dynamically execute a "CREATE TABLE" sentence.
Also you can detect if the returned value of InStr function is 0. In this case it means the text ('-') hasn't found.

Regards.
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 36488462
How do you get the data types for the table?
0
 

Expert Comment

by:pbarnes7
ID: 36539545
For the data types and sizes, it's simplest just to guess and set them all the VARCHAR(2000).  If you need to perform arithmetic operations on them, you can let Oracle do automatic data conversion for you, which will work much of the time (until you need it most).
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 36539629
If you are just going to 'guess' set it to the MAX of 4000 not 2000 unless you are running multi-byte character sets.  Also use varchar2:  varchar2(4000).

But that might break easily if you need more.  Then use a CLOB.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question