Call now: 252-767-6166  
Oracle Training Oracle Support Development Oracle Apps

 
 Home
 E-mail Us
 Oracle Articles
New Oracle Articles


 Oracle Training
 Oracle Tips

 Oracle Forum
 Class Catalog


 Remote DBA
 Oracle Tuning
 Emergency 911
 RAC Support
 Apps Support
 Analysis
 Design
 Implementation
 Oracle Support


 SQL Tuning
 Security

 Oracle UNIX
 Oracle Linux
 Monitoring
 Remote s
upport
 Remote plans
 Remote
services
 Application Server

 Applications
 Oracle Forms
 Oracle Portal
 App Upgrades
 SQL Server
 Oracle Concepts
 Software Support

 Remote S
upport  
 Development  

 Implementation


 Consulting Staff
 Consulting Prices
 Help Wanted!

 


 Oracle Posters
 Oracle Books

 Oracle Scripts
 Ion
 Excel-DB  

Don Burleson Blog 


 

 

 


 

 

 

 
 

Oracle dbms_output tips

Oracle Database Tips by Donald Burleson

Oracle dbms_output

The PL/SQL dbms_output package has a put_line procedure to allow you to write data to flat file or to direct your PL/SQL output to a screen.

When using dbms_output inside SQL*Plus, you need to use the "set serveroutput on 10000" command (where 10000 is the display arraysize argument) to see the PL/SQL output directly with dbms_output.put_line

Here is a code example using dbms_output:


dbms_output.put_line('Fixed IO/SEC:'||to_char(fixed_io_per_sec,'9,999,999.99'));
dbms_output.put_line('Temp IO/SEC :'||to_char(temp_io_per_sec, '9,999,999.99'));
dbms_output.put_line('Total IO/SEC:'||to_char(tot_io_Per_Sec, '9,999,999.99'));



dbms_output.put_line('FS1 Blocks = '||v_fs1_blocks);
dbms_output.put_line('FS2 Blocks = '||v_fs2_blocks);
dbms_output.put_line('FS3 Blocks = '||v_fs3_blocks);
dbms_output.put_line('FS4 Blocks = '||v_fs4_blocks);
dbms_output.put_line('Full Blocks = '||v_full_blocks);
end;


More information on dbms_output


The dbms_output package was introduced in Oracle7. Dbms_output is used to allow output to the SQL buffer in SQL*Plus from PL/SQL blocks. The dbms_output package was intended primarily as a debugging tool. The dbms_output package is now being supplanted by Oracle's step-through debuggers and several other third-party tools.

The dbms_output
package is often used to deal with runtime errors. Dbms_output can be used to isolate the location of an error so that a developer can correct the problem. The dbms_output package provides an excellent debugging tool when used properly.

In order to use the DBMS_Output package for debugging, you must issue the following command.

 

set serveroutput on

 



Oracle 11g provides the following information about the DBMS_OUTPUT package:

Description of the DBMS_OUTPUT package:

 
 
PROCEDURE DBMS_OUTPUT.DISABLE
 
PROCEDURE DBMS_OUTPUT.ENABLE
 Argument Name                  Type                    In/Out Default?
 ------------------------------ ----------------------- ------ --------
 BUFFER_SIZE                    NUMBER(38)              IN     DEFAULT
 
PROCEDURE DBMS_OUTPUT.GET_LINE
 Argument Name                  Type                    In/Out Default?
 ------------------------------ ----------------------- ------ --------
 LINE                           VARCHAR2                OUT
 STATUS                         NUMBER(38)              OUT
 
PROCEDURE DBMS_OUTPUT.GET_LINES
 Argument Name                  Type                    In/Out Default?
 ------------------------------ ----------------------- ------ --------
 LINES                          TABLE OF VARCHAR2(32767) OUT
 NUMLINES                       NUMBER(38)              IN/OUT
 
PROCEDURE DBMS_OUTPUT.GET_LINES
 Argument Name                  Type                    In/Out Default?
 ------------------------------ ----------------------- ------ --------
 LINES                          DBMSOUTPUT_LINESARRAY   OUT
 NUMLINES                       NUMBER(38)              IN/OUT
 
PROCEDURE DBMS_OUTPUT.NEW_LINE
 
PROCEDURE DBMS_OUTPUT.PUT
 Argument Name                  Type                    In/Out Default?
 ------------------------------ ----------------------- ------ --------
 A                              VARCHAR2                IN
 
PROCEDURE DBMS_OUTPUT.PUT_LINE
 Argument Name                  Type                    In/Out Default?
 ------------------------------ ----------------------- ------ --------
 A                              VARCHAR2                IN
 
===========================================
 

Runtime errors are errors that occur while code is executing. These errors can arise due to data problems or code problems. For instance, attempting to assign a 31-character string to a varchar2 (30) variable will cause a runtime error.

Most approaches to dealing with runtime errors utilize the DBMS_Output package to isolate the location of an error so that a developer can correct the problem. This package provides an excellent debugging tool when used properly.

Using the DBMS_Output Package

