Use of serveroutput on or DBMS_OUTPUT in SQL Developer - oracle

What is the difference between the use of the script header Set serveroutput on, Set serveroutput on size 1000000 and enable DBMS_OUTPUT in SQLDeveloper, to see clearly the output (success or errors) of procedures PL/SQL or queries made in Oracle Databases. Or have I to use both?

DBMS_OUTPUT only writes to a buffer. It is the responsibility of the client to process that buffer - or not. The command 'set serverout on' is a sqlplus directive, telling sqlplus that it is to display the contents of that buffer, when control is returned to sqlplus.
I'm not sure exactly what you mean when you say " enable DBMS_OUTPUT in SQLDeveloper". In the SQLDev menu, under 'view' there is an item for 'dbms output', which pops open an additional pane for dbms output. In that pane there is a green 'plus sign' icon that is the SQL Dev equivalent of the sqlplus directive 'set serverout on'. Or you can include the 'set serverout on' directive in your sqlscript, and the output will appear in the 'script output' pane, as SQL Dev accepts most sqlplus directives.

Related

SQL Developer not displaying dbms_output

I am trying to code PL/SQL in SQL Developer but it is not displaying the output. My program is compiling successfully. Here is the code:
set serveroutput on
declare
begin
dbms_output.put_line('Hi');
end;
Please suggest what should I do to display the output? Is there any settings I need to change?
View->Dbms Output->Enable DBMS_OUTPUT for connection->OK
It may not be appearing because you are on an older version of Oracle Database. Make sure you're on 11gR2 or higher.
And then as an alterntative to the previous answer, you can simply
set serveroutput on
declare
begin
dbms_output.put_line('Hi');
end;
And then use the Execute as Script (F5) button on the worksheet toolbar.
Once serveroutput is set to 'on' we'll grab the DBMS_OUTPUT for you in the output panel anytime you run a Script.

Equivalent to SQLERRM when no error?

Does anyone knows if there is an equivalent to SQLERRM when no error occurred?
I know I can count the affected rows like here, but can I automatically get the message I would get from SQLPLUS e.g. role granted. after an execute immediate in PL/SQL?
I am trying to do
begin
execute immediate 'grant select on s1.tbl1 to user1';
dbms_output.put_line(<some function>);
end;
and get
Grant succeeded.
Thanks,
J
Feedback like "Grant succeeded." is the feature of sqlplus.
If you enable SQL*Net tracing (details http://docs.oracle.com/cd/E11882_01/java.112/e16548/apxtblsh.htm#JJDBC28987 or http://www.juliandyke.com/Diagnostics/Trace/NetTrace.php) you will see that string "Grant succeeded" is NOT something client application sqlplus receives from Oracle.
So how does it know what to display? Because sqlplus has privimitive mechanism of checking command type before sending it to server. If you type non-existend command then it will not be even sent to server (you can check that also in SQL*NET trace).
Moreover you can check list of commands if you open binary of sqlplus as text and search for string "grant", for example (bottom left corner on screenshot).
In below case there will be no exchange with server because command is intentionally wrong.
SQL> some_command
SP2-0734: unknown command beginning "some_comma..." - rest of line
ignored.
So Oracle knows command type before sending SQL text to server and for some commands it receives additional information after execution. Like number of rows affected for DML. This information is enough to display "feedback".
Back to your task, the easiest way would be to implement primitive parsing of first 1-3 words of the command and display correponding message if no errors occured.
Hardcore option is to enable SQL tracing and derive command type from trace file.
=====================
PARSING IN CURSOR #3 len=31 dep=0 uid=91 oct=17 lid=91 tim=2178486867995 hv=3483936374 ad='b967c6a4' sqlid='10y8y7m7uj9mq'
grant execute on <...> to <...>
END OF STMT
PARSE #3:c=0,e=614,p=0,cr=0,cu=0,mis=1,r=0,dep=0,og=1,plh=0,tim=2178486867994
=====================
oct means Oracle Command Type
SQL> select command_name from v$sqlcommand where command_type = 17;
COMMAND_NAME
----------------------------------------------------------------
GRANT OBJECT
Another option is to enable Audit but my preference would be to keep it simple.
If it is only the output for success message is what we are concerned, then simply using exceptions would help.
SET SERVEROUTPUT ON
BEGIN
execute immediate 'grant select on s1.tbl1 to user1' ;
dbms_output.put_line('Grant succeeded');
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line('Grant Failed');
procedure_to_log_error(SQLERRM);
END;

Get result rows in sql developer window

I run following procedure in sql developer window to search all oracle tables for specific string and at the end in Statement Output window it says "anonymous block completed" and now can't understand where is result rows, how to get it?
thanks
We have option in SQL Developer
and also you have to execute your procedure as below
SET SERVEROUTPUT ON;
begin
dbms_output.put_line('Testing output');
end;
/
Click ->View->Dbms Output and then click + symbol on Dbms output window. now you can run the procedure and can see output.

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
For SQLPlus, there is the PROMPT command.
Syntax:
PRO[MPT] [text]
where text represents the text of the message you want to display.
Sends the specified message or a blank line to the user's screen. If you omit text, PROMPT displays a blank line on the user's screen.
Usage:
You can use this command in scripts to give information to the user.
Examples:
The following example shows the use of PROMPT in conjunction with ACCEPT in a script called ASKFORDEPT.SQL. ASKFORDEPT.SQL contains the following SQL*Plus and SQL commands:
PROMPT
PROMPT Please enter a valid department
PROMPT For example: 10
SELECT DEPARTMENT_NAME FROM EMP_DETAILS_VIEWWHERE DEPARTMENT_ID = &NEWDEPT
Assume you run the file using START or #:
#ASKFORDEPT.SQL VAL1
#HTTP://machine_name.domain:port/ASKFORDEPT.SQL VAL1
Please enter a valid department
For example: 10
Department ID?>
You can enter a department number at the prompt Department ID?>. By default, SQL*Plus lists the line containing &NEWDEPT before and after substitution, and then displays the department name corresponding to the number entered at the Department ID?> prompt. You can use SET VERIFY OFF to prevent this behaviour.
Read full documentation here.
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:
Go to View | DBMS Output to enable the DBMS Output window
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.

learning plsql in sqldeveloper

I just started to learn PLSQL. I had only SQLDEVELOPER to access Oracle Database.
When I run following code:
DECLARE
title VARCHAR(8);
salary NUMBER;
BEGIN
title := 'DBA';
salary := 50000;
DBMS_OUTPUT.PUT_LINE('Job Title: '|| title);
DBMS_OUTPUT.PUT_LINE('Expected Salary'|| TO_CHAR(salary));
END;
/
I get the output "anonymous block completed".
Also, On running the command "set serveroutput on", I get a blank output.
I had other option is to download oracle database express edition and install on the local system.
In SQL Developer there is a menu option View -> Dbms output which you need to follow to set the serveroutput on for your database connection.
Once done, you will see the DBMS Output window pane, you then need to click the green plus "+" icon to enable serveroutput for your connection.
Other than that, you haven't asked a specific question....
put just above this line of code :
SET SERVEROUTPUT ON

Resources