Saturday, 16 June 2018

UTL File


declare
   f1 utl_file.file_type;
     v_str varchar(100);
 begin
      f1 := utl_file.fopen('TEST123','data.txt','r');
           utl_file.get_line(f1,v_str);
dbms_output.put_line(v_str);
         utl_file.fclose(f1);
end;
/

declare
   f1 utl_file.file_type;
    v_deptno number(10);
    v_deptnm varchar(20);
     v_str varchar(100);
 begin
      f1 := utl_file.fopen('TEST123’,'data.txt','r');
loop
          begin
         utl_file.get_line(f1,v_str);
        v_deptno := substr(v_str,1,instr(v_str,',',1)-1);
        v_deptnm := substr(v_str,instr(v_str,',',1)+1);
    insert into dept_demo(dnum,dname) values(v_deptno,v_deptnm);
   exception
            when no_data_found then
              exit;
       end;
     end loop;
      utl_file.fclose_all;
end;
/

declare
   f1 utl_file.file_type;
 cursor d is select * from dept_demo;
 v_str dept_demo%rowtype;
 v_var varchar(100);
 begin
     f1 := utl_file.fopen('TEST123' , 'data.txt','w');
   open d;
      loop
          fetch d into v_str;
          exit when d%notfound;
         v_var := v_str.dnum ||','||v_str.dname||','
         ||v_str.loc;
          utl_file.put_line(f1,v_var);
 end loop;
 close d;
     utl_file.fclose(f1);
 end;

ff to ff
declare
   fl1 utl_file.file_type;
   fl2 utl_file.file_type;
   v_st varchar(100);
begin
  fl1 := utl_file.fopen('TEST123',
                               'data.txt','R');
  fl2 := utl_file.fopen('TEST123',
                             'cust_trgt.txt','W');
 loop
   begin
       utl_file.get_line(fl1,v_st);
       utl_file.put_line(fl2,v_st);
      exception
         when no_data_found then
         exit;
   end;
 end loop;
utl_file.fclose_all;
end;
/

No comments:

Post a Comment