PUTF procedure - Write a formatted string to a file

The PUTF procedure writes a formatted string to a specified file.

Syntax

Read syntax diagramSkip visual syntax diagramUTL_FILE.PUTF (file,format, ,argN)

Procedure parameters

file
An input argument of type UTL_FILE.FILE_TYPE that contains the file handle.
format
An input argument of type VARCHAR(1024) the specifies the string to use for formatting the text. The special character sequence, %s, is substituted by the value of argN. The special character sequence, \n, indicates a new line.
argN
An optional input argument of type VARCHAR(1024) that specifies a value to substitute in the format string for the corresponding occurrence of the special character sequence %s. Up to five arguments, arg1 through arg5, can be specified. arg1 is substituted for the first occurrence of %s, arg2 is substituted for the second occurrence of %s, and so on.

Authorization

EXECUTE privilege on the UTL_FILE module.

Examples

Format employee data.

SET SERVEROUTPUT ON@

CREATE PROCEDURE proc1()
BEGIN
  DECLARE v_filehandle    UTL_FILE.FILE_TYPE;
  DECLARE v_dirAlias      VARCHAR(50) DEFAULT 'mydir';
  DECLARE v_filename      VARCHAR(20) DEFAULT 'myfile.csv';
  DECLARE v_format        VARCHAR(200);
  SET v_format = '%s %s, %s\nSalary: $%s Commission: $%s\n\n';
  CALL UTL_DIR.CREATE_OR_REPLACE_DIRECTORY('mydir', '/tmp');
  SET v_filehandle = UTL_FILE.FOPEN(v_dirAlias,v_filename,'w');
  CALL UTL_FILE.PUTF(v_filehandle,v_format,'000030','SALLY','KWAN','40175','3214'); 
  CALL DBMS_OUTPUT.PUT_LINE('Wrote to file: ' || v_filename);
  CALL UTL_FILE.FCLOSE(v_filehandle);
END@

CALL proc1@

This example results in the following output:

Wrote formatted text to file: myfile.csv

The formatted file, myfile.csv, contains the following data:

000030 SALLY, KWAN
Salary: $40175 Commission: $3214