The DBMS_Output package was first introduced with Oracle7 to allow output to the SQL buffer in SQL*Plus from PL/SQL blocks. The package was intended primarily as a debugging tool, and it has served that purpose admirably (although it is now being supplanted by step-through debuggers available in Oracle's Procedure Builder and several other third-party tools).

In order to use the DBMS_Output package for debugging, you must issue the

set serveroutput on

command in SQL*Plus. This command instructs SQL*Plus to collect the contents of the buffer after executing a PL/SQL block or stored PL/SQL object.

The size of this buffer defaults to 2,000 characters. For practical purposes, this limit is far too low. Fortunately, you can use the

set serveroutput on size n

command to specify the size of the buffer. In this command, n specifies the buffer size and can range from 2,000 characters to an upper limit of 1 million characters. A million characters is more than sufficient to debug any modularized block of code.

Debugging with the DBMS_Output package involves mostly calls to the DBMS_Output.Put_Line() procedure. This procedure writes a line to the SQL buffer. When a block of PL/SQL code finishes executing, the contents of the SQL buffer are displayed.

Listing 8.5 is an excerpt from the debugging version of the Build_SUID_Matrix package (the final version of the package can be found on the CD).

Listing 8.5 An excerpt of debugging code from the Build_SUID_Matrix package.

DBMS_Output.Put_Line ('Fetch ObjectSourceCode_cur');
 
FETCH ObjectSourceCode_cur INTO ObjectSourceCode_rec;
EXIT WHEN ObjectSourceCode_cur%NOTFOUND;
 
--
-- Initialize variables.
--
iStringLen        := 0;
iStringPos        := 0;
 
--
-- Clean the line of code before processing it.
--
DBMS_Output.Put_Line ('Call CleanLineOfSource');
vLine      := CleanLineOfSource (ObjectSourceCode_rec.text);
DBMS_Output.Put_Line (vLine);
 
--
-- If the line contains the string 'DELETE ', this might be a delete
-- operation.
--
DBMS_Output.Put_Line ('Check for DELETE ');
iStringPos := instr (vLine, 'DELETE ');
iStringLen := length (vLine);
 
--
-- Test the line to determine if the 'DELETE ' string is
--    A) inside a comment
--    B) part of an identifier
--
DBMS_Output.Put_Line ('Is the string inside a comment?');
 
IF ((instr (vLine, '--') > 0)
    AND
    (instr (vLine, '--') < iStringPos)) THEN
      iStringPos := 0;
END IF;
 
--
-- Is the string inside a comment?
--
DBMS_Output.Put_Line ('Check for string following a */');
 
IF ((instr (vLine, '/*') > 0)
    AND
    (iStringPos > instr (vLine, '/*'))) THEN
      iStringPos := 0;
END IF;
 
--
-- Is the string inside a comment?
--
DBMS_Output.Put_Line ('Check for */ without a preceding /*');
 
IF ((instr (vLine, '*/') > 0) AND (instr (vLine, '/*') = 0)) THEN
   iStringPos := 0;
END IF;
 
--
-- Is the string inside an identifier?
--
DBMS_Output.Put_Line ('Check for part of identifier');
IF ((instr (vLine, '_DELETE ') = (iStringPos - 1))
    AND
    (instr (vLine, '_DELETE ') > 0)) THEN
       iStringPos := 0;
END IF;
 
--
-- If the delete is beyond the first character of the line,
-- either it is poorly written code or it is a comment.
--
DBMS_Output.Put_Line ('The delete is past the first character');
 
IF (iStringPos > 1) THEN
   iStringPos := 0;
END IF;
 
--
-- If the line has passed all the false positive tests, go ahead
-- and display the table name.
--
IF (iStringPos > 0) THEN
   vParsedString := substr (vLine, (iStringPos + 6));
END IF;
 
--
-- If the line has passed the false positive tests, check to see
-- if it contains a 'FROM' clause.  If so, remove the clause from
-- the string.
--
IF (iStringPos > 0) THEN
   iStringPos := instr (vParsedString, 'FROM ');
 
   IF (iStringPos > 0) THEN
      vParsedString := substr (vParsedString, (iStringPos + 5));
   END IF;
 
   --
   -- Remove the semicolon at the end of the line.
   --
   DBMS_Output.Put_Line ('Replace ; at the end of the line');
   vParsedString := replace (vParsedString, ';', '');
 
   --
   -- Call the UpdateMatrix procedure to perform the write to the
   -- SUID_MATRIX table.
   --
   UpdateMatrix (vParsedString,
                 vOwner,
                 vObject,
                 'DELETE');
END IF;

Each of the calls to the DBMS_Output.Put_Line() procedure indicates the progress of the procedure. A string of text is passed as the procedure's lone parameter.

The runtime error must always occur after the last message that was delivered to the buffer. Isolating the error is now a simple matter of determining which statements occurred after the message.

 

Related notes on DBMS_OUTPUT:

For more information on dbms_output see here.

Advanced Oracle Utilities: The Definitive Reference by Rampant TechPress is written by top Oracle database experts (Bert Scalzo,  Donald Burleson, and Steve Callan). 

Buy direct from the publisher and save 30%!

 


 

 

��  
 
 
Oracle Training at Sea
 
 
 
 
oracle dba poster
 

 
Follow us on Twitter 
 
Oracle performance tuning software 
 
Oracle Linux poster
 
 
 

 

Burleson is the American Team

Note: This Oracle documentation was created as a support and Oracle training reference for use by our DBA performance tuning consulting professionals.  Feel free to ask questions on our Oracle forum.

Verify experience! Anyone considering using the services of an Oracle support expert should independently investigate their credentials and experience, and not rely on advertisements and self-proclaimed expertise. All legitimate Oracle experts publish their Oracle qualifications.

Errata?  Oracle technology is changing and we strive to update our BC Oracle support information.  If you find an error or have a suggestion for improving our content, we would appreciate your feedback.  Just  e-mail:  

and include the URL for the page.


                    









Burleson Consulting

The Oracle of Database Support

Oracle Performance Tuning

Remote DBA Services


 

Copyright © 1996 -  2020

All rights reserved by Burleson

Oracle ® is the registered trademark of Oracle Corporation.