0

I try to print something to a file. The file has header, body, footer and each of them has some function in it respectively. Is there a way I can separate it into each procedure then combine them as one procedure?

For instance, I made a procedure with open, write, close file process, and then I put the header procedure, body, footer procedure inside it,

Code idea demonstrates like below, but the error message said ORA-06550 and PLS-00222: no function with name 'header' exists in this scope.

This is why I want to know if SQL could do this way?

procedure print_document is

   -- Variables for writing data to file
   v_file utl_file.file_type;
   v_utlDir VARCHAR2(35) := get_dir('Directory','Dir');
   v_utlFileName VARCHAR2(35);

Begin

   --Open file and Write to file
   SELECT test.dat' into v_utlfilename from dual;
   v_file := utl_file.fopen(v_utlDir, v_utlFileName, 'W');  

   --print header, body, footer
   utl_file.put_line(v_file,header); -- the header is a procedure
   utl_file.put_line(v_file,body);
   utl_file.put_line(v_file,footer);

   utl_file.fclose(v_file);        
      --
      EXCEPTION WHEN OTHERS THEN
         DBMS_OUTPUT.PUT_LINE('Error in SQL. The error is');
         DBMS_OUTPUT.PUT_LINE(SQLERRM);  
End;

Ideally, if I call print_document procedure then it will print all the lines to the cmd console.

thatjeffsmith
  • 20,522
  • 6
  • 37
  • 120
Stu_Dent
  • 370
  • 1
  • 4
  • 19
  • 1
    yeah, but you have to write the header() procedure, and then a body(), and footer(). And utl_file.put_line() doesn't write to the buffer which you would see from your console, it writes to a FILE...which you would then need to read and print out... – thatjeffsmith May 25 '19 at 13:42
  • I made these procedures but doesn't know how to put them in the 'document() procedure' individual just to make it look nicer and similar to the encapsulation technique. May I ask if you'd like to demonstrate how to do it...Is that very advance as I'm a beginner at this language. Sorry for my abrupt reply. – Stu_Dent May 25 '19 at 14:01
  • can we circle back? is the point of your exercise to just print the contents of test.dat? – thatjeffsmith May 25 '19 at 14:12
  • Yes, and I try to make it look better without combining all the variables and put_line into one procedure to write into a file. – Stu_Dent May 25 '19 at 14:20
  • 1
    reading in a text file and printing it out to the console...much easier see example from oracle-base https://oracle-base.com/articles/9i/utl_file-random-access-of-files-9i – thatjeffsmith May 25 '19 at 14:37
  • Is that mean I still have to put all content (variable/ SQL statement) in the `print_document procedure()` to print on the console? – Stu_Dent May 26 '19 at 07:01
  • Sample data and expected results would be really helpful. It seems like printing the file contents should be straightforward, but there is some complication to do with headers and footers that I don't understand. What do you mean by *'the header is a procedure'*? Do you mean there is some other procedure for printing headers? – William Robertson May 26 '19 at 08:53

0 Answers0