0

I'm using the Oracle Developer Days image to get started with PL/SQL in SQL Developer.

Right now I have the following PL/SQL anonymous block:

DECLARE
  v_country_name VARCHAR2(40);
  v_region_id NUMBER;

BEGIN
  SELECT country_name, region_id
  INTO v_country_name, v_region_id
  FROM countries 
  WHERE country_id = 'CA';

  DBMS_OUTPUT.PUT_LINE('The country name is: ' || v_country_name ||
                        ' and is located in ' || v_region_id || '.');

EXCEPTION
  WHEN TOO_MANY_ROWS THEN
  DBMS_OUTPUT.PUT_LINE('Your SELECT statement retrieved multiple rows. ' ||
                        ' Consider using a cursor.');

END;

This executes, but it doesn't do what I want it to do. The Script Output only contains 'anonymous block completed'.

I have tried to explicitly enable the output with DBMS_OUTPUT.enable; but this didn't make a difference.

What am I overlooking?

3
  • 2
    Did you call, "set serveroutput on" from your sqlplus session before executing your script? Commented Jan 2, 2014 at 18:08
  • 3
    Are you looking in the dbms_output tab? Commented Jan 2, 2014 at 18:12
  • @OldProgrammer & phantom: Thanks, your comments have helped. I now have two ways of viewing my output :) Commented Jan 2, 2014 at 18:16

1 Answer 1

1

If you're running it as a script you can add set serveroutput on before your declare, which will show the output in the 'Script Output' window. This will enable output for the remainder of your session, or until you turn it off again.

If you're running it as a statement rather than a script, and don't already have (or don't want) serveroutput on, you can attach SQL Developer to the output. From the View menu choose 'DBMS Output', which opens a new panel with the same name. Running your code again will still produce nothing at this point.

Click the green plus sign (+) and choose your connection from the list. The next time you run it you will see the output. The first time it will probably show multiple versions as the previous runs will have stored the output in a buffer on the server, and they will all be retrieved. Subsequent runs will just show the new output generated by each run.

There's more in the SQL Developer documentation; and there is background in the PL/SQL packages and types reference, which you've probably already seen. Personally I only ever use the script output, partly because it's quicker, partly because it puts mixed output from SQL and PL/SQL in one place (if out of step), but mostly from habit and to maintain script compatibility with SQL*Plus.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.