SPOOL or UTL_FILE
Hi all,
with the next script I spool an information into a file. Then I update all processed lines with UPDATE-Command.
set echo off
set verify off
set pagesize 0
set termout off
set heading off
set feedback off
spool c:\Temp\test_info.lst
select id||'|'||date||'|'||firname||'|'||secname||'|'||hiredate
||'|'||las
from test
where las is null
spool off
update test
set las = 'x'
commit;
set feedback on
set heading on
set termout on
set pagesize 24
set verify on
set echo on
Now I would like to however only examine, whether spool was successful, and only then to update. How can I do that?
Must I write a PL/SQL-Script with UTL_FILE-Package instead of a SQL*Plus-Script with SPOOL?
But UTL_FILE Package does not fit, because I do not want to permit
to the Client something write on the Server-machine. Or can I store the file on the Client with UTL_FILE Package?
Regards
Leonid
Hi Serge,
I would like updates then only if spool into the file something wrote.
And still another question: can I examine somehow whether the file exists? E.g. if I would write instead of c:\Temp c:\Temp1 (or if drive assembly is full), no file am provided, but the table is nevertheless changed.
Regadrs
Leonid
Similar Messages
-
Exporting text files in earlier editions of Oracle
I'm not familiar with Oracle, but hope someone might be able to help me out. I have filed an access to information request with a public agency for data that I'm told is in an Oracle database.
I have asked for the information to be released in flat text files but was told by the agency that they have an old edition (I'm not sure which one) and cannot export the information in that format as a result. As part of conditions for releasing any data to me, certain fields in their database will have to be severed to strip out personal information etc.
The agency says it can only provide the data in pdf, which I don't believe I will be able to use.
I'm wondering if this sounds like a valid argument - if there are earlier versions of Oracle that cannot export raw data in the format I requested. Or are there add-ons or aspects of each edition of Oracle that should make the severing and export possible?
Thanks for any help you're able to provide...Any version of Oracle going back at least into the 1980s has the ability to output text files while simultaneously stripping out confidential/personal data.
Not one of them is capable of producing that output as a .PDF file.
Keep in mind that the people you are talking to may not know enough about Oracle to spell it correctly. If they need help they can contact me.
Refer them to:
SPOOL and UTL_FILE. -
How to insert records into an Excel file
i was wondering if anyone knows how to insert records from a pl/sql block straight into an excel file
try with spool or UTL_FILE...
Re: CSV into Oracle and Oracle into CSV
SQL> spool c:\test1.csv
SQL> select substr(str,2,length(str)-3) from (select regexp_replace(column_value,'\s*<[^>]*>[^>]*>',',') str from table(xmlsequence(cursor(select * from test_emp))));
SUBSTR(STR,2,LENGTH(STR)-3)
9999,fredi's,CLERK,2345,10-OCT-06,1250,123,20
4567,STEWART,ANALYST,3456,02-APR-07,3200,215,30
2345,Cockrel,CLERK,7566,23-JAN-82,800,30
3 rows selected.
SQL> spool off; -
How to capture sound into an mp3 file
Hi all,
I have a program to capture sound into a wav file . and it's wrkng.
but in my site i have an mp3 player. can i capture the sound directly to an mp3 file?
I use classes like AudioSystem, TargetDataLine, AudioFormat etc.
For the file format i specify like
AudioFileFormat.Type.WAVE
like this can i do for mp3??
Please help me...
Thanx and Regards,
sand...try with spool or UTL_FILE...
Re: CSV into Oracle and Oracle into CSV
SQL> spool c:\test1.csv
SQL> select substr(str,2,length(str)-3) from (select regexp_replace(column_value,'\s*<[^>]*>[^>]*>',',') str from table(xmlsequence(cursor(select * from test_emp))));
SUBSTR(STR,2,LENGTH(STR)-3)
9999,fredi's,CLERK,2345,10-OCT-06,1250,123,20
4567,STEWART,ANALYST,3456,02-APR-07,3200,215,30
2345,Cockrel,CLERK,7566,23-JAN-82,800,30
3 rows selected.
SQL> spool off; -
Spooling large data using UTL_FILE
Hi Everybody!
While spooling out data into a file using UTL_FILE package I am unable spool the data The column data has a size of 2531 characters
The column 'source_where_clause_text' has very large data.
Its not giving any error but the external table is not returning and data
Following is the code.
CREATE OR REPLACE PROCEDURE transformation_utl_file AS
CURSOR c1 IS
select transformation_nme,source_where_clause_text
from utility.data_transformation where transformation_nme='product_closing';
v_fh UTL_FILE.file_type;
BEGIN
v_fh := UTL_FILE.fopen('UTLFILELOAD', 'transformation_data.dat', 'w', 32000);--132767
FOR ci IN c1
LOOP
UTL_FILE.put_line( v_fh, ci.transformation_nme ||'~'|| ci.source_where_clause_text);
-- UTL_FILE.put_line( v_fh, ci.system_id ||'~'||ci.system_nme ||'~'|| ci.system_desc ||'~'|| ci.date_stamp);
END LOOP;
UTL_FILE.fclose( v_fh );
exception
when utl_file.invalid_path then dbms_output.put_line('Invalid Path');
END;
select length(
'(select to_char(b.system_id) || to_date(a.period_start_date,''dd-mon-yyyy'') view_key, b.system_id, to_date(a.period_start_date,''dd-mon-yyyy'') period_start_date, to_date(a.period_end_date,''dd-mon-yyyy'') period_end_date, to_date(a.clos
ing_date,''dd-mon-yyyy'') closing_date from ((select decode(certification_type_code, ''A'', ''IDESK_PRODUCTS_PIPELINE'',''C'', ''IDESK_PRODUCTS_COMMITMENT_LINKAGE'') system_nme, to_char(to_date(''01'' || lpad(trim(to_char(certification_as_of_month_yr)),6,''0''),''ddmmyy
yy''),''dd-mon-yyyy'') period_start_date, to_char(last_day(to_date(''12'' || lpad(trim(to_char(certification_as_of_month_yr)),6,''0''),''ddmmyyyy'')),''dd-mon-yyyy'') period_end_date, to_char(trunc(certification_datetime_stamp), ''dd-mon-yyyy'') closing_date from odsu
pload.prod_monthly_certification where certification_type_code in (''A'',''C'') minus select trim(system_nme), to_char(period_start_date, ''dd-mon-yyyy''), to_char(period_end_date, ''dd-mon-yyyy''), to_char(closing_date, ''dd-mon-yyyy'') from utility.system_closing
statusv where system_nme in (''IDESK_PRODUCTS_PIPELINE'', ''IDESK_PRODUCTS_COMMITMENT_LINKAGE'')) union all (select ''BMS Commitment Link'' system_nme, to_char(to_date(''01'' || lpad(trim(to_char(certification_as_of_month_yr)),6,''0''),''ddmmyyyy''),''dd-mon-yyyy'')
period_start_date, to_char(last_day(to_date(''12'' || lpad(trim(to_char(certification_as_of_month_yr)),6,''0''),''ddmmyyyy'')),''dd-mon-yyyy'') period_end_date, to_char(trunc(certification_datetime_stamp), ''dd-mon-yyyy'') closing_date from odsupload.prod_monthly_c
ertification where certification_type_code = ''C'' minus select trim(system_nme), to_char(period_start_date, ''dd-mon-yyyy''), to_char(period_end_date, ''dd-mon-yyyy''), to_char(closing_date, ''dd-mon-yyyy'') from utility.system_closing_status_v where system_nme
= ''BMS Commitment Link'') union all (select ''BMS'' system_nme, to_char(to_date(''01'' || lpad(trim(to_char(certification_as_of_month_yr)),6,''0''),''ddmmyyyy''),''dd-mon-yyyy'') period_start_date, to_char(last_day(to_date(''12'' || lpad(trim(to_char(certification_as_
of_month_yr)),6,''0''),''ddmmyyyy'')),''dd-mon-yyyy'') period_end_date, to_char(trunc(certification_datetime_stamp), ''dd-mon-yyyy'') closing_date from odsupload.prod_monthly_certification where certification_type_code = ''A'' minus select trim(system_nme), to_char
(period_start_date, ''dd-mon-yyyy''), to_char(period_end_date, ''dd-mon-yyyy''), to_char(closing_date, ''dd-mon-yyyy'') from utility.system_closing_status_v where system_nme = ''BMS'')) a, utility.system_v b where a.system_nme = b.system_nme)') length1
from dual
--2531
begin
SSUBRAMANIAN.transformation_utl_file;
end;
create table transformation_utl
TRANSFORMATION_NME VARCHAR2(40),
SOURCE_WHERE_CLAUSE_TEXT VARCHAR2(4000)
ORGANIZATION external
type oracle_loader
default directory UTLFILELOAD
ACCESS PARAMETERS
records delimited by newline CHARACTERSET US7ASCII
BADFILE UTLFILELOAD:'transformation.bad'
LOGFILE UTLFILELOAD:'transformation.log'
fields TERMINATED by "~"
LOCATION ('transformation_data.dat')
) REJECT LIMIT UNLIMITED
select * from transformation_utlafter running the procedure, did you verify that the file 'transformation_data.dat' has data? open it, make sure it's correct. maybe it has no data, and that's why the external table doesn't show anything.
also, check the LOG and BAD files after selecting from the external table. maybe they have ERRORS in them (or all the data is going to BAD because you defined something wrong). -
Hey SAPians,
i am stuck here with an issue. i am trying to print a report using spool in Russian, its status ends in error. however when i try printing same in english its status is complete.
where am i going wrong, how do i make this work?
THANKS IN ADVANCE
AbhinavHi,
Why do you want to do
SELECT *
FROM table_name;
inside the procedure? Why not have your script run the procedure (without the query above), and then do that query afterwards, when you can use SPOOL to put the results in a file?
If you really must write a file from a procedure, here's an example of how to use utl_file.
First, SYSTEM (or some other user with the CREATE ANY DIRECCTORY system privilege) must create a directory, and grant privileges to use it. For example:
CREATE OR RPLEACE DIRECTORY a_dir AS 'C:\Foo\A_Folder';
GRANT ALL ON DIRECTORY a_dir TO PUBLIC;
Typically, SYSTEM does this once, and other people use it hundreds of times.
You might create a file in that directory like this:
CREATE OR REPLACE PROCEDURE write_dept
IS
f utl_file.file_type;
BEGIN
f := utl_file.fopen ('A_DIR', 'Dept.txt', 'W');
FOR r IN ( SELECT *
FROM scott.dept
LOOP
utl_file.put_line ( f
, TO_CHAR (r.deptno, 'FM00')
|| ' '
|| RPAD (r.dname, 15)
|| r.loc
END LOOP;
utl_file.fclose (f);
END write_dept;
SHOW ERRORS -
Problem with UTL_FILE (please see my last post on this thread)
Hi all,
I'm trying to get the code (procedures, functions, etc) of my schemas. I've tried it using DBMS_METADATA.GET_DDL but it fails with many objects. Finally, I'm trying to create a procedure to extract the code.
I've created this two procedures:
CREATE OR REPLACE PROCEDURE spool_code (code IN varchar2, propi IN varchar2) is
CURSOR codigo is
select text from dba_source where name = code and owner = propi order by line;
line varchar2(4000);
BEGIN
open codigo;
loop
fetch codigo into line;
exit when codigo%notfound;
dbms_output.put_line(line);
end loop
close;
END;
CREATE OR REPLACE PROCEDURE ext_codigo is
CURSOR objeto is
select object_name, owner from dba_objects where object_type in ('PROCEDURE','FUNCTION','PACKAGE')
and owner not in ('OUTLN','DBSNMP','SYSTEM','SYS','REPADMIN','PERFSTAT','SPOTLIGHT','MONITOR','PRUEBAS','TOAD')
and status='VALID';
nom varchar2(128);
owner varchar2(30);
BEGIN
open objeto;
loop
fetch objeto into nom, owner;
exit when objeto%notfound;
spool_code(nom, owner);
end loop;
close objeto;
END;
And I'm calling from sqlplus to spool it:
SQL> spool Users_code.sql
SQL> exec ext_codigo;
But it don't bring me results...
where is the problem??
Thanks in advance for your support!
dbajug
Edited by: dbajug on Aug 29, 2012 6:36 AMHi,
yes guys, I've set serverout on using the max limit but, always fails with:
ERROR at line 1:
ORA-20000: ORU-10027: buffer overflow, limit of 1000000 bytes
ORA-06512: at "SYS.DBMS_OUTPUT", line 35
ORA-06512: at "SYS.DBMS_OUTPUT", line 198
ORA-06512: at "SYS.DBMS_OUTPUT", line 139
ORA-06512: at "SYS.SPOOL_CODE", line 15
ORA-06512: at "SYS.EXT_CODIGO", line 17
ORA-06512: at line 1
I'm working with a 9i version trying to extract the code to migrate it to a 11g.
In order to avoid the buffer error, I've decide use the UTL_FILE package but I'm having another problem: my procedure now is this
CREATE OR REPLACE PROCEDURE spool_code (code IN varchar2, propi IN varchar2) is
CURSOR codigo is
select text from dba_source where name = code and owner = propi order by line;
line varchar2(4000);
out_file UTL_FILE.File_Type;
BEGIN
begin
out_file := UTL_FILE.Fopen('/export/home/oracle', 'Users_code.sql', 'w');
exception
when others then
dbms_output.put_line('Error opening file');
end;
open codigo;
loop
fetch codigo into line;
exit when codigo%notfound;
UTL_FILE.Put_Line(out_file, line);
end loop;
close codigo;
UTL_FILE.Fclose(out_file);
END;
The directory exists and the file too but fails with this error:
ERROR at line 1:
**ORA-29282: invalid file ID**
ORA-06512: at "SYS.UTL_FILE", line 714
ORA-06512: at "SYS.SPOOL_CODE", line 23
ORA-06512: at "SYS.EXT_CODIGO", line 17
ORA-06512: at line 1
any idea? about the reason? The file is a text file on the server:
ls -lrt /export/home/oracle/Users_code.sql
-rw-rw-r-- 1 oracle dba 0 Aug 29 14:43 /export/home/oracle/Users_code.sql
best regards,
dbajug -
Producing text file from PL/SQL procedure (UTL_FILE)?
Hi,
I need to produce a text file from a PL/SQL procedure. My output should be a little over 38K records and my query has 70 fields. I'd like to separate the fields by pipe delimiters in the text file. I can either concatenate all 70 fields into one variable for each record or send all 70 fields over with a pipe, whichever is easier. It looks like I can't spool within PL/SQL but UTL_FILE might be an option from what I'm reading. Does anyone have any good, simple example(s) to produce a text file using this package in calling multiple fields from a LOOP? Our os is UNIX and db is Oracle 9i.
Also, I've read that UTL_FILE has a record output of 1,023 bytes. Is this for each record line or the whole file size? I belive the max # of characters for each line wouldn't exceed 500 characters. The 38K rows I have for the data set would be well over 1MB. If this is the case, is there a work around to produce a larger text file?
Any suggestions and/or examples would be greatly appreciated.
Thanks,
EricAssuming the goal is to create a text file on the database server, you can use UTL_FILE here. Each line can be 1023 characters, but you can have as many lines as you'd like.
Tom Kyte has an example here
http://asktom.oracle.com/pls/ask/f?p=4950:8:::::F4950_P8_DISPLAYID:235814350980
Justin
Distributed Database Consulting, Inc.
http://www.ddbcinc.com/askDDBC -
Preserve a single space at the end of line using spool command
Hi,
Can you please help me to write the result of an sql query into a file with the last column of the row ending with a single space?
For example:
COL1DATA|COL2DATA|COL3DATA<space1>
As mentioned in the example the col3 value in the file should end with a single space. And there should not be any delimiters at the end of the row.
I tried with set trimspool on/off. But, it didn't work. When I say trimspool on - it is trimming all the trailing spaces.
When I say trimspool off - it is retaining all the trailing spaces to the size of the line.
But, I do not wish to modify the file through shell commands once it is written thru spool. I mean I do not wish to append spaces to the end of a line using shell script or any other method.
I do not wish to use other methods like UTL_FILE also.
Please help me how to do it using spool command?
Thank you.
RamanaMy requirement is that all the trailing spaces should be truncated except the last one in the row.Why?
As you have discovered a single column in sqlplus, is always a fixed length regardless of the size of the data, if the length of the data varies the output is padded to the maximum or line size with spaces. The trim and trimpsool commands are there to remove all the spaces from the end of a line if there are any. There are no commands to trim all the spaces except one, or even to trim all the spaces except two, or three even.
If you want such custom processing you should post process the file in the OS using sed, awk or perl or something designed for such things. -
Help needed in spooling a file
hello gurus ,
i have a table emp
consist of values like
name place phno
abcd ksla 123
errd lkis 456
ertw asd 324
reqq lsd 432
then i did was
spool d:/file1/empdetails.xls
select * from emp
then the file got spooled
but the output was not exactly wat i needed
the values were spooled in a single column in ms-excel (name ,place ,phno all in one column )
i needed the spooled output to be seperate in three columns like name in column1 and place in another column like that
so can u guide me to achieve the result
regards,
friendThat's because Excel (.xls) files are a proprietary Microsoft format, and are not compatible with just simply spooled output from and SQL*Plus output of a select command.
You could look at the forum FAQ: {message:id=9360007} as this question is asked over and over again.
Or you could do something like the following to generate a CSV files that Excel can understand...
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
v_finaltxt := ltrim(v_finaltxt||','||lower(rec_tab(j).col_name),',');
END LOOP;
-- DBMS_OUTPUT.PUT_LINE(v_finaltxt);
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 := ltrim(v_finaltxt||',"'||v_v_val||'"',',');
WHEN 2 THEN DBMS_SQL.COLUMN_VALUE(c,j,v_n_val);
v_finaltxt := ltrim(v_finaltxt||','||v_n_val,',');
WHEN 12 THEN DBMS_SQL.COLUMN_VALUE(c,j,v_d_val);
v_finaltxt := ltrim(v_finaltxt||','||to_char(v_d_val,'DD/MM/YYYY HH24:MI:SS'),',');
ELSE
v_finaltxt := ltrim(v_finaltxt||',"'||v_v_val||'"',',');
END CASE;
END LOOP;
-- DBMS_OUTPUT.PUT_LINE(v_finaltxt);
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.txt file contains:
empno,ename,job,mgr,hiredate,sal,comm,deptno
7369,"SMITH","CLERK",7902,17/12/1980 00:00:00,800,,20
7499,"ALLEN","SALESMAN",7698,20/02/1981 00:00:00,1600,300,30
7521,"WARD","SALESMAN",7698,22/02/1981 00:00:00,1250,500,30
7566,"JONES","MANAGER",7839,02/04/1981 00:00:00,2975,,20
7654,"MARTIN","SALESMAN",7698,28/09/1981 00:00:00,1250,1400,30
7698,"BLAKE","MANAGER",7839,01/05/1981 00:00:00,2850,,30
7782,"CLARK","MANAGER",7839,09/06/1981 00:00:00,2450,,10
7788,"SCOTT","ANALYST",7566,19/04/1987 00:00:00,3000,,20
7839,"KING","PRESIDENT",,17/11/1981 00:00:00,5000,,10
7844,"TURNER","SALESMAN",7698,08/09/1981 00:00:00,1500,0,30
7876,"ADAMS","CLERK",7788,23/05/1987 00:00:00,1100,,20
7900,"JAMES","CLERK",7698,03/12/1981 00:00:00,950,,30
7902,"FORD","ANALYST",7566,03/12/1981 00:00:00,3000,,20
7934,"MILLER","CLERK",7782,23/01/1982 00:00:00,1300,,10The 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. -
Spooling from PL/SQL Block
Hi,
Is it possible to do spooling from a pl/sql block.The spool cannot be the first command because I want to give the spool file name in a loop and each time the spool name should change based on the value.
I tried all possiblities,but I couldn't can anyone give me some idea on how to do this.
I cannot use UTL_FILE option ,because the client agreed only for spooling.
Is it possible to write the spool data is excel format..
Please find below an example of my requirement..
DECLARE
CurSor Cur_Designation IS
Select Designation from tbl_Designation;
N_Desig Cur_Designation%RowType;
Cursor Cur_Emp(Desig Number) IS
Select Emp_no,Emp_name
From Employee Where Emp_Designation = Desig;
N_Emp Cur_Emp%RowType;
BEGIN
Open Cur_Designation ;
Loop
Fetch Cur_Designation INTO N_Desig;
EXIT WHEN Cur_Designation %NOTFOUND;
--Here I want to give the spool file name
Spool --Should be designation name
Open Cur_Emp(N_Desig.Designation);
Loop
Fetch Cur_Emp INTO N_Emp ;
EXIT WHEN Cur_Emp %NOTFOUND;
-- All employee details I need to come to log file
End Loop;
Close Cur_Emp;
End Loop;
Close Cur_Desingnation;
End;
Please advice me how to do this..
Thanks,
Bindu<< I cannot use UTL_FILE option ,because the client agreed only for spooling. >>
<< I want to use it in a PL/SQL Block.I have some variables to declare and some cursors to be used.
So where should I use the spool command.
I tried to given after a fetch command ,but it is not working. >>
you can't use SPOOL inside PLSQL
you may use dbms_output calls in PLSQL
so create a procedure test2 instead of sqlscript test2 and you will be fine
rem =========== procedure test2 ================
create or replace
procedure test2 (p_param number) is
begin
dbms_output.enable (1000000);
dbms_output.put_line('A' || chr(9) || 'B' || chr(9) || 'C' ) ;
FOR i_rec in (select a,b.c from xyz where a = p_param) LOOP
dbms_output.put_line (to_char(a) || chr(9) || b || chr(9) || c ) ;
END LOOP ;
end ;
REM ==================================================== -
How to spool for a file inside a procedure
Hi,
I have a Procedure. Now that procedure writes some values into a file in /tmp directory (in Unix). Now there is a server program that reads this file does some manipulations and writest the output to a particular fiel(this file not the same as input file) in the same directory. My PL/SQL code or procedure has to keep looking for this file (output file generated by server program). How can I spool for a file in PL/SQL? Is it possible? Could you please put some light on this as of how to do it?
Thanks in advanceHi,
I didnt get any exact way of spooling for the file. I tried to do this program for reading lines from a file using GET_LINE
create or replace procedure film as
log varchar2(300);
logfile UTL_FILE.FILE_TYPE;
begin
log := 'check_log';
logfile := utl_file.fopen('/tmp','check_log','R');
utl_file.get_line(logfile,log);
utl_file.fclose(logfile);
end;
show errors;
It gives me this error.BEGIN film; END;
ERROR at line 1:
ORA-06510: PL/SQL: unhandled user-defined exception
ORA-06512: at "SYS.UTL_FILE", line 101
ORA-06512: at "SYS.UTL_FILE", line 157
ORA-06512: at "DACSCAN.FILM", line 6
ORA-06512: at line 1
Could you please tell me where I am going wrong.
Thanks in advance -
Oracle 11g : UTL_FILE query
Hi,
I am having 2 server. One server has oracle database while on another one is application server where all the data files and application specific shell scripts are kept. We are connecting to database via the application server.
I am using Oracle 11g:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - 64bit Production
PL/SQL Release 11.1.0.6.0 - Production
CORE 11.1.0.6.0 Production
TNS for HPUX: Version 11.1.0.6.0 - Production
NLSRTL Version 11.1.0.6.0 - Production
I know that using UTL_FILE in PL/SQL procedure we can create file on the server (where oracle is installed).
My requirement is generated some report which will involve writing to more than 2 files simultaneously. I know I can easily do this in PL/SQL procedure.
But my issue is that due to space issue I cannot generate the files on database server and need to generate the same on the application one (client side)
Queries:
1] Is it also possible to create file on client machince instead of server in my case the application server via PL/SQL because I read that in Oracle 11g
UTL_FILE provides file access both on the client side and on the server side.
2] If yes any idea, how is this possible?
3] If not, is there any way by which I can write to 2 or more file simultaneously on the client using oralce. I know SQL*Plus can spool file on client. But it is only one file at a time.
Rgds,
AmolLooking at the 11g documentation you are probably referring to:
http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28419/u_file.htm#BABDEJDH
where it says...
>
UTL_FILE provides file access both on the client side and on the server side. When run on the server, UTL_FILE provides access to all operating system files that are accessible from the server. On the client side, as in the case for Forms applications, UTL_FILE provides access to operating system files that are accessible from the client.
>
This is referring to client installed forms applications where it provides a local UTL_FILE functionality that accesses the local file system. It doesn't mean that UTL_FILE within PL/SQL that is running on the database server can access the client machine's file system as this would also be a breach of network security. -
Hi,
I've a package with 6 procedures.
In one procedure I need to load the data into a flat file. The procedure contains only one select statement.
example:
procedure pro_test as
select * from emp;
end pro_test;
Shell script is being used to call one procedure inside the package and that procedure is calling remaining procedures inside it.
connected to database using sqlplus.
Can anyone help me out how I can spool the select statement inside the procedure. Version of oracle is 11g.Hi,
Why do you want to do
SELECT *
FROM table_name;
inside the procedure? Why not have your script run the procedure (without the query above), and then do that query afterwards, when you can use SPOOL to put the results in a file?
If you really must write a file from a procedure, here's an example of how to use utl_file.
First, SYSTEM (or some other user with the CREATE ANY DIRECCTORY system privilege) must create a directory, and grant privileges to use it. For example:
CREATE OR RPLEACE DIRECTORY a_dir AS 'C:\Foo\A_Folder';
GRANT ALL ON DIRECTORY a_dir TO PUBLIC;
Typically, SYSTEM does this once, and other people use it hundreds of times.
You might create a file in that directory like this:
CREATE OR REPLACE PROCEDURE write_dept
IS
f utl_file.file_type;
BEGIN
f := utl_file.fopen ('A_DIR', 'Dept.txt', 'W');
FOR r IN ( SELECT *
FROM scott.dept
LOOP
utl_file.put_line ( f
, TO_CHAR (r.deptno, 'FM00')
|| ' '
|| RPAD (r.dname, 15)
|| r.loc
END LOOP;
utl_file.fclose (f);
END write_dept;
SHOW ERRORS -
Utl_file help!!!
Hey people :) Below is my code it is produces a list of names address for a mail merge, an example of an outputted file:
32-36 GREAT VICTORIA STREET
BELFAST
BT2 7BA
08448920902
0026936
000001
MRS FRAME
51 HOUSE DRIVE
HOUSETOWN
COUNTY ANTRIM
BT36 7JY
03 DEC 2008
MRS FRAME
0.35
28-JUL-08
07-DEC-08
01-APR-08
07-DEC-08
0.72
ALAN CASKEY
Which is perfect, but when some users input the data, tehy leave the l.INPUT_HOUSE_NO as null and input the house number as part of the l.input_address_line1, which leaves a blank space where the house number should be for example:
32-36 GREAT VICTORIA STREET
BELFAST
BT2 7BA
08448920902
0031955
000002
MR TOWNSLEY
__2 HOUSE GARDENS (unneeded space in the address, how could i fix this??)
HOUSETOWN
BT13 3RJ
03 DEC 2008
MR TOWNSLEY
0.04
01-APR-08
23-NOV-08
01-APR-08
23-NOV-08
0.60
ALAN CASKEY
Thanks
DECLARE
v_count NUMBER(10) := 0;
wk_start_seqno date;
EXEC_file UTL_FILE.FILE_TYPE;
CURSOR read_lpa_input
IS
SELECT
a.address_line3,
a.address_line4,
a.address_line5,
a.post_code,
a.tel_no,
l.claim_no,
lpad(row_number() OVER(ORDER BY l.claim_no), 6, '0') AS snum,
l.input_title,
l.input_surname,
l.INPUT_HOUSE_NO,
l.input_address_line1,
l.input_address_line2,
l.input_address_line3,
l.input_address_line4,
l.input_post_code,
to_char(sysdate, 'DD MON YYYY') AS prntdate,
l.input_title AS input_title2,
l.input_surname AS input_surname2,
h.lpa_amt,
h.start_date,
c.paidto_date,
h.start_date AS start_date2,
c.paidto_date AS paidto_date2,
c.overpay_halahra,
a.sho_name
FROM lpa_input l, lpa_address a, lpa_history h, lpa_claim c
WHERE a.aun_code = l.input_aun_code
AND h.claim_no = c.claim_no
AND l.claim_no = h.claim_no
AND c.overpay_halahra > '0'
and ent_seqno = (select max (ent_seqno) from lpa_history where claim_no = c.claim_no);
TYPE wk_tab IS TABLE OF read_lpa_input%ROWTYPE
INDEX BY PLS_INTEGER;
wk wk_tab;
BEGIN
exec_file := utl_file.fopen('/spp/spool/RBLIVE/rr_output', 'output501.txt', 'W');
OPEN read_lpa_input;
LOOP
EXIT WHEN read_lpa_input%NOTFOUND;
FETCH read_lpa_input
BULK COLLECT INTO wk LIMIT 100;
FOR i IN 1 .. wk.count
LOOP
select start_date into wk_start_seqno from lpa_history where claim_no = wk(i).claim_no
and ent_seqno = (select max (ent_seqno) - 1 from lpa_history
where claim_no = wk(i).claim_no);
v_count :=0;
utl_file.put_line(exec_file, wk(i).address_line3);
utl_file.put_line(exec_file, wk(i).address_line4);
IF wk(i).address_line5 IS NOT NULL THEN
utl_file.put_line(exec_file, wk(i).address_line5);
ELSE
v_count := v_count+1; -- count the number of blank lines
END IF;
utl_file.put_line(exec_file, wk(i).post_code);
IF v_count > 0 THEN -- we have omitted some lines
FOR i IN 1..v_count LOOP -- loop that many times
utl_file.new_line(exec_file); -- write a blank line
END LOOP;
END IF;
utl_file.put_line(exec_file, wk(i).tel_no);
utl_file.put_line(exec_file, wk(i).claim_no);
utl_file.put_line(exec_file, wk(i).snum);
utl_file.put_line(exec_file, wk(i).input_title||' '||wk(i).input_surname);
v_count :=0;
---------------------------------------------------------------------------------HELP HERE!!!!
IF wk(i).input_address_line1 IS NOT NULL THEN
utl_file.put_line(exec_file, wk(i).INPUT_HOUSE_NO||' '||wk(i).input_address_line1);
ELSE
v_count := v_count+1; -- count the number of blank lines
END IF;
IF wk(i).input_address_line2 IS NOT NULL THEN
utl_file.put_line(exec_file, wk(i).input_address_line2);
ELSE
v_count := v_count+1; -- count the number of blank lines
END IF;
IF wk(i).input_address_line3 IS NOT NULL THEN
utl_file.put_line(exec_file, wk(i).input_address_line3);
ELSE
v_count := v_count+1; -- count the number of blank lines
END IF;
IF wk(i).input_address_line4 IS NOT NULL THEN
utl_file.put_line(exec_file, wk(i).input_address_line4);
ELSE
v_count := v_count+1; -- count the number of blank lines
END IF;
utl_file.put_line(exec_file, wk(i).input_post_code);
IF v_count > 0 THEN -- we have omitted some lines
FOR i IN 1..v_count LOOP -- loop that many times
utl_file.new_line(exec_file); -- write a blank line
END LOOP;
END IF;
utl_file.put_line(exec_file, wk(i).prntdate);
utl_file.put_line(exec_file, wk(i).input_title2||' '||wk(i).input_surname2);
utl_file.put_line(exec_file, to_char(round(wk(i).lpa_amt,2),'99G990D00'));
utl_file.put_line(exec_file, wk(i).start_date);
utl_file.put_line(exec_file, wk(i).paidto_date);
utl_file.put_line(exec_file, wk_start_seqno);
utl_file.put_line(exec_file, wk(i).paidto_date2);
utl_file.put_line(exec_file, to_char(round(wk(i).overpay_halahra,2),'99G990D00'));
utl_file.put_line(exec_file, wk(i).sho_name);
END LOOP;
END LOOP;
CLOSE read_lpa_input;
utl_file.fclose(exec_file);
END;
/Edited by: user653315 on 03-Dec-2008 04:11Try this...
IF wk(i).input_address_line1 IS NOT NULL THEN
utl_file.put_line(exec_file, CASE WHEN wk(i).INPUT_HOUSE_NO IS NULL THEN NULL ELSE wk(i).INPUT_HOUSE_NO||' ' END||wk(i).input_address_line1);
ELSE
v_count := v_count+1; -- count the number of blank lines
END IF;
Maybe you are looking for
-
Memory Problems with Adobe PDF iFilter for 64-bit
In preparation to rebuild my Windows Search Index, I installed the Adobe PDF iFilter for 64-bit on my system (Vista Business 64). When I finally rebuilt the index, I wasn't too surprised by what I saw happen, namely, the SearchFilter.exe process wou
-
How to install Photoshop cc on a new computer as the main when it is already on another?
Buying a new computer and I want to make it the main one for Photoshop cc instead of the older computer it is already on. What is the process for that?
-
I need to establish an extensive report in Lab View but should not use MS Office. With standard report vi's it seems to be very hard to create such a report (contains text, tables and a lot of lists). Is there an easy way to generate an extensice rep
-
Xslt ecc6 ISO-8859-1 problem when download xml file
Hello, i create an ABAP test program: *& Report Z_ABAP_TO_XML * *& Write the data from an internal ABAP table into an XML document, * *& and write it onto your frontend computer * RE
-
How to import my photos to Photos libray with the same folder name?
how to import my photos to Photos libray with the same folder name?