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
Sai
Hi 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
Similar Messages
-
Uploading comma delimited text file
Dear Experts,
I want to upload comma delimited text file in sap and get that values into internal table
Can any 1 provide a sample code ??
thanx in advance.Hi Suchitra ,
There is FM GUI_UPLOAD which will help you in this.
Its has got one parameter calles has_field_separator . Here you can pass comma. So that this will take care while uploading the data in Internal table.
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = wf_file
filetype = 'ASC'
has_field_separator = ','
* HEADER_LENGTH = 0
* READ_BY_LINE = 'X'
* DAT_MODE = ' '
* CODEPAGE = ' '
* IGNORE_CERR = ABAP_TRUE
* REPLACEMENT = '#'
* CHECK_BOM = ' '
* IMPORTING
* FILELENGTH =
* HEADER =
TABLES
data_tab = it_data
EXCEPTIONS
file_open_error = 1
file_read_error = 2
no_batch = 3
gui_refuse_filetransfer = 4
invalid_type = 5
no_authority = 6
unknown_error = 7
bad_data_format = 8
header_not_allowed = 9
separator_not_allowed = 10
header_too_long = 11
unknown_dp_error = 12
access_denied = 13
dp_out_of_memory = 14
disk_full = 15
dp_timeout = 16
OTHERS = 17
Hope this will help you.
Regards,
Nikhil -
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; -
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 -
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);
.. -
Deleting a column in a text file using LabVIEW
Hello all,
I'm trying to delete the first column of my tab delimited text file using LabVIEW and then save it under the same file name. Can someone show me a quick way to perform this operation. Is this even possible with LabVIEW? Any help would be much appreciated.
My purpose is to automate this operation for hundreds of daily text files containing data that needs processing. I'm currently using LabVIEW 8.2.
Thanks!
-noviceLabVIEWuserIf the file is relatively small:
Read the file using the Read from Spreadsheet File VI to get 2D array.
Remove the column from the 2D array.
Write out new 2D array to new file using Write to Spreadsheet File VI.
If the file is relatively large then you will likely run into memory issues. In this case you will need to read the file in chunks. You can decide how many lines to read at a time. Use a for-loop that's set to run for the number of chunks to read (based on the total number of lines and the number of lines you want to read at a time). Hint: Quotient & Remainder function. In the loop use the Read Text File VI to read your set number of lines. Convert the lines to a 2D array delete your column, and write out that chunk of data to the new file. Rinse and repeat. -
SQL to match a single value in a field with comma-delimited text
I have a column that can contain none, one or many recordIDs
(from another table) stored as comma-delimited text strings (i.e.,
a list). I need to retrieve all records that match a single value
within that list.
For example, if I want to match all values that equal
recordID 3, and I use ... WHERE MyColumn IN ('3') ... , I will get
all records that have EXACTLY 3 as the value of MyColumn, but not
any MyColumn records whose values include 3, if they are instances
such as "3,17" or the like.
Also using the LIKE operator -- as WHERE MyColumn LIKE '%3%'
-- will get me unwanted records with values such as 35 or 13 ...
Can I use some sort of intervening ColdFusion list processing
to output only the desired records?Normalize your database so that your data becomes
accessible. -
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 -
If you have answer, please provide specific and detailed steps.
Situation 1:
Start:
150+ files in .xls OR .xlsx format
data contains many characters EXCEPT "Tab"
Process:
Use Automator to:
Select Files from Finder window
Convert selected files from xls/xlsx to TAB-DELIMITED file format
End:
150+ files in .txt format
files have identical names to those from "Start:"
data is TAB-DELIMITED
Situation 2:
Start:
150+ files in .xls OR .xlsx format
data contains many characters EXCEPT | (pipe, vertical bar)
Process:
Use Automator to:
Select Files from Finder window
Convert selected files from xls/xlsx to |-delimited file format
End:
150+ files in .txt format
files have identical names to those from "Start:"
data is "|"-delimitedIf your post is addressed to my response then the answer is you have to write your own or search on-line to see if something exists to do what you want.
As you note there is no pre-built script for this.
regards
Message was edited by: Frank Caggiano - Are you certain there isn;t something in Excel to export as a tab delimited file?
This link Convert An Excel Spreadsheet To A Tab Delimited Text File seems to indicate that the function is in Excel already. -
Why this error in writing string to text file using utl_file?
HI Friends,
Iam trying to write procedure to add one line of text in text file using UTIL package. But getting error.
create or replace procedure Add_To_File() is
OutFile utl_file.file_type;
l_err_code NUMBER(10);
l_err_msg VARCHAR2(2000);
vNewLine VARCHAR2(4000);
vdir varchar2(200):='UTIL_DIR';
begin
OutFile:=utl_file.fopen(vdir,'out.txt','w','32000');
vNewLine:='Loading is successfull';
utl_file.put_line(OutFile,vNewLine);
EXCEPTION
WHEN OTHERS THEN
l_err_code := SQLCODE;
l_err_msg := SUBSTR(SQLERRM,1,128);
Dbms_output.put_line(l_err_code || l_err_msg);
end;
when i execute above procedure iam getting this error.
LINE/COL ERROR
1/23 PLS-00103: Encountered the symbol ")" when expecting one of the
following:
<an identifier> <a double-quoted delimited-identifier>
current delete exists prior
Please suggest me where iam wrong.
Thanks,
Venkat Vadlamudi868591 wrote:
HI Friends,
Iam trying to write procedure to add one line of text in text file using UTIL package. But getting error.
create or replace procedure Add_To_File() is
OutFile utl_file.file_type;
l_err_code NUMBER(10);
l_err_msg VARCHAR2(2000);
vNewLine VARCHAR2(4000);
vdir varchar2(200):='UTIL_DIR';
begin
OutFile:=utl_file.fopen(vdir,'out.txt','w','32000');
vNewLine:='Loading is successfull';
utl_file.put_line(OutFile,vNewLine);
EXCEPTION
WHEN OTHERS THEN
l_err_code := SQLCODE;
l_err_msg := SUBSTR(SQLERRM,1,128);
Dbms_output.put_line(l_err_code || l_err_msg);
end;
when i execute above procedure iam getting this error.
LINE/COL ERROR
1/23 PLS-00103: Encountered the symbol ")" when expecting one of the
following:
<an identifier> <a double-quoted delimited-identifier>
current delete exists prior
Please suggest me where iam wrong.
Thanks,
Venkat Vadlamudibelow works for me
1 CREATE OR replace PROCEDURE Add_to_file
2 IS
3 outfile utl_file.file_type;
4 l_err_code NUMBER(10);
5 l_err_msg VARCHAR2(2000);
6 vnewline VARCHAR2(4000);
7 vdir VARCHAR2(200) := 'UTIL_DIR';
8 BEGIN
9 outfile := utl_file.Fopen(vdir, 'out.txt', 'w', '32000');
10 vnewline := 'Loading is successfull';
11 utl_file.Put_line(outfile, vnewline);
12 EXCEPTION
13 WHEN OTHERS THEN
14 l_err_code := SQLCODE;
15 l_err_msg := Substr(sqlerrm, 1, 128);
16 dbms_output.Put_line(l_err_code
17 || l_err_msg);
18* END;
SQL> /
Procedure created. -
XML Column from table extract to Pipe Delimited Text File
Hi,
I have an XML column with large data in a Table ( Source SQL server Database).
I was asked to extract XML column to .txt file using SSIS.
Is it possible to extract xml column with huge data to text file ?
when I tried, select XML column from Table in source , I noticed that Property of column is taken as [DT_NTEXT] . I Converted it to DT_TEXT as Ansi donot support DT_NTEXT.
Execution method was success but it failed due to trucation. so wondering is there a way to get XML column extracted to Pipe delimited text file?
Is it advisable to do this ? or IS It Valid to export XML in Pipe Delimited File ?
Please Kindly advice
thanks
kodiAre you looking at shredding data within XML nodes and then importing it to text file or are you looking at exporting XML value as is? Also is SSIS a necessity?
If not, You can simply use T-SQL for this along with bcp for this. just use a query like
EXEC xp_cmdshell 'bcp "SELECT CAST(XMLColumn AS varchar(max)) AS Column FROM table" queryout <full file path> -c -S <ServerName> -T -t |'
provided you use trusted connection (windows authentication)
see
http://visakhm.blogspot.in/2013/10/bcp-out-custom-format-data-to-flat-file.html
If you want to shred the data use Xpath functions in the query as below
http://visakhm.blogspot.in/2012/10/shred-data-as-well-as-metadata-from-xml.html
Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs -
Search in txt file using PL/SQL
Is there any way to search for a string in a text file using a PL/SQL block??
Thnks in advance.
AshishRichard:
It would depend on the nature of the text file, but it could be done for most plain text files. If there is some consistent delimiter, that is, the text file is fielded in some way, you could define an external table with the appropriate structure then use normal SQL queries against the particular fields from the file.
If the file is just plain text with no fields, you could create an external table something like:
CREATE TABLE myexternal (
line VARCHAR2(4000),
ORGANIZATION EXTERNAL
(TYPE oracle_loader
DEFAULT DIRECTORY mydirectory
ACCESS PARAMETERS
(RECORDS DELIMITED BY newline
BADFILE 'textfile.bad'
DISCARDFILE 'textfile.dis'
LOGFILE 'textfile.log'
(line CHAR(4000) NULLIF line = blanks))
LOCATION ('textfile.txt'))
REJECT LIMIT UNLIMITEDThen use normal sql to search the contents of that file.
I have not done any benchmarks on this, but my gut feel is that it would be significantly faster than using utl_file to loop over the lines looking for specific content.
In one of our warehouse load programs, we process a text file that is typically about 7Mb in under 30 seconds.
John -
How to get tab delimited text file when Stored Procedure executes ?
Hello Everyone,
I have a stored procedure which returns 1 result set as an output.
I want the output as a Tab Delimited Text file.
I know that I can use SSIS and get the same output but I am not allowed to use SSIS in this case.
Is there any other ways ?
I will be really appreciate If someone can answer with, How many different ways I can get Tab delimited text file ?
Thanks in advance
Henaryyou can do that by using SQLCMD
http://blog.sqlauthority.com/2013/03/05/sql-server-exporting-query-results-to-csv-using-sqlcmd/
you can also use BCP
EXEC master..xp_cmdshell 'bcp "select * from tempdb.dbo.orders" queryout "c:\output.txt" -c -T'
EXEC master..xp_cmdshell 'bcp "exec usertest.dbo.proctest" queryout "c:\output.txt" -c -T'
--Prashanth -
EXcel to tab delimited text file
I am saving an excel file into a tab delimited text file so that i can use it for uploading data.
The excel file contains a text field in which there is a comma ',' .
for all the fields which has a comma in the excel file
the text file is adding double qoutes " in the beginning and end of the filed.
for example
contents of filed in excel file is : GASKET, FLAT OEM 1380X1530X3 SIL ARAMIDE
and in the text file it is coming as "GASKET, FLAT OEM 1380X1530X3 SIL ARAMIDE"
i dont want the " in the beginning and end
can anybody help.
points will be rewarded.
I am just saving the excel file as tab delimited text file and i am getting the quotes added in the begining and end.
Thanks.Thats to preserer the comma in between otherwise when you upload the same the data will split at comma.
Regards,
Amit
Reward all helpful replies.
Maybe you are looking for
-
How to get back lost mail and Shutdown problems?
Dear Apple Users and Support, I recently did an idiotic thing and managed to turn one of my mail boxes where I collected mail from 2-3 of my friends totally into junk. I remember highlighting all the message and was attempting to move them to a new m
-
HP Laserjet 5si is printing postscript, not the print job
What is the adjustment that needs to stop my HP Laserjet 5si from printing page after page of postscript jibberish? Thanks to everyone that helped me to install this printer in my System Printer cue. After the present problem is solved, all should be
-
UCCX CSQ Most Skilled doesn't work
Hi, We have UCCX 9.0.2 with three CSQ's: - Arabic_CSQ - English_CSQ - VIP_CSQ and there are three skills (Arabic, English and VIP) I assigned 9 agents for these CSQ with the mentioned skills. Agent 1, Agent 2 and Agent 3 skills : Arabic 9, English, 6
-
Hi, Can anyone please give me pros and cons of using active directory vs OID. We are in process of centralizing tnsnames.ora. Thanks in advance!
-
Adobe PremCS5.5.0 works but after the update to CS5.5.2 it crashes
Adobe PremCS5.5.0 works but after the update to CS5.5.2 it crashes, it starts with loading some modules from Premiere and then disappears from srceen. When I reload my old image again on the disk, CS5.5.0 works again Will it help to do first a manual