Creating a text file using pl/sql
how can one create a text file using pl/sql?
Say for example abcd.txt in the d: drive
(d:\abcd.txt)
on a windows nt server
<BLOCKQUOTE><font size="1" face="Verdana, Arial">quote:</font><HR>Originally posted by bhrigu nagal ([email protected]):
thanks for your replies
I had already set the utl_file_dir parameter
I tried the utl package and wrote the following code but got some errors
declare
filehandler UTL_FILE.FILE_TYPE;
begin
filehandler:=UTL_FILE.fopen('d:/intel/','myfile.txt','w');
utl_file.put_line(filehandler,'look masjdfgsd\n');
utl_file.fclose(filehandler);
exception when utl_file.invalid_path then
raise_application_error(-20023,'bhrigu ,chk the path');
end;
the errors
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 line 4
if u know what this is please tell me
thanks bhrigu
<HR></BLOCKQUOTE>
U need :
1) To have UTL_FILE_DIR param in the init file of the instance with the same directory (don't forget to stop and restart the base)
2) Handled the exceptions listed in the utl_file package (Oracle_home\rdbms\admin\utlfile.sql) in your source.
3) Turn set serveroutput on in SQL*Plus.
Run your procedure and see the error message to debug.
Good luck
null
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; -
I was given an assingment, but have no idea where to begin. The assingment is to create a text file using notepad with all of my digital inputs and some how make those imputs show up on my digital indicators on my control pannel.
When it was explained to me it didn't sound to hard of a task, I have no LabVIEW experience and the tutortial sucks.StevenD: FYI, I did NOT give you the one star rating. I would never do that!
StevenD wrote:
Ow. Someone is grumpy today.
Well, this is an assignment, so it is probably homework.
Why else would anyone give HIM such an assigment, after all he has no LabVIEW experience and the tutorials are too hard for him?
This would make no sense unless all of it was just covered in class!
This is not a free homework service with instant gratification.
OK! Let's do it step by step. I assume you already have a VI with the digital indicators.
"...but have no idea where to begin".
open notepad.
decide on a format, possibly one line per indicator.
type the document.
close notepad.
open LabVIEW.
Open the existing VI with all the indicators.
(are you still following?)
look at the diagram.
Who made the program?
Does the code make sense so far?
Is it a statemachine or just a bunch of crisscrossed wires?
Where do you want to add the file read?
How should the file be read (after pressing a read button, at the start of the program ,etc.)
See how far you get!
Message Edited by altenbach on 06-24-2008 11:23 AM
LabVIEW Champion . Do more with less code and in less time . -
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; -
Compress Text File Using PL/Sql
Hi All,
My Environment is -------> Oracle 11g Database Release 1 On Windows 2003 Server SP2
Requirement is ------------> Compress a text file using Oracle pl/sql
I am tryring to compress a text file using below pl/sql code,
DECLARE
src_file BFILE;
v_content BLOB;
v_blob_len INTEGER;
v_file UTL_FILE.file_type;
v_buffer RAW(32767);
v_amount BINARY_INTEGER := 32767;
v_pos INTEGER := 1;
v_blob BLOB;
BEGIN
src_file := BFILENAME('MY_FILES','expLive.log');
DBMS_LOB.fileopen(src_file, dbms_lob.file_readonly);
v_content := utl_compress.lz_compress(src_file);
v_blob_len := DBMS_LOB.getlength(v_content);
v_file := UTL_FILE.fopen('MY_FILES','test.log.zip','wb');
WHILE v_pos < v_blob_len
LOOP
DBMS_LOB.READ(v_content, v_amount, v_pos, v_buffer);
UTL_FILE.put_raw(v_file, v_buffer, TRUE);
v_pos := v_pos + v_amount;
END LOOP;
UTL_FILE.fclose(v_file);
EXCEPTION
WHEN OTHERS THEN
IF UTL_FILE.is_open(v_file) THEN
UTL_FILE.fclose(v_file);
END IF;
RAISE;
END;
i am able to get the zip file without any problem.But the Problem here is size of the zip file, Acutal Text File Size is 520 KB and zip file size is 241KB. is there any possible way to compress little bit further to 100KB?
Please Advice ....in case if you are have an example please share it.
Thanks for your helps
Shanis there any possible way to compress little bit furtherYou can compress better (but slower) with
v_content := utl_compress.lz_compress(src_file, 9);
.. -
Comma delimited text file using pl/sql ???
Hi friends
I want to create a comma delimited text file from emp table like using pl/sql program. I have oracle 8.0.4.0.0 production.
emp.txt
7844,TURNER,SALESMAN,7698,08-SEP-81,1500,0,30
7876,ADAMS,CLERK,7788,23-MAY-87,1100,,20
7900,JAMES,CLERK,7698,03-DEC-81,950,,30
Thanks
SaiHi Justin
I traced out the error message, is below,
1 declare
2 fh utl_file.file_type;
3 d varchar2(20) := 'sample data';
4 sql_err number(10);
5 error_info varchar2(60);
6 begin
7 fh := utl_file.fopen('c:\','A.TXT','A');
8 utl_file.put(fh,d);
9 dbms_output.put_line('file created');
10 d:='&urdata';
11 utl_file.putf(fh,'\n given data is %s\n',d);
12 utl_file.put_line(fh,'this is last line');
13 utl_file.put(fh,'hello');
14 utl_file.fflush(fh);
15 utl_file.fclose(fh);
16 exception
17 when others then
18 sql_err := SQLCODE;
19 error_info := SQLERRM;
20 dbms_output.put_line(SQL_ERR||'-'||error_info);
21 if utl_file.is_open(fh) then
22 utl_file.fclose(fh);
23 end if;
24* end;
SQL> /
Enter value for urdata: TEST
old 10: d:='&urdata';
new 10: d:='TEST';
1-User-Defined Exception
PL/SQL procedure successfully completed.
Sai -
How do I create a binary file using PL/SQL
Hi
Can you help me with writing a binary file in PL/SQL? I like to load binary data from data file, process in database then write to a new binary file.
Thank you very much in advance.
regrds
YongdongDBMS_LOB won't help you write a BLOB to disk.
You need to an external procedure to do that. Eric K's java works great and he's helped me with his solution and probably about a thousand others.
Doing a search on this forum will yield many discussions on this often regurgitated topic. -
Create a Multiple Files using PL-SQL
Hi Experts,
I have a table which contains 360 records and want to create multiple files with each file containing 12 records.
I tried to use the below code, however was unsuccessfull..
Please help!!!!
DECLARE
num NUMBER := 1;
f_file UTL_FILE.file_type;
row_sm NUMBER := 0.00;
CURSOR c_cur
IS
SELECT customer_key, all_columns_concat
FROM demo;
c_rec c_cur%ROWTYPE;
BEGIN
OPEN c_cur;
LOOP
FETCH c_cur
INTO c_rec;
EXIT WHEN c_cur%NOTFOUND;
WHILE num = c_rec.file_num
LOOP
f_file := UTL_FILE.fopen ('DM_DIR', 'FILE_' || num, 'w', 3267);
num := num + 1;
END LOOP;
UTL_FILE.put_line (f_file, c_rec.all_columns_concat);
row_sm := c_rec.sum_row + row_sm;
END LOOP;
UTL_FILE.fclose (f_file);
CLOSE c_cur;
END;
Regards,
RaviR.Thanks Ramin,
Your Code really helped. I am now not getting no errors and files are also being created. However, if I replace the UTL_FILE with DBMS_OUTPUT, I get below output. In your code i replaced 12 with 3. Hence its dividing after 3 rows. (with 1st row duplicated)
Output
1
1
2
3
========================================
4
4
5
6
========================================
7
7
8
9
========================================
10
10
11
12
========================================
13
13
14
15
========================================
16
16
Now my concern is that i my table I have a flag which determines the seperation...
Column1
File_Num
1
1
2
1
3
1
4
2
5
2
6
2
7
3
8
3
9
3
10
3
11
3
Hence if the file_Num is 1 it will go to File1, file_num=2 will be written to File2 and so on.
Please help me to get this logic.
Thanks in advance.
Regards,
RaviR -
Not able to create a text file in Client Machine using Forms 10g
Hi - I'm mot able to create a text file in Client Machine using Forms 10g. I'm getting error when system executes the bolded line.(i.e. CLIENT_TEXT_IO.FOPEN). During run-time system is throwing "ORA-06508: PL/SQL: could not find program unit being called".
I had attached webutil.pll in the form.
Please advise me.
DECLARE
l_Temp CLIENT_TEXT_IO.FILE_TYPE;
LC$Name Varchar2(100) ;
LC$Fic Varchar2(100) ;
LN$Lines Pls_integer := 0 ;
LC$Line Varchar2(4000) ;
Cursor C_CUR Is
Select * From TEST_TEXTIO where rownum <= 50;
BEGIN
LC$Name := 'tmp_file.txt' ;
LC$Fic := 'c:\temp\tmp_file.txt';
Begin
l_temp := CLIENT_TEXT_IO.FOPEN( 'C:\temp\tmp_file.txt', 'W');
Exception
when others then
message( 'OPEN FILE ERROR ' || LC$Fic ||sqlerrm );
display_error;
raise form_trigger_failure;
End ;
-- Write the lines --
For Cur In C_CUR Loop
LC$Line := Cur.CODE || ' -> ' || Cur.COL1 || ',' || Cur.COL2 || ',' || Cur.Col3 ;
CLIENT_TEXT_IO.PUT_LINE( LF$File, LC$Line ) ;
LN$Lines := C_CUR%ROWCOUNT ;
End loop ;
-- Close the file --
CLIENT_TEXT_IO.FCLOSE( LF$File ) ;
Exception
When others Then
message('err='||sqlerrm);message(' ');
display_error;
CLIENT_TEXT_IO.FCLOSE( LF$File ) ;
Raise ;
END;
Thanks.
ArunHi ,
i have one Question though its not related to thos question if some could help me it will be really helpful,
i have created one program to import and export excel to forms vice versa, if the form is attached with webutil its working fine am using ole2.but i have a new idea if i add this program in menu, it can be used for all the forms attched to that menu, i made program also, but problem am facing right now is if the form is not attched with webutil it will not work so is there possiblity to share my webutil from one form to another form ,so i will keep one form as a interface form in that webutil will be attched ,i can share that webutil to all other forms so that all the forms no need to attach webutil again
if its possible means please suggest some ideas -
How can I create an csv/excel file using pl/sql and then sending that file
How can I create an csv/excel file using pl/sql and then sending that file to a clients site using pl/sql?
I know how to create the csv/excel file but I can't figure out how I would get it to the clients site.968776 wrote:
How can I create an csv/excel file using pl/sql and then sending that file to a clients site using pl/sql?
I know how to create the csv/excel file but I can't figure out how I would get it to the clients site.You are trying to do it at a wrong place..
Whay do you want database (pl/sql) code to do these things?
Anyhow, you may be interested in :
{message:id=9360007}
{message:id=9984244} -
HOw to create a text file in the given path and delete it after the use?
Hi all,
I am trying to create a text file at the given path and delete the created file after the use.
I am using following code.:
import java.io.*;
// write binary data as characters
public class RanIO {
public static void main(String f[])
// First illustrate append
String lineSep = "\n";
try {
File temp= new File("C:/Ash","cute.txt");
boolean ch=temp.createNewFile();
if(ch)
System.out.println("file created");
else
System.out.println("file Not created");
//writing to file
/* PrintWriter p = new PrintWriter(new BufferedWriter(new FileWriter("cute.txt",true)));
p.print("Emp NO");
p.close();*/
// Open fileWriter in append mode
FileWriter fos = new FileWriter(temp, true);
BufferedWriter bw = new BufferedWriter(fos);
PrintWriter pw = new PrintWriter(fos);
double d=550;
// lineSep = System.getProperty("line.separator");
pw.print("Hello");
//pw.print( lineSep );
pw.print( d );
pw.close();
boolean det=temp.delete();
if(det)
System.out.println("File deleted");
else
System.out.println("File not deleted");
} catch (IOException ioe)
System.out.println( "Append IO error:" + ioe );
My problem:
1)
I am not able to write to the file. I want to know, where i am going wrong.
It is giving error message like
"Canot resolve Symbol: temp,"
But, FileWriter Constructor should accept a File type parameter.
here temp is a file parameter.
If i am not using file=new file();
i can't delete the file after the use. i.e if i use
PrintWriter p = new PrintWriter(new BufferedWriter(new FileWriter("cute.txt",true)));
how can i delete cute.txt after the use?
2)
I am not able to write to the text file. file is created but, a blank file.
"Hello" is not written into the text file.
can anyone help me in this regard
Thanks in advance
AshviniThank you Ram,
But, i want to create a text file in Append mode.
for that i used
FileWriter fos = new FileWriter(temp,true); But, it is not accepting FileWriter constructor in
this format. if i use
FileWriter fos = new
FileWriter("c:/ash/cute.txt",true); it works fine. !!!!!Here's the javadoc
public FileWriter(File file,
boolean append)
throws IOExceptionConstructs a FileWriter object given a File object. If the second argument is true, then bytes will be written to the end of the file rather than the beginning.
Parameters:
file - a File object to write to
append - if true, then bytes will be written to the end of the file rather than the beginning
Throws:
IOException - if the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason
Since:
1.4
Are you using jdk.13 or lower ?
>
ONe more doubt, Does flush method deletes a file?
if not, then i need to use
File temp=new File("c:/ash/cute.txt");
FileWriter fos = new FileWriter(temp,true); //which
is again a problem
if(temp.delete())
out.println("File is deleted");
}I don't know whether i am taking it wrong ! or
anything wrong with my coding ! but, after creating
and writing data into a text file. I must delete it
as it contains confidential informations.
Regards,
Thanks.'flush' writes to a file immediately. Else you should explicitly call 'flush' to write contents from buffer to underlying source.
javadoc again
PrintWriter
public PrintWriter(Writer out,
boolean autoFlush)Create a new PrintWriter.
Parameters:
out - A character-output stream
autoFlush - A boolean; if true, the println, printf, or format methods will flush the output buffer
cheers,
ram.
Question; What do you gain by opening a file, writing to it and deleting it in the same program ? -
Reading Text Files using Oracle PL/SQL and UTL_FILE
Hi, experts. I tried to read a text file into oracle. Here is what i did:
1. Create a text file in the directory: C:\temp\New Text Document.txt
2. Run the following SQL in PL/SQL:
CREATE or replace DIRECTORY sampledata AS 'C:\temp\';
grant read, write on directory sampledata to public;
3. When I run the following code, it caused an error: ora-29280: invalid directory path. I have alread checked the directory path, it is correct. Would someone know what wrong is my code?
declare
f utl_file.file_type;
s varchar2(3000);
begin
f := utl_file.fopen('sampledata','New Text Document.txt','R');
utl_file.get_line(f,s);
utl_file.fclose(f);
dbms_output.put_line(s);
end;
Thank youTry with a filename without spaces:No problem with spaces
SQL> declare
2 f utl_file.file_type;
3 s varchar2(3000);
4 begin
5 f := utl_file.fopen('SAMPLEDATA','New Text Document.txt','R');
6 utl_file.get_line(f,s);
7 utl_file.fclose(f);
8 dbms_output.put_line(s);
9 end;
10 /
line1
PL/SQL procedure successfully completed. -
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 -
Creating Files Using PL/SQL?
Hi,
Is there a way to create a file using PL/Sql? It seems that the
utl_file package doesn't allow you to create a file. It only
allows you to read, write to a file that already exists there.
Thanks
VicMake sure the following is set in the init.ora:
UTL_FILE_DIR = /path/to/your/file/directory (for unix)
or
UTL_FILE_DIR = <drive>:\path\to\your\file\directory (for windows) -
How to create and read text file using LabVIEW 7.1 PDA module?
How to create and read text file using LabVIEW 7.1 PDA module? I can not create a text file and read it.
I attach my code here.
Attachments:
File_IO.vi 82 KBWell my acquisition code runs perfect. The problem is reading it. I can't seem to read my data no matter what I do. My data gets saved as a string using the array to string vi but I've read that the string to array vi (which I need to convert back to array to read my data) does not work on the pda. I'm using version 8.0. So I was trying to modify the program posted in this discussion so that it would save data from my DAQ. I did that but I still can't read the data after its saved. I really don't know what else to do. All I need to do is read the data on the pda itself. I can't understand why I'm having such a hard time doing that. I found a possible solution on another discussion that talks about parsing the strings because of the bug in the "string to array" vi. However, that lead me to another problem because for some reason, the array indicators or graphs don't function on the pda. When i build the program to the pda or emulator, the array indicators are faded out on the front panel as if the function is not valid. Does this kind of help give a better picture of what I'm trying to do. Simply read data back. Thanks.
Maybe you are looking for
-
I need help disentangling gmail and Thunderbird
The article "Thunderbird and Gmail" would be very helpful if I were setting up my email from the beginning. but I set it up a long time ago and I need help straightening things out. The folders at the left hand side of the Thunderbird screen are very
-
Have had the BT home hub 1 for years with 3 PCs connected via Ethernet & USB connections (wireless is useless as house is 170 yrs old with 15 inch thick walls that have defeated all wireless attempts). Now that I have vision there are not enough conn
-
How can I deternmine what apps are running on my Mac Book Pro
Hoe can I determine what applications I have open and running on my Mac Book Pro
-
Problem with currency and quantity field
Hi, I have done a couple of BDC and watched that if some currency or quantity field is there and i want to run the BDC the particular currency/quantity field will not populate the value inspite of i have declared the same table field that it refers t
-
My screen keeps moving around with the cursor?
HI everyone out there. i just got back from my 2 week honeymoon and had my G5 power mac, with a 27inch cinema display powered off the whole time. I turned it on and the screen is zoomed out a little bit. and when I move the mouse cursor around the sc