DBMS_OUTPUT.PUT_LINE not printing
Asked Answered
C

9

140

When executing the following code, it just says the procedure is completed and doesn't print the infomation i want it to (firstName, lastName) and then the other values from the select query in a table below.

 CREATE OR REPLACE PROCEDURE PRINT_ACTOR_QUOTES (id_actor char)
AS
CURSOR quote_recs IS
SELECT a.firstName,a.lastName, m.title, m.year, r.roleName ,q.quotechar from quote q, role r,   
rolequote rq, actor a, movie m
where
rq.quoteID = q.quoteID
AND
rq.roleID = r.roleID
 AND
r.actorID = a.actorID
AND
r.movieID = m.movieID
AND
 a.actorID = id_actor;
BEGIN
FOR row IN quote_recs LOOP
DBMS_OUTPUT.PUT_LINE('a.firstName' || 'a.lastName');

end loop;
END PRINT_ACTOR_QUOTES;
/ 

When setting server output on, I get

a.firstNamea.lastName
a.firstNamea.lastName
a.firstNamea.lastName
a.firstNamea.lastName

multiple times!

Consueloconsuetude answered 3/5, 2012 at 15:24 Comment(0)
P
239

What is "it" in the statement "it just says the procedure is completed"?

By default, most tools do not configure a buffer for dbms_output to write to and do not attempt to read from that buffer after code executes. Most tools, on the other hand, have the ability to do so. In SQL*Plus, you'd need to use the command set serveroutput on [size N|unlimited]. So you'd do something like

SQL> set serveroutput on size 30000;
SQL> exec print_actor_quotes( <<some value>> );

In SQL Developer, you'd go to View | DBMS Output to enable the DBMS Output window, then push the green plus icon to enable DBMS Output for a particular session.

Additionally, assuming that you don't want to print the literal "a.firstNamea.lastName" for every row, you probably want

FOR row IN quote_recs
LOOP
  DBMS_OUTPUT.PUT_LINE( row.firstName || ' ' || row.lastName );
END LOOP;
Porcine answered 3/5, 2012 at 15:30 Comment(16)
Sorry, when executing the procedure above the message is PL/SQL procedure successfully completed. I am using SQL plusConsueloconsuetude
@Consueloconsuetude - OK. Then you just need to add the set serveroutput on command before executing the procedure in SQL*Plus.Porcine
@Consueloconsuetude - Right. See my comment at the end-- if you don't want that literal for every row, you'll probably want the syntax I posted at the end of my answer.Porcine
Ok so now it displays the correct names but like 100times. how would i display title, year, rolename, quote in a table below instead of the 100s of name that appearsConsueloconsuetude
@Consueloconsuetude - If you are stating that each name is repeated hundreds of times, and that is not what you expect, that implies that your SELECT statement is incorrect and is probably missing a join condition. You'll want to refine your query (which you can run separately without the procedure) until the query returns the data you expect.Porcine
thanks. How would i display the other values?thanks for your helpConsueloconsuetude
@Consueloconsuetude - You'd just concatenate more data to the string you build and send to dbms_output. For example dbms_output.put_line( row.firstName || ' ' || row.lastName || ' ' || row.title || ' ' || row.year ). Of course, in reality, you wouldn't rely on dbms_output being enabled-- you'd use dbms_output for debugging whatever your procedure is really supposed to do.Porcine
Yes I would rather the table be outputted from a select statement in the loop. is that possible? As i want the headings of each column to show also. Thanks againConsueloconsuetude
@Consueloconsuetude - You could return a SYS_REFCURSOR and the caller could then read from the ref cursor to display the data. Or you could create a view rather than a stored procedure and just query the view. Or you could just execute the SQL statement.Porcine
I have executed the SQL statement but the table doesnt appear.Consueloconsuetude
@Consueloconsuetude - I'm sorry, I don't understand. Are you saying that you typed the SELECT statement at the SQLPlus command prompt, the SQL statement executed, data was returned, but the data was not displayed in SQLPlus? That seems unlikely unless you were playing with autotrace settings in which case you might have seen a query plan and execution statistics rather than the query results. We're starting to get rather far from your original question, however.Porcine
Maybe interesting for someone: I still didn't get the output with SQL Developer using this manual. After some time I found the reason. community.oracle.com/thread/4062562 There is a bug if you use an old database (Oracle 10g) with the current version of the SQL Developer.Maighdiln
Is there any way to view array elements by index in any of the debug window?Unbidden
@Unbidden - What do you mean by “array elements”? Like a specific line of the ‘dbms_output’ buffer?Porcine
@JustinCave No. I mean string or numeric array, result of a split function. My pl/sql block splits '1,2,3,A,B,C,@,#' by comma resulting in an array of characters, I declare an array of integer values like 10,20,30,40. How can I view these elements in any debug window (Data, Smart Data, ...) without printing these elements?Unbidden
@Unbidden - That's probably worth posting a separate question. Which IDE are you using-- SQL Developer? Or something else?Porcine
R
40
  1. Ensure that you have your Dbms Output window open through the view option in the menubar.
  2. Click on the green '+' sign and add your database name.
  3. Write 'DBMS_OUTPUT.ENABLE;' within your procedure as the first line. Hope this solves your problem.
