You Asked
Here is a simpler example
declare
f utl_file.file_type;
CRLF1 CONSTANT CHAR(2) := CHR(13)||CHR(10); -- Carriage Return+Line Feed
CRLF CONSTANT CHAR(1) := CHR(10); -- Line Feed
begin
f := utl_file.fopen('BTAM_DIR', 'something.txt', 'w');
utl_file.put_line(f, 'line one: some text'||CRLF);
utl_file.put_line(f, 'line two: more text'||CRLF);
utl_file.put_line(f, 'EOF'||CRLF1);
utl_file.fclose(f);
end;
The carriage return comes out as ^M even though i am creating the file on unix and there is no FTP involved. The only reason I sent my original code was for you to see if I was making any mistake while creating or appending the clob or while creating a file from it.
Thanks
Sangita
Hi Tom,
I am creating a flat file from the database using utl_file.
The first step is to format the data in way it is need into a clob and then create a file on unix using utl_file. However the unix folks processing the file cannot see the linefeed or carriage return.
SQL> desc extract
Name Null? Type
----------------------------------------- -------- -------------------
CLIENT_CD NOT NULL VARCHAR2(12)
FILE_ID NOT NULL NUMBER(20)
EXTRACT NOT NULL CLOB
PROCEDURE SSP_GENERATE_CLOB (p_client_cd in extract.client_cd%type,
p_recd_file_id in extract.received_file_id%type,
p_upd_user_id in extract.update_user_id%type)
AS
/******************************************************************************
NAME: AIP_CREATE_EXTRACT
REVISIONS:
Ver Date Author Description
--------- ---------- --------------- ------------------------------------
1.0 01/15/2006 A268343 Created this package.
******************************************************************************/
-------------------------
-- Constants
-------------------------
c_method_nm CONSTANT TRANSACTION_LOG.METHOD_NM%TYPE := 'SSP_GENERATE_CLOB';
l_clob EXTRACT.EXTRACT%TYPE;
l_last_record varchar2(200) := 'EOF';
l_alloc_header varchar2(4000);
l_alloc_detail varchar2(4000);
l_ref_no varchar2(11);
l_alloc_cnt_in_trd number(4);
l_tot_commission_a trades.tot_commission_a%type;
l_file_header varchar2(500);
allc_cnt number := 0;
CRLF1 CONSTANT CHAR(2) := CHR(13)||CHR(10); -- Carriage Return+Line Feed
CRLF CONSTANT CHAR(1) := CHR(10); -- Line Feed
l_message_txt transaction_log.message_txt%type;
l_commission varchar2(30);--trades.tot_commission_a%type;
l_comm_code varchar2(1);
l_cusip_symbol varchar2(20);
-- l_master_acct_type trade_allocation.acct_type_c%type;
BEGIN
BEGIN
--Step 1. Create FTO header
l_file_header := ssp_get_file_header(p_client_cd,p_recd_file_id);
-- Step 2 Create the Allocation header / detail record for trades and trade allocations
-- Loop thru the trades for the given file
INSERT INTO btam_extract
(CLIENT_CD,
RECEIVED_FILE_ID,
EXTRACT,
CREATE_USER_ID,
CREATE_TMSTP,
UPDATE_USER_ID,
UPDATE_TMSTP )
values
( p_client_cd,p_recd_file_id, empty_clob(),p_upd_user_id,sysdate,p_upd_user_id,sysdate )
returning extract into l_clob;
-- Append the file header to the empty clob
dbms_lob.writeappend( l_clob,length(l_file_header|| CRLF),l_file_header|| CRLF );
-- Create The allocation group record. For format please refer to the documentation or
-- SDS / TSD for AutoAllocations.
For trd in (Select trd.CLIENT_CD ,trd.RECEIVED_FILE_ID, trd.TRADE_ID , trd.MASTER_FBSI_BRCH_C,
trd.MASTER_FBSI_BASE_C, trd.PRICE , trd.TRADE_DT, trd.SETTLEMENT_DT, trd.CUSP_N ,
trd.TICKER_NM, trd.TRADE_AWAY_IND,trd.EXECUTING_BROKER,trd.FBSI_ORDER_NUM ,trd.REF_ORDER_NUM,
trd.TRADE_QTY,trd.TRADE_STATUS_CD ,trd.TOT_COMMISSION_A,trd.ACTION_CD
from trades trd
where received_file_id = p_recd_file_id
and trade_status_cd = 'P')
LOOP
-- Create the trade header record. Format of this record can be found in the SDS or TSD
l_alloc_header := 'H'||'A'|| rpad(nvl(trd.master_fbsi_brch_c,' '),3,' ')||rpad(nvl(trd.master_fbsi_base_c,' '),6,' ')||' '||
to_char(trd.trade_dt,'yyyy-mm-dd')||l_cusip_symbol||
rpad(trd.action_cd,2,' ')||
' '||'P'||to_char(trd.settlement_dt,'yyyy-mm-dd')||
'A'||substr(trd.ref_order_num,2,11)||
lpad(l_alloc_cnt_in_trd,4,'0')||btam.SSP_GET_BTAM_FMT_NUMBER(trd.price,6,9)||rpad(' ',9,' ')|| btam.SSP_GET_BTAM_FMT_NUMBER(trd.trade_qty,12,5)||rpad(' ',3,' ')||
lpad(nvl(btam.SSP_GET_BTAM_FMT_NUMBER(l_commission,8,5),' '),13,' ')||rpad(nvl(l_comm_code,' '),1,' ')||
-- since all fields are optional after this, I am just adding space for the cumulative length
rpad(' ',36,' ')||rpad(nvl(substr(trd.executing_broker,1,4),' '),4,' ')||rpad(' ',4,' ')||
rpad(' ',27,' ');
dbms_lob.writeappend( l_clob, length(l_alloc_header || CRLF),l_alloc_header || CRLF );
-- create the allocation detail block
FOR allc in (SELECT allc.CLIENT_CD ,allc.RECEIVED_FILE_ID, allc.TRADE_ID, allc.ALLOC_FBSI_BRCH_C,
allc.ALLOC_FBSI_BASE_C,allc.ACCT_TYPE_C,allc.ALLOC_QTY, allc.ACTION_CD,
allc.COMMISSION_CD, allc.COMMISSION_A,allc.ALLOCATION_STATUS_CD
from trade_allocation allc
WHERE trade_id = trd.trade_id
and allocation_status_cd = 'P')
LOOP
allc_cnt := allc_cnt + 1;
-- Format of this detail record can be found in the BTAM doc or SDS / TSD
l_alloc_detail:= 'D'||'A'||rpad(trd.master_fbsi_brch_c,3,' ')||rpad(trd.master_fbsi_base_c,6,' ')||
allc.acct_type_c||to_char(trd.trade_dt,'yyyy-mm-dd')||
l_cusip_symbol||rpad(allc.action_cd,2,' ')||
' '||lpad(allc_cnt,4,'0')||rpad(allc.alloc_fbsi_brch_c,3,' ')||rpad(allc.alloc_fbsi_base_c,6,' ')||
rpad(' ',1,' ')||btam.SSP_GET_BTAM_FMT_NUMBER(allc.alloc_qty,12,5)||'FIX'||lpad(' ',13,' ')||
' '||lpad(' ',11,' ')||rpad(' ',2,' ')||rpad(' ',2,' ')||' '||'Y'||rpad(' ',4,' ')||
rpad(' ',4,' ')||rpad(' ',20,' ')||rpad(' ',6,' ') ;
dbms_lob.writeappend( l_clob,length(l_alloc_detail|| CRLF),l_alloc_detail || CRLF );
END LOOP;
END LOOP;
-- Step 3 End of file
-- Append the last record
dbms_lob.writeappend( l_clob,length(l_last_record|| CRLF1),l_last_record || CRLF1 );
COMMIT;
The next step is to create a file from the db
PROCEDURE SSP_CLOB_TO_FILE ( p_client_cd in btam_extract.client_cd%type,
p_recd_file_id in btam_extract.received_file_id%type,
p_output_file_nm OUT received_files.output_file_nm%type)
AS
-------------------------
-- Constants
-------------------------
c_method_nm CONSTANT TRANSACTION_LOG.METHOD_NM%TYPE := 'SSP_CLOB_TO_FILE';
l_output utl_file.file_type;
l_amt number default 32000;
l_offset number default 1;
l_clob clob;
l_length number ;
l_file_nm varchar2(30):= 'OUTPUTFILE';
l_message_txt transaction_log.message_txt%type;
BEGIN
-- get the extract for this client for this recd_file_id
BEGIN
select extract
into l_clob
from btam_extract
where client_cd = p_client_cd
and received_file_id = p_recd_file_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
l_message_txt := 'EXTRACT NOT FOUND';
alloc_adm.aip_ins_trans_log (p_client_cd,p_recd_file_id, null, null, null, null, 'ERROR', 'E', g_package_nm, c_method_nm,l_message_txt);
RAISE;
WHEN OTHERS THEN
l_message_txt := substr(sqlerrm,1,500);
alloc_adm.aip_ins_trans_log (p_client_cd,p_recd_file_id, null, null, null, null, 'ERROR', 'E', g_package_nm, c_method_nm,l_message_txt);
RAISE;
END;
-- Get the output file name for this client.
BEGIN
select fto_file_nm
into l_file_nm
from clients
where client_cd = p_client_cd;
EXCEPTION
WHEN NO_DATA_FOUND THEN
l_message_txt := 'FTO FILE NAME NOT FOUND';
alloc_adm.aip_ins_trans_log (p_client_cd,p_recd_file_id, null, null, null, null, 'ERROR', 'E', g_package_nm, c_method_nm,l_message_txt);
RAISE;
WHEN OTHERS THEN
l_message_txt := substr(sqlerrm,1,500);
alloc_adm.aip_ins_trans_log (p_client_cd,p_recd_file_id, null, null, null, null, 'ERROR', 'E', g_package_nm, c_method_nm,l_message_txt);
RAISE;
END;
-- append date/time to the file name
l_file_nm := l_file_nm||to_char(sysdate,'mmddyyyyhh24miss');
l_length := nvl(dbms_lob.getlength(l_clob),0);
l_output := utl_file.fopen('BTAM_DIR', l_file_nm, 'w', 32760);
while ( l_offset < l_length )
loop
utl_file.put(l_output,
dbms_lob.substr(l_clob,l_amt,l_offset) );
utl_file.fflush(l_output);
l_offset := l_offset + l_amt;
end loop;
-- utl_file.new_line(l_output);
utl_file.fclose(l_output);
p_output_file_nm := l_file_nm;
EXCEPTION
WHEN OTHERS THEN
l_message_txt := substr(sqlerrm,1,500);
alloc_adm.aip_ins_trans_log (p_client_cd,p_recd_file_id, null, null, null, null, 'ERROR', 'E', g_package_nm, c_method_nm,l_message_txt);
RAISE;
END SSP_CLOB_TO_FILE ;
Please advice - what am I not doing right regarding the linefeed and carriage return. Thanks Sangita
and Tom said...
On unix, only the linefeed is the end of line.
If you have a file that has \r\n (carriage return, linefeed) the carriage return will appear at ^M in many editors.
this is normal, this is expected.
You put it there. If you don't want it, don't use it.
If you just used utl-file.put_line - it would do the right thing on the right OS's as you move the code.
Rating
(7 ratings)
We're not taking comments currently, so please try again later if you want to add a comment.