Question

Basically its a letter printing in text I'm trying to do via an Oracle PL/SQL Procedure.

My Code below: -

DECLARE
       out_file UTL_FILE.file_type;    lv_file VARCHAR2 (200) DEFAULT TO_CHAR (SYSDATE, 'yyyymmddhhmiss')||'.txt' ;LVSQL varchar2(4000);    lV_TXT varchar2(4000);lv_txt1 varchar2(4000);lv_final_str varchar2(4000); BEGIN    out_file := UTL_FILE.fopen ('SPOOL_DIR', lv_file, 'W');    for i in 1..5 loop
       begin
            select tn,tt into lv_txt,lv_txt1 from 
            (SELECT rownum r, TNAME tn ,TABTYPE tt FROM tab) where r=j;
            lv_final_str:=lV_TXT||chr(9)||lv_txt1||CHR(13);
            utl_file.put_line(Out_file,lv_final_str );
       end;    end loop;

       UTL_FILE.fclose (out_file);
    END;

I'm able to print the values in next next line by concatenating the lv_final_str with CHR(13) and that I use the *utl_file.put_line(Out_file,lv_final_str );* within the loop

I want to do the same thing by collecting the text in a variable inside the loop. and writing the value of the variable outside the loop, using the below code.

DECLARE
   out_file UTL_FILE.file_type;
   lv_file VARCHAR2 (200) DEFAULT TO_CHAR (SYSDATE, 'yyyymmddhhmiss')||'.txt' ;LVSQL varchar2(4000);
   lV_TXT varchar2(4000);lv_txt1 varchar2(4000);tmpvar varchar2(4000);lv_build_str varchar2(4000);lv_final_str varchar2(4000);
BEGIN
   --SELECT TO_CHAR (SYSDATE, 'yyyymmddhhmiss') INTO lv_file FROM DUAL;
   out_file := UTL_FILE.fopen ('SPOOL_DIR', lv_file, 'W');
   for i in 1..5 loop
       begin
            select tn,tt into lv_txt,lv_txt1 from 
            (SELECT rownum r, TNAME tn ,TABTYPE tt FROM tab) where r=i;
            tmpvar:=lV_TXT||chr(9)||lv_txt1||CHR(13);
            lv_build_str := lv_build_str || chr(9)||tmpvar;
--            utl_file.put_line(Out_file,lv_final_str );
       end;
   end loop;
   lv_final_str:=lv_build_str;
    utl_file.put_line(Out_file,lv_final_str );
   UTL_FILE.fclose (out_file);
END;

how to do this, please help. I used chr(10) as well, it is not printing in new line. if you observe chr(9) which is used to print tab space is working well. only next line chr(10) or chr(13) is not working. Why...?

any help.. I'm trying since last 3 days... please help.

Was it helpful?

Solution

It depends on whether you're writing to a file for a Unix environment, or Windows.

Unix uses just a single LF character CHR(10) whereas Windows expects a CR followed by LF CHR(13)||CHR(10).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top