How to generate text file using spool command
How can I use a SPOOL command with DBMS_OUTPUT.PUT_LINE with PL/SQL to produce text file on oracle client machine.
You could try using a REF CURSOR as an alternative. Bare bones example listed below:
--- SQL Script
SET FEEDBACK OFF
COLUMN first_name FORMAT A25
COLUMN last_name FORMAT A25
VARIABLE example_data REFCURSOR
BEGIN
example.get_data(:example_data);
END;
SPOOL example_data.txt
PRINT example_data
SPOOL OFF
-----Output in example_data.txt
FIRST_NAME LAST_NAME
First Name 1 Last Name 1
First Name 2 Last Name 2
First Name 3 Last Name 3
First Name 4 Last Name 4
First Name 5 Last Name 5
First Name 6 Last Name 6
First Name 7 Last Name 7
First Name 8 Last Name 8
First Name 9 Last Name 9
First Name 10 Last Name 10
-----Example package used in SQL Script
CREATE OR REPLACE PACKAGE example
IS
TYPE result_set IS REF CURSOR;
PROCEDURE get_data
p_result_set OUT result_set
END;
SHOW ERRORS
CREATE OR REPLACE PACKAGE BODY example
IS
PROCEDURE get_data
p_result_set OUT result_set
IS
BEGIN
-- replace this with your query
OPEN p_result_set FOR
WITH fake_data AS (
SELECT
'First Name ' || level first_name,
'Last Name ' || level last_name
FROM DUAL
CONNECT BY LEVEL <= 10
SELECT *
FROM fake_data
END;
END;
SHOW ERRORS
SPOOL OFF
Similar Messages
-
Generating text file using PL/SQL
can any body send sample program how to generate text file using PL/SQL program.I want to store table data into that text file.
Hi,
try this.
Thanks
CREATE OR REPLACE procedure write_index (TABLE_NAME_ACCEPT VARCHAR2) IS
id UTL_FILE.FILE_TYPE;
name VARCHAR2(20) := 'e:\db_coca\input';
err VARCHAR2(100);
ColNameLength Integer;
NumberoFColumns Integer;
num NUMBER;
i integer;
loc_query VARCHAR2(2000);
cursor_name INTEGER;
ret INTEGER;
l_const_name VARCHAR2(30);
CURSOR c_columns(i_Constraint_name VARCHAR2) IS
SELECT *
FROM all_cons_columns
WHERE CONSTRAINT_NAME = i_Constraint_name
order by table_name;
BEGIN
DBMS_OUTPUT.PUT_LINE('BEFORe OPEN');
--id := UTL_FILE.FOPEN('e:\db_coca\input',name,'w');
--IF UTL_FILE.IS_OPEN(id) THEN
-- DBMS_OUTPUT.PUT_LINE('Opened');
--ELSE
-- DBMS_OUTPUT.PUT_LINE('Still Closed');
--END IF;
DBMS_OUTPUT.PUT_LINE('AFTER OPEN');
--The table generation script
loc_query := 'Select CONSTRAINT_NAME from user_constraints ';
loc_query := loc_query || ' where owner = ' || '''CPS''';
loc_query := loc_query || ' AND CONSTRAINT_TYPE = ' || '''P''';
dbms_output.put_line('TABLE_NAME_ACCEPT = ' || TABLE_NAME_ACCEPT);
IF NOT (TABLE_NAME_ACCEPT IS NULL ) THEN
loc_query := loc_query || ' AND TABLE_NAME like ''';
loc_query := loc_query || TABLE_NAME_ACCEPT ;
loc_query := loc_query || '''';
END IF;
cursor_name := DBMS_SQL.OPEN_CURSOR;
dbms_output.put_line('Query = ' || loc_query);
DBMS_SQL.PARSE(cursor_name, loc_query, DBMS_SQL.v7);
dbms_output.put_line('After parse');
dbms_sql.DEFINE_COLUMN(cursor_name, 1,l_const_name, 30);
ret := DBMS_SQL.EXECUTE(cursor_name);
-- IF ret > 0 THEN
LOOP
dbms_output.put_line('return = ' || ret);
IF DBMS_SQL.FETCH_ROWS(cursor_name) > 0 THEN
dbms_sql.COLUMN_VALUE(cursor_name, 1,l_const_name);
-- dbms_sql.BIND_VARIABLE(cursor_name,
l_const_name,CONSTRAINT_NAME);
-- FOR C10 in Table_Loop Loop --The cursor
for the table name
Select COunt(*) INTO NumberoFColumns from
all_cons_columns
Where CONSTRAINT_NAME = l_const_name;
-- FOR C11 IN c_columns(l_const_name) LOOP
-- End loop;
-- UTL_FILE.PUT(id, ');');
ELSE EXIT;
END if; --THe 1 st cursor if is closed
End Loop; --The table loop ends here
DBMS_SQL.CLOSE_CURSOR(cursor_name);
-- END IF;
-- UTL_FILE.PUT_LINE(id,name);
-- UTL_FILE.PUT(id,'It worked and wrote to this file');
UTL_FILE.FCLOSE(id);
DBMS_OUTPUT.PUT_LINE('Successful write to file');
EXCEPTION
WHEN OTHERS THEN
err := SQLERRM;
num := SQLCODE;
DBMS_OUTPUT.PUT_LINE(err);
DBMS_OUTPUT.PUT_LINE(num);
DBMS_OUTPUT.PUT_LINE('Error in writing to file');
END write_index; -
How to call text file using Script in Data Integrator
Dear All,
Can any one assit me in how to call a text file using script with the help of Data Integrator.
and one question ?
M having 32 csv files i want to club thos 32 csv files into one table with the help of Data Integrator, can
any one assist me.mary,
since you knew the file name ,when clicked in name send to server,read the file and write to servlet outputstream.
I think this would help you.
If anything wrong in mycode ..forums will help you further
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
int bytesRead=0;
byte buff[]=new byte[1024];
File f=new File(test.txt);
try{
bis= new BufferedInputStream(new FileInputStream(f));
bytesRead=bis.read(buff,0,buff.length);
if(bytesRead!=-1){
// create a BufferedOutputStream from ServletOutputStream
bos=new BufferedInputStream(response.getOutputStream());
do{
bos.write(buff,0,bytesRead);
}while((bytesRead=bis.read(buff,0,buff.length))!=-1)
}catch(Exception e){
////error handling
} -
How to write data to existing file using spool command?
Hi,
I am calling a stored procedure from a shell script. This stored procedure is having a CLOB object as an OUT parameter. How to write the data in CLOB object a existing file which is there in my client system. Below is the shell and sql scripts.
Shell script
( echo "hello" ) > /root/file.txt
sqlplus -s $user/$pass@$tns @/root/proc.sql /root/file.txt << EOF
EOFSQL script
set pages 0
set trimspool off
set serveroutput off
set feedback off
set term off
set echo off
variable out CLOB
define file='&1'
begin
pack.proc(:out);
end;
spool &file
select :out from dual;
spool offThis code writes contents of the OUT variable to file.txt, but my existing data ('hello') is lost. Please help me.
I figured the it...Use append in the spool command... :)
Edited by: Balaji on Jul 19, 2012 8:55 PM>
Hi Balaji
I figured the it...Use append in the spool command... :Please mark the thread as answered as per the FAQ.
Paul... -
How to generate XML file using select files manually option in MOPz
Hi Guru's!
Can someone tell me how can I generate XML file not using the calculate automatically option but using select files manually. thanks!Hi Maix,
You may manually add the files into download basket and then approve it.
Use the SCN document below for the steps.
http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/10b14f0c-b9af-2e10-ba83-f05ebcfbc3c2?QuickLink=index&…
Hope this helps.
Regards,
Deepak Kori -
How to generate .xsd file using jaxb generated java files
Hi,
We need to upgrade our applicatio to jdk1.6 which has jaxb2.0 class files in it. Our application has java files generated from .xsd using jaxb1.0.2 version. At present we dont have .xsd or .xml file with us.
We decide to generate .xsd file by using jaxb1.0.2 generated java files. I tried using schemagen.exe given by jdk1.6 to generate .xsd file. It error out. Is there any other way to generate .xsd file ?
pls let me know.
thanks,
ThiruObject-XML mapping is a new feature in JAXB 2.0. Classes generated with JAXB 1.0 won't generate a schema.
Generate Java classes with JAXB 2.0 xjc.
http://www.theregister.co.uk/2006/09/22/jaxb2_guide/ -
How to generate text file on a condition
I want to generate a text file if following count is > 1 on a Stored procedure. Your help is much appreicated.
select count(p.policy_no)
from policy p,
risk r
where upper(trim(p.policy_no)) not like 'Q%'IF COUNTER > 1
THEN
UTL_FILE
END IF -
How to generate "CertificateSigningRequest" by using shell command
Hi,
I want to create "CertificateSigningRequest" file by using shell command
We can create this csr file by using keychain.
Keychain Access/Certificate Assistant/Request a certification from a Certificate Authroity
I really need your help.
Thanks
MengHi,
Thanks for your message.
Yes, it is self-signed Certificate Authrity.
If it is very easy and free, please let me know.
Thanks
Meng -
How to generate .pdx file using File Adapter and FTP Adapter
Hi,
is it possible to genearate .pdx file using oracle ESB Adapters(File Adapter and FTP adapter).
Can you anyone guide me.
Thanks,
Raj.hi,
regarding report, there is a dedicated report forum. you should post there.
but answer for your question is form is nothing to do with the pdf generation. you can call the report from the as usual.
In the report you should set the properties like
destype to 'file'
desname to 'path with file name'
desformat to 'pdf' -
How to generate class files using XMLBeans
I had used XMLBeans scomp command to generate document classes from XSD.But problem is i got xsb files and java files, not class files.
I want to use jar of these generated files in my project , so i need to have class files.
Can anyone help me in overcoimg this problem...well i got it...
i need to use this cmd.....
scomp -out sortie.jar -debug -compiler C:\jdk1.5.0_04\bin\javac b.xsd
as i was getting
java.io.IOException: CreateProcess: C:\Program @C:\DOCUME~1\CFERNA~1\LOCALS~1\Te
mp\javac18651 error=2 -
How to generate XML file using EJB ?
Hi
Cab anyone point me towards some good examples which creates xml file for the data extracted using EJBs .I have rough idea that this could be done with DOM/JDOm but need some goos example .
Also I have J2EE RI server,Xalan/Xerces ....Do I need to download DOM/JDOM APIs separately...
I am relatively new to J2EE .
Thanks
Ashhttp://www.theserverside.com/home/index.jsp
J2EE and XML Development PDF
Dave -
Generating Postscript file using (psl132.prt) and Text file in one go
Actually my requirement is to have soft copy , postscript files in a single shot for character mode report because the report fetches data from the table then deletes the data.
- I am able to generate softcopy, and postscript files same using distribution file for Bitmap report as shown below :-
[RWRUN REPORT=C:\Report\R_TEST.RDF userid = test / test @ test DISTRIBUTE=YES DESTINATION=C:\Report\bRep.dst ERRFILE=C:\Report\Final\report.err
The brep.dst file for bitmap report is as follows:-
DEST1: DESNAME=C:\Report\tpdf.pdf DESTYPE=File DESFORMAT=PDF COPIES=1 LEVEL=Report
DEST2: DESNAME=C:\Report\test.prn DESTYPE=File DESFORMAT=BITMAP COPIES=1 LEVEL=Report
But in case of Char mode report i am able to generate text file but i am unable to generate postscript file for using printer definition file. The commands used are as follows :-
RWRUN REPORT=C:\Report\R_TEST.RDF userid = test / test @ test DISTRIBUTE=YES MODE=CHARACTER DESTINATION=C:\Reports\cRep.dst ERRFILE=C:\Report\Final\report.err
The crep.dst file for char report is as follows :-
DEST1: DESNAME=C:\Report\tpdf.text DESTYPE=File DESFORMAT=ASCII COPIES=1 LEVEL=Report
DEST2: DESNAME=C:\Report\test.prn DESTYPE=File DESFORMAT=prt132.prn COPIES=1 LEVEL=Report (This option does not work is there any work around to make it work) ]
I am getting an error. it there any work around for this or there is any other solution.
Is there any other way i convert my text file to postscript file or viceversa...thanksHi,
Peter Gjelstrup wrote:
Next thing is how to use it:
declare
k_file constant varchar2(30) := 'YourFile.xls'
begin
create_file(k_file);
-- Call other procedures
close_file;
end;
Don't forget: these procedures are all part of the gen_xl_xml package.
To call them from outside the package, you have to prefix each procedure name with the package name, like this:
declare
k_file constant varchar2(30) := 'YourFile.xls'
begin
gen_xl_xml.create_file(k_file);
-- Call other procedures
gen_xl_xml.close_file;
end; -
Spool SQl data into text file using dynamic sql
Hi,
I am spooling output data into text file using command
select 'select t.mxname,bo.lxtype,t.mxrev'||chr(10)||'from mx_1234567'||chr(10)||
'where <condition>';
here mxname varchar(128),lxtype(128),mxrev(128) all are of varchar type.I want the output in format
e.g Part|1211121313|A
but due to column width the output,I am getting is with spaces.
"Part then blank spaces |1211121313 then blank spaces |A"
how can I remove these spaces between columns.I used set space 0 but not working.
Thanks in advance.
Your help will be appreciated.Hi Frank,
I have seen your reply for SET LINE SIZE function. But, I could not be able to understand it.
I am facing similar kind of issue in my present project.
I am trying spool more than 50 columns from a table into flat file. Because of more column lengths in few columns, i am getting space. There are so many columns with the same issue. I want to remove that space.so that, data can fit perfectly in one line in .txt file without any wrap text.
Below is my sample query.sql. Please let me know the syntax. My mail id : [email protected]
--Created : Sep 22,2008, Created By : Srinivasa Bojja
--Export all Fulfillments
--Scheduled daily after 1:00am and should complete before 3:30am
WHENEVER SQLERROR EXIT SQL.SQLCODE
SET LINESIZE 800
SET WRAP OFF
SET PAGESIZE 800
SET FEEDBACK OFF
SET HEADING ON
SET ECHO OFF
SET CONCAT OFF
SET COLSEP '|'
SET UNDERLINE OFF
SPOOL C:\Fulfillment.txt;
SELECT SRV.COMM_METHOD_CD AS Method,
SRV.SR_NUM AS "Fulfillment Row_Id",
CON.LAST_NAME AS "Filled By"
SRV.SR_TITLE AS Notes,
SRVXM.ATTRIB_04 AS "Form Description"
FROM SIEBEL.S_SRV_REQ SRV,
SIEBEL.S_SRV_REQ_XM SRVXM,
SIEBEL.S_USER USR,
SIEBEL.S_CONTACT CON
WHERE SRV.ROW_ID = SRVXM.PAR_ROW_ID AND
SRV.OWNER_EMP_ID = USR.ROW_ID AND
CON.ROW_ID= SRV.CST_CON_ID;
SPOOL OFF;
EXIT; -
Generating Text file from table using Shell script
I am using KSH for generating and FTPing a text file from a table.
While generating Text file I am not getting my Column names in orderly manner.
q2="select COLUMN1||' '||COLUMN2||' '||COLUMN3 from table1;"
set pagesize 0
set head off
set trimspool on
set trimout on
set colsep ' '
set linesize 1500
set trimspool on
spool /ss/app11/oastss/reports/$file2
select 'COLUMN1'||' '||'COLUMN2'||' '||'COLUMN3' from dual;
$q2
spool off;
EOF
I am getting the result some what like below in text file
COLUMN1 COLUMN2 COLUMN3
MALLIK_ACCT 17-SEP-11 908030482
MALLIK_ACCT 17-SEP-11 908266967
MALLIK_ACCT 17-SEP-11 909570766
I want the format like below
COLUMN1........ COLUMN2 .... COLUMN3
MALLIK_ACCT ...17-SEP-11 .... 908030482
MALLIK_ACCT ...17-SEP-11 .... 908266967
MALLIK_ACCT ...17-SEP-11 .... 909570766
I put dots(.) for illustration purpose.
column data length may icrease some times . it shoudl automatically adjust column and data so that they are in alignment. thanks in advance.Mallik wrote:
Hi my question is to format the headers so that they will be in alignment with column data and readable.So you want to output a query as a fixed width format data file? How about this (rather than using scripts)...
As sys user:
CREATE OR REPLACE DIRECTORY TEST_DIR AS '\tmp\myfiles'
GRANT READ, WRITE ON DIRECTORY TEST_DIR TO myuser
/As myuser:
CREATE OR REPLACE PROCEDURE run_query(p_sql IN VARCHAR2
,p_dir IN VARCHAR2
,p_header_file IN VARCHAR2
,p_data_file IN VARCHAR2 := NULL) IS
v_finaltxt VARCHAR2(4000);
v_v_val VARCHAR2(4000);
v_n_val NUMBER;
v_d_val DATE;
v_ret NUMBER;
c NUMBER;
d NUMBER;
col_cnt INTEGER;
f BOOLEAN;
rec_tab DBMS_SQL.DESC_TAB;
col_num NUMBER;
v_fh UTL_FILE.FILE_TYPE;
v_samefile BOOLEAN := (NVL(p_data_file,p_header_file) = p_header_file);
BEGIN
c := DBMS_SQL.OPEN_CURSOR;
DBMS_SQL.PARSE(c, p_sql, DBMS_SQL.NATIVE);
d := DBMS_SQL.EXECUTE(c);
DBMS_SQL.DESCRIBE_COLUMNS(c, col_cnt, rec_tab);
FOR j in 1..col_cnt
LOOP
CASE rec_tab(j).col_type
WHEN 1 THEN DBMS_SQL.DEFINE_COLUMN(c,j,v_v_val,2000);
WHEN 2 THEN DBMS_SQL.DEFINE_COLUMN(c,j,v_n_val);
WHEN 12 THEN DBMS_SQL.DEFINE_COLUMN(c,j,v_d_val);
ELSE
DBMS_SQL.DEFINE_COLUMN(c,j,v_v_val,2000);
END CASE;
END LOOP;
-- This part outputs the HEADER
v_fh := UTL_FILE.FOPEN(upper(p_dir),p_header_file,'w',32767);
FOR j in 1..col_cnt
LOOP
CASE rec_tab(j).col_type
WHEN 1 THEN v_finaltxt := v_finaltxt||rpad(lower(rec_tab(j).col_name),rec_tab(j).col_max_len,' ');
WHEN 2 THEN v_finaltxt := v_finaltxt||rpad(lower(rec_tab(j).col_name),rec_tab(j).col_max_len,' ');
WHEN 12 THEN v_finaltxt := v_finaltxt||rpad(lower(rec_tab(j).col_name),greatest(19,length(rec_tab(j).col_name)),' ');
END CASE;
END LOOP;
UTL_FILE.PUT_LINE(v_fh, v_finaltxt);
IF NOT v_samefile THEN
UTL_FILE.FCLOSE(v_fh);
END IF;
-- This part outputs the DATA
IF NOT v_samefile THEN
v_fh := UTL_FILE.FOPEN(upper(p_dir),p_data_file,'w',32767);
END IF;
LOOP
v_ret := DBMS_SQL.FETCH_ROWS(c);
EXIT WHEN v_ret = 0;
v_finaltxt := NULL;
FOR j in 1..col_cnt
LOOP
CASE rec_tab(j).col_type
WHEN 1 THEN DBMS_SQL.COLUMN_VALUE(c,j,v_v_val);
v_finaltxt := v_finaltxt||rpad(nvl(v_v_val,' '),rec_tab(j).col_max_len,' ');
WHEN 2 THEN DBMS_SQL.COLUMN_VALUE(c,j,v_n_val);
v_finaltxt := v_finaltxt||rpad(nvl(to_char(v_n_val,'fm99999999999999999999999999999999999999'),' '),rec_tab(j).col_max_len,' ');
WHEN 12 THEN DBMS_SQL.COLUMN_VALUE(c,j,v_d_val);
v_finaltxt := v_finaltxt||rpad(nvl(to_char(v_d_val,'DD/MM/YYYY HH24:MI:SS'),' '),greatest(19,length(rec_tab(j).col_name)),' ');
END CASE;
END LOOP;
UTL_FILE.PUT_LINE(v_fh, v_finaltxt);
END LOOP;
UTL_FILE.FCLOSE(v_fh);
DBMS_SQL.CLOSE_CURSOR(c);
END;This allows for the header row and the data to be written to seperate files if required.
e.g.
SQL> exec run_query('select * from emp','TEST_DIR','output.txt');
PL/SQL procedure successfully completed.Output.csv file contains:
empno ename job mgr hiredate sal comm deptno
7369 SMITH CLERK 7902 17/12/1980 00:00:00800 20
7499 ALLEN SALESMAN 7698 20/02/1981 00:00:001600 300 30
7521 WARD SALESMAN 7698 22/02/1981 00:00:001250 500 30
7566 JONES MANAGER 7839 02/04/1981 00:00:002975 20
7654 MARTIN SALESMAN 7698 28/09/1981 00:00:001250 1400 30
7698 BLAKE MANAGER 7839 01/05/1981 00:00:002850 30
7782 CLARK MANAGER 7839 09/06/1981 00:00:002450 10
7788 SCOTT ANALYST 7566 19/04/1987 00:00:003000 20
7839 KING PRESIDENT 17/11/1981 00:00:005000 10
7844 TURNER SALESMAN 7698 08/09/1981 00:00:001500 0 30
7876 ADAMS CLERK 7788 23/05/1987 00:00:001100 20
7900 JAMES CLERK 7698 03/12/1981 00:00:00950 30
7902 FORD ANALYST 7566 03/12/1981 00:00:003000 20
7934 MILLER CLERK 7782 23/01/1982 00:00:001300 10
The procedure allows for the header and data to go to seperate files if required. Just specifying the "header" filename will put the header and data in the one file.
Adapt to output different datatypes and styles are required (this is currently coded for VARCHAR2, NUMBER and DATE) -
How to display the out put of the sql query in a text file using forms
I want to display the out put of the sql query in a text file using forms 6.0.Same could be done using spool command in sqlplus but i want it using forms....Fiaz
Have a look at the text_io package:
http://www.oracle.com/webapps/online-help/forms/10g/state?navSetId=_&navId=3&vtTopicFile=f1_help/oraini/c_text_io.html&vtTopicId=
cheers
Maybe you are looking for
-
We have 2 iPhones, 1 iPad, and 2 iPod touches all set up using the same apple id. The iPods were christmas presents to our kids and we are having problems with the iMessages and Facetime from the iPods going to our phones. Do we need to reset their i
-
Fonts in some websites wont render
I got the interface fonts to render, and they seem to work fine, but some (a lot) of websites text wont render at all. The problem seems to be in the way it handles CSS, that if a font listed in font-family is not installed on the system, it just won
-
Dynamic Form? Is this possible?
Hello I am trying to create a application to store Biblography references so researchers dont forget who they are citing. It will be similar to an address book, but with different form fields depending on the type of reference. For the form where use
-
How do I remove the "Add Page on my screen?
how do I remove the "Add Page" on my screen?
-
Hi All, I have one servlet named as servletA in which i have init() method. In init() method i have source for connecting to a specific database. Other method in servletA is returning a Connection object. I have another servlet named as servletB, in