This code compiles, but in TOAD it won't show the "hi wo" output
CREATE OR REPLACE PROCEDURE AdelTest IS
tmpVar NUMBER;
BEGIN
DBMS_OUTPUT.ENABLE(100: in INTEGER);
DBMS_OUTPUT.PUT_LINE('hi wo');
tmpVar := 0;
EXCEPTION
WHEN NO_DATA_FOUND THEN
NULL;
WHEN OTHERS THEN
--consider logging error then r-raise
RAISE;
END AdelTest;
How do I show the output(similar to how println shoots to console in Java ) ?
You need to enable DBMS Output. If working in the Editor right-click and choose DBMS Output off of the Desktop flyout menu. There's a button that is red if it's disabled. Click it and it will turn green. Then execute your code. Output will display there. If you are working outside of the Editor (in Schema Browser for instance) select DBMS Output off of the main Toad View menu. Enable your output there. Output will display in that window.
You can also view server output if for some reason DBMS output window is not getting enable. use QSR editor embedded within toad for oracle edition..
How to use
select the code in place
Editor > Execute SQL via QSR
have the set serveroutput on as first statement in QSR window and run..
now it will show output in window..
Hope it helps!!!
In the Editor right-click and choose DBMS Output off of the Desktop
flyout menu. There's a button that is red if it's disabled. Click it
and it will turn green. Then execute your code.
Need to enable the Automatically poll for output after execution under DBMS Output
DECLARE
v_text VARCHAR2(20); -- declare
BEGIN
v_text := 'Hello World'; --assign
dbms_output.Put_line(v_text); --display
END;
Related
I'm working on a solution where several SQL and PL/SQL scripts are being run together, in a batch of sorts, via SQL*Plus.
I'm declaring SET ECHO OFF; and SET ECHO ON; at relevant points in the scripts so as to output relevant code.
Currently the output looks something like this:
SQL> DECLARE
2 ct number := 0;
3 ctChanges number := 0;
4
5 BEGIN
6 select count(*) into ct from ...
7 (...rest of code block...)
"some specific status message"
Commit executed.
We keep this output as a run-log in our build-environment, but can also access it as a plain text file.
One downside of this format however, is that if I'd like to copy a certain section of the code and run it again in an IDE (like Toad or SQL Developer), it's hard to exclude the line numbers.
Is it possible to tell SQL*Plus to output the code as above, but without including the line numbers?
You can use options sqlnumber and sqlprompt:
set sqlprompt ''
set sqlnumber off
SET SQLN[UMBER] {ON|OFF}
SET SQLNUMBER is not supported in iSQL*Plus
Sets the prompt for the second and subsequent lines of a SQL command or PL/SQL block. ON sets the prompt to be the line number. OFF sets the prompt to the value of SQLPROMPT.
I am working on a PL/SQL script that will take a user decision to either display the results in the SQL Plus window, or export it to a .CSV file based on a user input:
ACCEPT output_location PROMPT 'Output: (S) -SQL Output to Screen (E) - Export to .CSV: '
!touch /my_path/My_output_file.csv
DECLARE
(list of variables);
CURSOR my_cursor IS ...;
BEGIN
IF '&output_location' = 'E' THEN
SPOOL $PWD/My_Output_File
OPEN my_cursor;
FETCH my_cursor into...;
WHILE (my_cursor%FOUND) LOOP
DBMS_OUTPUT.PUT_LINE(my output data);
FETCH my_cursor into...;
END LOOP;
CLOSE my_cursor;
SPOOL OFF
!cp $PWD/My_output_file.lst $PWD/My_output_file.csv
set define ">"
!/libreoffice/program/soffice.bin
set define "&"
ELSE IF '&output_location' = 'S' THEN
(Code to output cursor info as a normal SQL execution)
END IF;
END;
/
Everything between OPEN my_cursor and CLOSE my_cursor works if I move all of the spool and file opening commands outside of the BEGIN-END block. As it currently is, it gives me the error:
PLS-00181: unsupported preprocessor directive '$PWD'
I can move these commands and just have two distinct scripts for a user to run, but I would really prefer to have it all in a one stop shop. How do I split this one script into two different output modes?
This is kind of missmatch. You're mixing PL/SQL with bash commands.
!cp $PWD/My_output_file.lst $PWD/My_output_file.csv
set define ">"
!/libreoffice/program/soffice.bin
set define "&"
This is not valid PL/SQL and it won't work here. You have to either split your scripts or instead of using bash use utl_file package for creating and handling files.
As we know in MSSQL we can write below line to print some message
print 'Some Message';
How we can do same in Oracle as print not working with Oracle?
Use
DBMS_OUTPUT.put_line('Some Message');
To expand on #mhasan's answer and #AlexPoole's comment: assuming that you're executing your script using SQL*Plus you'll need to add the beginning of your script before the first DECLARE or BEGIN:
SET SERVEROUTPUT ON SIZE 1000000
SET LINESIZE 255
If you're using a tool other than SQL*Plus to run your script there will be different ways to view output written to DBMS_OUTPUT. For example, in PL/SQL Developer a "Test" window has a "DBMS Output" tab where text written to DBMS_OUTPUT can be viewed after the test script terminates.
Share and enjoy.
DECLARE
V_NUMBER NUMBER :=23;
BEGIN
LOOP
V_NUMBER:=V_NUMBER+1;
EXIT WHEN V_NUMBER:=25;
--Some kind of function to be applied for printing and nesting lines into CSV or TEXT file.
END LOOP;
COMMIT;
END;
Scripting an Oracle SQL Query for Creating a CSV or Text Typed File Output
Consider running this from a SQL Plus session and use the SPOOL command. All output of the SQL command that follows will be written to the file name you specify.
If you need to append your results each successive time the SQL commands are run, then an OS level command would work appropriately when invoking this sqlplus executable block of PL/SQL:
Where the file name of this script is: "sample_csv_out.sql"
DECLARE
v_total_columns constant number:= 3; -- Number of columns queried
v_column_counter number;
v_csv_record varchar2(1000);
c_csv_column_format constant varchar2(15):=
'<<COLUMN1_VAL>>,<<COLUMN2_VAL>>,<<COLUMN3_VAL>>';
cursor result_cur is
SELECT column1, column2, column3
FROM tablea
WHERE column1 = ... ;
BEGIN
v_csv_record:= 'COLUMN1,COLUMN2,COLUMN3';
dbms_output.put_line (v_csv_record);
FOR i in result_cur LOOP
v_csv_record:= replace(c_csv_column_format, '<<COLUMN1_VAL>>', i.column1);
v_csv_record:= replace(v_csv_record, '<<COLUMN2_VAL>>', i.column2);
v_csv_record:= replace(c_csv_record, '<<COLUMN3_VAL>>', i.column3);
dbms_output.put_line(v_csv_record);
END LOOP;
END;
So, for example in a WINDOWS O/S environment, the call to append the output to a specific file name would be:
C:\> sqlplus sample_csv_out.sql >> mycsv_out.csv
The >> notation instructs the operating system to pipe the output of running sample_csv_out.sql via a sqlplus session.
The command DBMS_OUTPUT does the rest. If you need more details, see more Oracle documentation on DBMS_OUTPUT.
COMMENTS: I chose the RECORD STRING TEMPLATE approach to make this script a little more flexible and reusable. I recommend to keep any data manipulation logic within the CURSOR statement. Often when the two are mixed, it gets harder to debug any typos in syntax within a long string of values.
The construction of an output record was also designed to reduce typos, mistakes and frustration... if there are more than 3 columns in your own scripts, adding another element to the output string is mostly a cut-and-paste operation. Likewise with the "header" row (column titles).
You can read and write files in PL/SQL using the UTIL_FILE package
http://docs.oracle.com/cd/B19306_01/appdev.102/b14258/u_file.htm
I've got a PL/SQL-Block which looks like this:
declare
L_Count number := 10;
begin
if L_Count > 0then
dbms_output.put_line('l_Count > 0');
else
dbms_output.put_line('l_Count <= 0');
end if;
exception
when others then
dbms_output.put_line('exception occurred');
end;
Note the fourth line containing 0then instead of 0 then.
Using PL/SQL-Developer, I can execute this block as an SQL statement, which actually outputs l_Count > 0. Using a "Program Window" and compiling this, PL/SQL-Developer says the following error:
Unable to perform operation due to errors in source code
How can this statement execute but not compile?
Thank you for your hints!
The behavior is not what I would expect either. However, both the PL/SQL Developer SQL Window and Command Window modes execute this consistently with how SQL*Plus behaves. So, this is either:
Not a bug, or
An Oracle bug but not an Allround Automations bug.
Execution and compilation are two separate things. The code block is an anonynous block and it cannot be compiled. However you can execute the block. Execution would show you l_Count > 0.
Thanks,
Aditya