109

I am using Oracle SQL (in SQLDeveloper, using the SQL Worksheet). I would like to print a statement before my select, such as

PRINT 'Querying Table1';
SELECT * from Table1;

What do I use to Print / show text output? It's not Print, because that gives me the error: Bind Variable Table1 is NOT DECLARED. DBMS_OUTPUT.PUT_LINE is an unknown command. (Obviously, I'm an inexperienced SQLDeveloper and Oracle user. There must be some synonym for Print, but I'm having trouble finding help on it without knowing what it is.)

Srini V
  • 11,045
  • 14
  • 66
  • 89
thursdaysgeek
  • 7,696
  • 20
  • 78
  • 115

8 Answers8

161

enter image description here

for simple comments:

set serveroutput on format wrapped;
begin
    DBMS_OUTPUT.put_line('simple comment');
end;
/

-- do something

begin
    DBMS_OUTPUT.put_line('second simple comment');
end;
/

you should get:

anonymous block completed
simple comment

anonymous block completed
second simple comment

if you want to print out the results of variables, here's another example:

set serveroutput on format wrapped;
declare
a_comment VARCHAR2(200) :='first comment';
begin
    DBMS_OUTPUT.put_line(a_comment);
end;

/

-- do something


declare
a_comment VARCHAR2(200) :='comment';
begin
    DBMS_OUTPUT.put_line(a_comment || 2);
end;

your output should be:

anonymous block completed
first comment

anonymous block completed
comment2
Community
  • 1
  • 1
Perry Tribolet
  • 2,624
  • 1
  • 21
  • 9
  • 1
    First line set serveroutput on format wraped; should be set serveroutput on format WRAPPED; – geographika Apr 16 '10 at 13:24
  • I can't get this to work. `set serveroutput on format word_wrapped; begin dbms_output.put_line('hello world'); end; select * from dual` Gives me: `select * from dual; Error report: ORA-06550: line 7, column 1: PLS-00103: Encountered the symbol "SELECT" 06550. 00000 - "line %s, column %s:\n%s" *Cause: Usually a PL/SQL compilation error. *Action ` – dwjohnston Apr 15 '13 at 21:43
  • 14
    In newer versions of SQL Developer you first have to open the DBMS Output window using the topleven menu bar: View > Dbms Output. – Bruno Ranschaert Jul 30 '14 at 13:58
  • could've but the screen shot and no text, would have been a +1 haha – David Mann Aug 25 '14 at 17:09
52
PROMPT text to print

Note: must use Run as Script (F5) not Run Statement (Ctl + Enter)

H77
  • 5,859
  • 2
  • 26
  • 39
23

You could set echo to on:

set echo on
REM Querying table
select * from dual;

In SQLDeveloper, hit F5 to run as a script.

Eddie Awad
  • 3,669
  • 1
  • 19
  • 17
  • I had accepted this as an answer, but I see another answer has a lot more points, so I changed my accept to that. However, this did work for me, and it's a good answer. – thursdaysgeek Jan 25 '14 at 00:48
23

The main answer left out a step for new installs where one has to open up the dbms output window.

enter image description here

Then the script I used:

dbms_output.put_line('Start');

Another script:

set serveroutput on format wrapped;
begin
    DBMS_OUTPUT.put_line('jabberwocky');
end;
ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
16

You could put your text in a select statement such as...

SELECT 'Querying Table1' FROM dual;
Leigh Riffel
  • 6,381
  • 3
  • 34
  • 47
7

For me, I could only get it to work with

set serveroutput on format word_wrapped;

The wraped and WRAPPED just threw errors: SQLPLUS command failed - not enough arguments

Michael Erickson
  • 3,881
  • 2
  • 20
  • 16
3

If I ommit begin - end it is error. So for me this is working (nothing else needed):

set serveroutput on;
begin
DBMS_OUTPUT.PUT_LINE('testing');
end;
FrenkyB
  • 6,625
  • 14
  • 67
  • 114
1

If you don't want all of your SQL statements to be echoed, but you only want to see the easily identifiable results of your script, do it this way:

set echo on

REM MyFirstTable

set echo off

delete from MyFirstTable;

set echo on

REM MySecondTable

set echo off

delete from MySecondTable;

The output from the above example will look something like this:

-REM MyFirstTable

13 rows deleted.

-REM MySecondTable

27 rows deleted.

Community
  • 1
  • 1