DBMS_OUTPUT v14
The DBMS_OUTPUT package sends messages (lines of text) to a message buffer or gets messages from the message buffer. A message buffer is local to a single session. Use the DBMS_PIPE package to send messages between sessions.
The procedures and functions available in the DBMS_OUTPUT package are listed in the following table.
| Function/procedure | Return type | Description |
|---|---|---|
DISABLE | n/a | Disable the capability to send and receive messages. |
ENABLE(buffer_size) | n/a | Enable the capability to send and receive messages. |
GET_LINE(line OUT, status OUT) | n/a | Get a line from the message buffer. |
GET_LINES(lines OUT, numlines IN OUT) | n/a | Get multiple lines from the message buffer. |
NEW_LINE | n/a | Puts an end-of-line character sequence. |
PUT(item) | n/a | Puts a partial line without an end-of-line character sequence. |
PUT_LINE(item) | n/a | Puts a complete line with an end-of-line character sequence. |
SERVEROUTPUT(stdout) | n/a | Direct messages from PUT, PUT_LINE, or NEW_LINE to either standard output or the message buffer. |
The following table lists the public variables available in the DBMS_OUTPUT package.
| Public variables | Data type | Value | Description |
|---|---|---|---|
chararr | TABLE | For message lines. |
CHARARR
The CHARARR is for storing multiple message lines.
TYPE chararr IS TABLE OF VARCHAR2(32767) INDEX BY BINARY_INTEGER;
DISABLE
The DISABLE procedure clears out the message buffer. You can no longer access any messages in the buffer at the time the DISABLE procedure is executed. Any messages later sent with the PUT, PUT_LINE, or NEW_LINE procedures are discarded. No error is returned to the sender when the PUT, PUT_LINE, or NEW_LINE procedures are executed and messages were disabled.
Use the ENABLE or SERVEROUTPUT(TRUE)` procedure to reenable sending and receiving messages.
DISABLE
Examples
This anonymous block disables sending and receiving messages in the current session.
BEGIN
DBMS_OUTPUT.DISABLE;
END;ENABLE
The ENABLE procedure enables you to send messages to or retrieve messages from the message buffer. Running SERVEROUTPUT(TRUE) also implicitly performs the ENABLE procedure.
The destination of a message sent with PUT, PUT_LINE, or NEW_LINE depends on the state of SERVEROUTPUT.
- If the last state of
SERVEROUTPUTisTRUE, the message goes to standard output of the command line. - If the last state of
SERVEROUTPUTisFALSE, the message goes to the message buffer.
ENABLE [ (<buffer_size> INTEGER) ]
Parameter
buffer_size
Maximum length of the message buffer in bytes. If you specify a buffer_size of less than 2000, the buffer size is set to 2000.
Examples
This anonymous block enables messages. Setting SERVEROUTPUT(TRUE) forces them to standard output.
BEGIN
DBMS_OUTPUT.ENABLE;
DBMS_OUTPUT.SERVEROUTPUT(TRUE);
DBMS_OUTPUT.PUT_LINE('Messages enabled');
END;
Messages enabledYou can achieve the same effect by using SERVEROUTPUT(TRUE).
BEGIN
DBMS_OUTPUT.SERVEROUTPUT(TRUE);
DBMS_OUTPUT.PUT_LINE('Messages enabled');
END;
Messages enabledThis anonymous block enables messages, but setting SERVEROUTPUT(FALSE) directs messages to the message buffer.
BEGIN
DBMS_OUTPUT.ENABLE;
DBMS_OUTPUT.SERVEROUTPUT(FALSE);
DBMS_OUTPUT.PUT_LINE('Message sent to buffer');
END;GET_LINE
The GET_LINE procedure retrieves a line of text from the message buffer. Only text that was terminated by an end-of-line character sequence is retrieved. That includes complete lines generated using PUT_LINE or by a series of PUT calls followed by a NEW_LINE call.
GET_LINE(<line> OUT VARCHAR2, <status> OUT INTEGER)
Parameters
line
Variable receiving the line of text from the message buffer.
status
0 if a line was returned from the message buffer, 1 if there was no line to return.
Examples
This anonymous block writes the emp table out to the message buffer as a comma-delimited string for each row.
EXEC DBMS_OUTPUT.SERVEROUTPUT(FALSE);
DECLARE
v_emprec VARCHAR2(120);
CURSOR emp_cur IS SELECT * FROM emp ORDER BY empno;
BEGIN
DBMS_OUTPUT.ENABLE;
FOR i IN emp_cur LOOP
v_emprec := i.empno || ',' || i.ename || ',' || i.job || ',' ||
NVL(LTRIM(TO_CHAR(i.mgr,'9999')),'') || ',' || i.hiredate ||
',' || i.sal || ',' ||
NVL(LTRIM(TO_CHAR(i.comm,'9990.99')),'') || ',' || i.deptno;
DBMS_OUTPUT.PUT_LINE(v_emprec);
END LOOP;
END;This anonymous block reads the message buffer and inserts the messages written by the prior example into a table named messages. The rows in messages are then displayed.
CREATE TABLE messages (
status INTEGER,
msg VARCHAR2(100)
);
DECLARE
v_line VARCHAR2(100);
v_status INTEGER := 0;
BEGIN
DBMS_OUTPUT.GET_LINE(v_line,v_status);
WHILE v_status = 0 LOOP
INSERT INTO messages VALUES(v_status, v_line);
DBMS_OUTPUT.GET_LINE(v_line,v_status);
END LOOP;
END;
SELECT msg FROM messages;
msg
-----------------------------------------------------------------
7369,SMITH,CLERK,7902,17-DEC-80 00:00:00,800.00,,20
7499,ALLEN,SALESMAN,7698,20-FEB-81 00:00:00,1600.00,300.00,30
7521,WARD,SALESMAN,7698,22-FEB-81 00:00:00,1250.00,500.00,30
7566,JONES,MANAGER,7839,02-APR-81 00:00:00,2975.00,,20
7654,MARTIN,SALESMAN,7698,28-SEP-81 00:00:00,1250.00,1400.00,30
7698,BLAKE,MANAGER,7839,01-MAY-81 00:00:00,2850.00,,30
7782,CLARK,MANAGER,7839,09-JUN-81 00:00:00,2450.00,,10
7788,SCOTT,ANALYST,7566,19-APR-87 00:00:00,3000.00,,20
7839,KING,PRESIDENT,,17-NOV-81 00:00:00,5000.00,,10
7844,TURNER,SALESMAN,7698,08-SEP-81 00:00:00,1500.00,0.00,30
7876,ADAMS,CLERK,7788,23-MAY-87 00:00:00,1100.00,,20
7900,JAMES,CLERK,7698,03-DEC-81 00:00:00,950.00,,30
7902,FORD,ANALYST,7566,03-DEC-81 00:00:00,3000.00,,20
7934,MILLER,CLERK,7782,23-JAN-82 00:00:00,1300.00,,10
(14 rows)GET_LINES
The GET_LINES procedure retrieves one or more lines of text from the message buffer into a collection. Only text that was terminated by an end-of-line character sequence is retrieved. That includes complete lines generated using PUT_LINE or by a series of PUT calls followed by a NEW_LINE call.
GET_LINES(<lines> OUT CHARARR, <numlines> IN OUT INTEGER)
Parameters
lines
Table receiving the lines of text from the message buffer. See CHARARR for a description of lines.
numlines IN
Number of lines to retrieve from the message buffer.
numlines OUT
Actual number of lines retrieved from the message buffer. If the output value of numlines is less than the input value, then no more lines are left in the message buffer.
Examples
This example uses the GET_LINES procedure to store all rows from the emp table that were placed in the message buffer into an array.
EXEC DBMS_OUTPUT.SERVEROUTPUT(FALSE);
DECLARE
v_emprec VARCHAR2(120);
CURSOR emp_cur IS SELECT * FROM emp ORDER BY empno;
BEGIN
DBMS_OUTPUT.ENABLE;
FOR i IN emp_cur LOOP
v_emprec := i.empno || ',' || i.ename || ',' || i.job || ',' ||
NVL(LTRIM(TO_CHAR(i.mgr,'9999')),'') || ',' || i.hiredate ||
',' || i.sal || ',' ||
NVL(LTRIM(TO_CHAR(i.comm,'9990.99')),'') || ',' || i.deptno;
DBMS_OUTPUT.PUT_LINE(v_emprec);
END LOOP;
END;
DECLARE
v_lines DBMS_OUTPUT.CHARARR;
v_numlines INTEGER := 14;
v_status INTEGER := 0;
BEGIN
DBMS_OUTPUT.GET_LINES(v_lines,v_numlines);
FOR i IN 1..v_numlines LOOP
INSERT INTO messages VALUES(v_numlines, v_lines(i));
END LOOP;
END;
SELECT msg FROM messages;
msg
-----------------------------------------------------------------
7369,SMITH,CLERK,7902,17-DEC-80 00:00:00,800.00,,20
7499,ALLEN,SALESMAN,7698,20-FEB-81 00:00:00,1600.00,300.00,30
7521,WARD,SALESMAN,7698,22-FEB-81 00:00:00,1250.00,500.00,30
7566,JONES,MANAGER,7839,02-APR-81 00:00:00,2975.00,,20
7654,MARTIN,SALESMAN,7698,28-SEP-81 00:00:00,1250.00,1400.00,30
7698,BLAKE,MANAGER,7839,01-MAY-81 00:00:00,2850.00,,30
7782,CLARK,MANAGER,7839,09-JUN-81 00:00:00,2450.00,,10
7788,SCOTT,ANALYST,7566,19-APR-87 00:00:00,3000.00,,20
7839,KING,PRESIDENT,,17-NOV-81 00:00:00,5000.00,,10
7844,TURNER,SALESMAN,7698,08-SEP-81 00:00:00,1500.00,0.00,30
7876,ADAMS,CLERK,7788,23-MAY-87 00:00:00,1100.00,,20
7900,JAMES,CLERK,7698,03-DEC-81 00:00:00,950.00,,30
7902,FORD,ANALYST,7566,03-DEC-81 00:00:00,3000.00,,20
7934,MILLER,CLERK,7782,23-JAN-82 00:00:00,1300.00,,10
(14 rows)NEW_LINE
The NEW_LINE procedure writes an end-of-line character sequence in the message buffer.
NEW_LINE
Parameter
The NEW_LINE procedure expects no parameters.
PUT
The PUT procedure writes a string to the message buffer. No end-of-line character sequence is written at the end of the string. Use the NEW_LINE procedure to add an end-of-line character sequence.
PUT(<item> VARCHAR2)
Parameter
item
Text written to the message buffer.
Examples
The following example uses the PUT procedure to display a comma-delimited list of employees from the emp table.
DECLARE
CURSOR emp_cur IS SELECT * FROM emp ORDER BY empno;
BEGIN
FOR i IN emp_cur LOOP
DBMS_OUTPUT.PUT(i.empno);
DBMS_OUTPUT.PUT(',');
DBMS_OUTPUT.PUT(i.ename);
DBMS_OUTPUT.PUT(',');
DBMS_OUTPUT.PUT(i.job);
DBMS_OUTPUT.PUT(',');
DBMS_OUTPUT.PUT(i.mgr);
DBMS_OUTPUT.PUT(',');
DBMS_OUTPUT.PUT(i.hiredate);
DBMS_OUTPUT.PUT(',');
DBMS_OUTPUT.PUT(i.sal);
DBMS_OUTPUT.PUT(',');
DBMS_OUTPUT.PUT(i.comm);
DBMS_OUTPUT.PUT(',');
DBMS_OUTPUT.PUT(i.deptno);
DBMS_OUTPUT.NEW_LINE;
END LOOP;
END;
7369,SMITH,CLERK,7902,17-DEC-80 00:00:00,800.00,,20
7499,ALLEN,SALESMAN,7698,20-FEB-81 00:00:00,1600.00,300.00,30
7521,WARD,SALESMAN,7698,22-FEB-81 00:00:00,1250.00,500.00,30
7566,JONES,MANAGER,7839,02-APR-81 00:00:00,2975.00,,20
7654,MARTIN,SALESMAN,7698,28-SEP-81 00:00:00,1250.00,1400.00,30
7698,BLAKE,MANAGER,7839,01-MAY-81 00:00:00,2850.00,,30
7782,CLARK,MANAGER,7839,09-JUN-81 00:00:00,2450.00,,10
7788,SCOTT,ANALYST,7566,19-APR-87 00:00:00,3000.00,,20
7839,KING,PRESIDENT,,17-NOV-81 00:00:00,5000.00,,10
7844,TURNER,SALESMAN,7698,08-SEP-81 00:00:00,1500.00,0.00,30
7876,ADAMS,CLERK,7788,23-MAY-87 00:00:00,1100.00,,20
7900,JAMES,CLERK,7698,03-DEC-81 00:00:00,950.00,,30
7902,FORD,ANALYST,7566,03-DEC-81 00:00:00,3000.00,,20
7934,MILLER,CLERK,7782,23-JAN-82 00:00:00,1300.00,,10PUT_LINE
The PUT_LINE procedure writes a single line to the message buffer including an end-of-line character sequence.
PUT_LINE(<item> VARCHAR2)
Parameter
item
Text to write to the message buffer.
Examples
This example uses the PUT_LINE procedure to display a comma-delimited list of employees from the emp table.
DECLARE
v_emprec VARCHAR2(120);
CURSOR emp_cur IS SELECT * FROM emp ORDER BY empno;
BEGIN
FOR i IN emp_cur LOOP
v_emprec := i.empno || ',' || i.ename || ',' || i.job || ',' ||
NVL(LTRIM(TO_CHAR(i.mgr,'9999')),'') || ',' || i.hiredate ||
',' || i.sal || ',' ||
NVL(LTRIM(TO_CHAR(i.comm,'9990.99')),'') || ',' || i.deptno;
DBMS_OUTPUT.PUT_LINE(v_emprec);
END LOOP;
END;
7369,SMITH,CLERK,7902,17-DEC-80 00:00:00,800.00,,20
7499,ALLEN,SALESMAN,7698,20-FEB-81 00:00:00,1600.00,300.00,30
7521,WARD,SALESMAN,7698,22-FEB-81 00:00:00,1250.00,500.00,30
7566,JONES,MANAGER,7839,02-APR-81 00:00:00,2975.00,,20
7654,MARTIN,SALESMAN,7698,28-SEP-81 00:00:00,1250.00,1400.00,30
7698,BLAKE,MANAGER,7839,01-MAY-81 00:00:00,2850.00,,30
7782,CLARK,MANAGER,7839,09-JUN-81 00:00:00,2450.00,,10
7788,SCOTT,ANALYST,7566,19-APR-87 00:00:00,3000.00,,20
7839,KING,PRESIDENT,,17-NOV-81 00:00:00,5000.00,,10
7844,TURNER,SALESMAN,7698,08-SEP-81 00:00:00,1500.00,0.00,30
7876,ADAMS,CLERK,7788,23-MAY-87 00:00:00,1100.00,,20
7900,JAMES,CLERK,7698,03-DEC-81 00:00:00,950.00,,30
7902,FORD,ANALYST,7566,03-DEC-81 00:00:00,3000.00,,20
7934,MILLER,CLERK,7782,23-JAN-82 00:00:00,1300.00,,10SERVEROUTPUT
The SERVEROUTPUT procedure directs messages to standard output of the command line or to the message buffer. Setting SERVEROUTPUT(TRUE) also performs an implicit execution of ENABLE.
The default setting of SERVEROUTPUT depends on the implementation. For example, in Oracle SQL*Plus, SERVEROUTPUT(FALSE) is the default. In PSQL, SERVEROUTPUT(TRUE) is the default. Also, in Oracle SQL*Plus, you control this setting using the SQL*Plus SET comman, not by a stored procedure as implemented in EDB Postgres Advanced Server.
SERVEROUTPUT(<stdout> BOOLEAN)
To get an Oracle-style display output, you can set the dbms_output.serveroutput to FALSE in the postgresql.conf file, which disables the message output. The default is TRUE, which enables the message output.
Parameter
stdout
Set to TRUE if you want subsequent PUT, PUT_LINE, or NEW_LINE to send text directly to standard output of the command line. Set to FALSE to send text to the message buffer.
Examples
This anonymous block sends the first message to the command line and the second message to the message buffer.
BEGIN
DBMS_OUTPUT.SERVEROUTPUT(TRUE);
DBMS_OUTPUT.PUT_LINE('This message goes to the command line');
DBMS_OUTPUT.SERVEROUTPUT(FALSE);
DBMS_OUTPUT.PUT_LINE('This message goes to the message buffer');
END;
This message goes to the command lineIf, in the same session, the following anonymous block is executed, the message stored in the message buffer from the prior example is flushed. It's displayed on the command line along with the new message.
BEGIN
DBMS_OUTPUT.SERVEROUTPUT(TRUE);
DBMS_OUTPUT.PUT_LINE('Flush messages from the buffer');
END;
This message goes to the message buffer
Flush messages from the buffer