Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an Oracle equivalent for the T-SQL PRINT statement? I want to show script progress

I have searched and not found any answers that address this directly. I just want to display that I have successfully completed script blocks while executing an implementation script that does multiple things.

PRINT 'Start script'
PRINT 'Insert A'
    INSERT INTO Customers (CustomerName, City, Country)
    VALUES ('Cardinal', 'Stavanger', 'Norway');
PRINT 'Update B'
    UPDATE Customers
    SET ContactName='Alfred Schmidt', City='Hamburg'
    WHERE CustomerName='Alfreds Futterkiste';
PRINT 'End script'

So, when done, the output should look something like:

Start script
Insert A
4 rows inserted
Update B
1 row updated
End script
like image 401
Spencer Sullivan Avatar asked Feb 11 '14 17:02

Spencer Sullivan


People also ask

Does Oracle use TSQL?

Although both systems use a version of Structured Query Language, or SQL, MS SQL Server uses Transact SQL, or T-SQL, which is an extension of SQL originally developed by Sybase and used by Microsoft. Oracle, meanwhile, uses PL/SQL, or Procedural Language/SQL.

Are there print statements in SQL?

Usually, we use the SQL PRINT statement to print corresponding messages or track the variable values while query progress. We also use interactions or multiple loops in a query with a while or for a loop. We can also use the SQL PRINT statement to track the iteration.

How do I display output in SQL?

To do this we use a procedure called dbms_output. put_line to place the results in a buffer that SQL*Plus will retrieve and display. SQL*Plus must be told to retrieve data from this buffer in order to display the results. The SQL*Plus command 'set serveroutput on' causes SQL*Plus to retrieve and display the buffer.


1 Answers

Oracle has a database output buffer called DBMS Output, which has a PUT_LINE() function. However, most Oracle tools (SQL*Plus and SQL Developer) do not have the output buffer enabled, by default, in a session.

To turn on this output buffer in SQL Developer, do the following:

  1. Go to View | DBMS Output to enable the DBMS Output window
  2. Push the green plus icon to enable DBMS Output for a particular session.

To turn on this output buffer on in SQL*Plus!, you need to set the SERVEROUTPUT value via the command prompt, like this:

SET SERVEROUTPUT ON [SIZE N|UNLIMITED]

Example:

SET SERVEROUTPUT ON 50000

Note: The 50000 is a length limit, in bytes.


Now you can use the PUT_LINE() function to actually write status messages, like this:

DBMS_OUTPUT.PUT_LINE('Status message goes here.');

Here is the documentation for SET SERVEROUTPUT PL-SQL command.

Here is the documentation for DBMS_OUTPUT.PUT_LINE() function.

like image 113
Karl Anderson Avatar answered Sep 22 '22 02:09

Karl Anderson