Remontant answered 23/5, 2016 at 7:29 Comment(2)
Which application are these instructions intended for?Discontinuation
@Discontinuation A bit late but its for sqldeveloper. lol : )Selfappointed
C
38

Set Query as below at first line

SET SERVEROUTPUT ON 
Chlordane answered 3/9, 2018 at 11:40 Comment(3)
even its not working .. Please provide any other alternativeCupule
Just to be extra clear. The line Sreenath S has suggested goes first and is outside all code blocks like DECLARE and BEGIN/END. I tried making it first in my DECLARE block which does not work. I am using SQL*Plus.Melcher
This should be the first step .. then next things.. will work.Clockmaker
M
20

enter image description here

In Oracle SQL Developer, you can follow steps by steps as the below image:

Maag answered 3/1, 2022 at 10:19 Comment(0)
S
19

this statement

DBMS_OUTPUT.PUT_LINE('a.firstName' || 'a.lastName');

means to print the string as it is.. remove the quotes to get the values to be printed.So the correct syntax is

DBMS_OUTPUT.PUT_LINE(a.firstName || a.lastName);
Spiegelman answered 9/5, 2012 at 5:59 Comment(0)
E
13

For SQL Developer

You have to execute it manually

SET SERVEROUTPUT ON 

After that if you execute any procedure with DBMS_OUTPUT.PUT_LINE('info'); or directly .

This will print the line

And please don't try to add this

 SET SERVEROUTPUT ON

inside the definition of function and procedure, it will not compile and will not work.

Erased answered 31/12, 2020 at 10:12 Comment(0)
D
3

I am using Oracle SQL Developer,

In this tool, I had to enable DBMS output to view the results printed by dbms_output.put_line

You can find this option in the result pane where other query results are displayed. so, in the result pane, I have 7 tabs. 1st tab named as Results, next one is Script Output and so on. Out of this you can find a tab named as "DBMS Output" select this tab, then the 1st icon (looks like a dialogue icon) is Enable DBMS Output. Click this icon. Then you execute the PL/SQL, then select "DBMS Output tab, you should be able to see the results there.

Dali answered 25/1, 2019 at 5:21 Comment(0)
L
0

All of them concentrate on the for loop but if we use a normal loop then we have to use the cursor record variable. The following is the modified code

 CREATE OR REPLACE PROCEDURE PRINT_ACTOR_QUOTES (id_actor char)
    AS
    CURSOR quote_recs IS
    SELECT a.firstName,a.lastName, m.title, m.year, r.roleName ,q.quotechar from quote q, role r,   
    rolequote rq, actor a, movie m
    where
    rq.quoteID = q.quoteID
    AND
    rq.roleID = r.roleID
     AND
    r.actorID = a.actorID
    AND
    r.movieID = m.movieID
    AND
     a.actorID = id_actor;
    recd quote_recs%rowtype;
    BEGIN
    open quote_recs;
    LOOP
    fetch quote_recs into recs;
    exit when quote_recs%notfound;
    DBMS_OUTPUT.PUT_LINE(recd.firstName||recd.lastName);
    end loop;
    close quote_recs;
    END PRINT_ACTOR_QUOTES;
    / 
Lavinia answered 2/9, 2014 at 7:25 Comment(0)
B
0

Maybe you are seeing the output in the script output window. Open Dbms Output window from View menu, and then click the green plus icon and must choose your connection name and press ok. Also make sure to chose the correction connection name in the query window.

Bulldoze answered 1/12, 2023 at 7:56 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.