Home > Oracle Database / Applications Tips > Oracle database administrator > Delimited data set generator
Oracle Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

ORACLE DATABASE ADMINISTRATOR

Delimited data set generator


Ravindra Nookala
03.01.2005
Rating: -3.60- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


This tip is useful for making a backup of a given table's data in a delimited format. The spool file generated can then be used for future data loads using a tool like SQL*Loader or any VB application. The script works on Oracle 7.x, 8.x and 9.x.

REM This script should be run as SYSTEM/SYS ONLY.
REM This script generates a text file with data for the given table, owner and delimiter.
REM This script works for tables with columns of datatypes CHAR, VARCHAR2, NUMBER and DATE ONLY.

set feed off
SET TERMOUT ON
ACCEPT own CHAR prompt 'Enter Value For Schema Name
:<>: '; 
ACCEPT tn CHAR prompt 'Enter Value For Table Name
:<>: '; 
ACCEPT dl CHAR prompt 'Enter Value For Delimiter (Eg: | Or , Or : Or ;)
:<>: '; 
prompt
prompt Generating The Delimited Data Sets.... Please Wait...
prompt

set ver off

create table ins_text (lineno NUMBER,text varchar2(2000))
storage(initial 200k next 200k maxextents 512)
/

declare
cursor c1 is select owner,table_name,column_id,column_name from 
dba_tab_columns where owner=upper('&&own') and
table_name=upper('&&tn') 
order by column_id;

v_colid number;
v_maxcolid number;
v_line number:=1;
v_colname varchar2(100);
v_tname varchar2(100);
v_owner varchar2(100);
v_str varchar2(2000);
procedure write_out(p_line INTEGER,p_str VARCHAR2) is
begin
insert into ins_text (lineno,text) values
(p_line,p_str);
commit;
end;
begin
SELECT MAX(COLUMN_ID)INTO v_maxcolid FROM DBA_TAB_COLUMNS WHERE
OWNER=upper('&&own')
AND TABLE_NAME=upper('&&tn');

v_str:='Select';
write_out(v_line,v_str);
v_line:=v_line+1;

open c1;
loop
fetch c1 into v_owner,v_tname,v_colid,v_colname;
exit when c1%notfound;

if v_colid < v_maxcolid then 
v_str:=v_colname||'||'||chr(39)||'&dl'||chr(39)||'||';
write_out(v_line,v_str);
v_line:=v_line+1;
else 
v_str:=v_colname;
write_out(v_line,v_str);
v_line:=v_line+1;
end if;
end loop;
close c1;

v_str:='from '||v_owner||'.'||v_tname||';';
write_out(v_line,v_str);
v_line:=v_line+1;
end; 
/

set termout off
set linesize 200
set head off
set wrap on
set trimspool on
set pages 2000
spool Ins
select text from ins_text order by lineno;
spool off
spool TabToText
@ins.lst
spool off

DROP TABLE ins_text
/
set termout on
set feed on
set ver on
set head on
set linesize 80
set wrap off
undefine own
undefine tn
undefine dl
set echo on
ed TabToText.lst


Rate this Tip
To rate tips, you must be a member of SearchOracle.com.
Register now to start rating these tips. Log in if you are already a member.


Submit a Tip




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
Oracle database administrator
Understanding SQL string functions
What is the difference between a database engineer, architect and administrator?
Import on one table from dump file
Error during RMAN backup
Can I drop a column in SYS schema?
STATSPACK tool: transaction vs. execution measurement
Should I port from Microsoft Access?
How can I find statistics on total memory usage and database connections?
Installing multiple Oracle homes
Modifying SYS password in a RAC environment

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



Oracle Development Solutions - SQL, J2EE, XML, SOA
HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2003 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts