Rudimentary issue: basic PL/SQL console output? [duplicate] - sql

This question already has answers here:
Why no output when PLSQL Anonymous block completes? [duplicate]
(7 answers)
Printing the value of a variable in SQL Developer
(9 answers)
Closed 4 years ago.
I am using SQL Developer and want to output the contents of a variable to the console using DBMS_OUTPUT.PUT_LINE(). I am running the following code that adds the numbers 1 through 5 inclusive but I'm not seeing any output.
SET SERVEROUTPUT ON;
DECLARE
n_counter NUMBER := 5; -- Substitute this variable
n_sum NUMBER := 0;
BEGIN
WHILE n_counter != 0
LOOP
n_sum := n_sum + n_counter;
n_counter := n_counter -1;
END LOOP;
DBMS_OUTPUT.PUT_LINE(n_sum);
END;
Additionally, do you Know of better resources for troubleshooting issues than the incredibly dense Oracle PL/SQL documentation? [similar to Java SE7 API?]

Since you are using SQL Developer, you have a couple of options.
In SQL Developer, go to View | DBMS Output to ensure that the DBMS Output window is visible. In the DBMS Output window, choose the "plus" icon and select the connection that you want to write data to the DBMS Output window. Then run the PL/SQL block in the SQL Worksheet window using the right arrow (Ctrl+Enter in Windows). You'll see the output appear in the DBMS Output window.
Alternately, you can put both the SQL*Plus SET SERVEROUTPUT ON command and the PL/SQL block in the SQL Worksheet and run it as a script (F5 in Windows). That will display the output immediately below the "anonymous block completed" message in the Script Output window.
Note: Dbms Output in Oracle Sql Developer doesn't show null in the output window. It moves to a new line, but until it will return something else than null, you'll not know all the previous nulls are there.

Related

Run an oracle SQL script twice with different parameters

I have an SQL statement in Oracle SQL developer that has some variables:
DEFINE custom_date = "'22-JUL-2016'" --run1
DEFINE custom_date = "'25-JUL-2016'" --run2
SELECT * FROM TABLE WHERE date=&custom_date
The real query is much more complicated and has many more variables and new tables are created from the results of the query. How can I create a script so that the query is executed twice, the first time with the custom date set as in the first line and the second time as in the second line.
In Oracle, the &variable is a "substitution variable" and is not part of SQL; it is part of the SQL*Plus scripting language (understood by SQL Developer, Toad etc.)
The better option, and what you are asking about, is BIND VARIABLES. The notation is :variable (with a colon : instead of &), for example :custom_date.
The difference is that a substitution variable is replaced by its value in the front-end application (SQL Developer in your case) before the query is ever sent to the Oracle engine proper. A bind variable is substituted at runtime. This has several benefits; discussing them is outside the scope of your question.
When you execute a query with bind variables in SQL Developer, the program will open a window where you enter the desired values for the bind variables. You will have to experiment with that a little bit till you can make it work (for example I never remember if a date must be entered with the single quotes or without). Good luck!
Define is used in TRANSACT SQL. To do this Oracle way, You can create anonymus PL/SQL block, similar to this:
DECLARE
p_param1 DATE;
p_param2 NUMBER;
CURSOR c_cur1(cp_param1 DATE,cp_param2 NUMBER)
IS
SELECT * FROM table WHERE date = cp_param1
;
BEGIN
-- Execute it first time
p_param1 := TO_DATE('2016-09-01','YYYY-MM-DD');
FOR r IN c_cur1(p_param1)
LOOP
NULL;
END LOOP;
-- Execute it second time
p_param1 := TO_DATE('2015-10-11','YYYY-MM-DD');
FOR r IN c_cur1(p_param1)
LOOP
NULL;
END LOOP;
END;
And in it, You create cursor with parameters and execute it twice with different parameter.
I do not know why You want to execute this query twice, so the script abowe does nothing with results, but it certainly should execute Your query twice, with different params.

Error: "exact fetch returns more than requested number of rows" [duplicate]

This question already has answers here:
Why no output when PLSQL Anonymous block completes? [duplicate]
(7 answers)
Printing the value of a variable in SQL Developer
(9 answers)
Closed 4 years ago.
I am using SQL Developer and want to output the contents of a variable to the console using DBMS_OUTPUT.PUT_LINE(). I am running the following code that adds the numbers 1 through 5 inclusive but I'm not seeing any output.
SET SERVEROUTPUT ON;
DECLARE
n_counter NUMBER := 5; -- Substitute this variable
n_sum NUMBER := 0;
BEGIN
WHILE n_counter != 0
LOOP
n_sum := n_sum + n_counter;
n_counter := n_counter -1;
END LOOP;
DBMS_OUTPUT.PUT_LINE(n_sum);
END;
Additionally, do you Know of better resources for troubleshooting issues than the incredibly dense Oracle PL/SQL documentation? [similar to Java SE7 API?]
Since you are using SQL Developer, you have a couple of options.
In SQL Developer, go to View | DBMS Output to ensure that the DBMS Output window is visible. In the DBMS Output window, choose the "plus" icon and select the connection that you want to write data to the DBMS Output window. Then run the PL/SQL block in the SQL Worksheet window using the right arrow (Ctrl+Enter in Windows). You'll see the output appear in the DBMS Output window.
Alternately, you can put both the SQL*Plus SET SERVEROUTPUT ON command and the PL/SQL block in the SQL Worksheet and run it as a script (F5 in Windows). That will display the output immediately below the "anonymous block completed" message in the Script Output window.
Note: Dbms Output in Oracle Sql Developer doesn't show null in the output window. It moves to a new line, but until it will return something else than null, you'll not know all the previous nulls are there.

Ad hoc querying Oracle PL/SQL - for SQL Server developer

I used to do Oracle development many many years ago. I have spent most of the past 15 years doing mainly SQL Server, and where I have done Oracle work, been insulated from the workings by Web services etc.
My question seems to have been asked on the web a few times, but it seems difficult somehow to communicate - at least judging by the answers. I very much appreciate that tools and functionality differ, know I have to learn new things, but this is so simple, yet seems so hard to do.
I am looking to do some ad-hoc queries on the database. At the moment we are using SQL Navigator - I am open to using other tools...
In SQL Server Management Studio, if you open a query window, type a bit of SQL that retuns a value or a set, you get a nice display of the rows or values in a results window.
I've gathered that with Oracle PL/SQL things are a bit different, worked out that I need to return a cursor - but how do I get a tool to display the results?
I started simple:
declare
my_id number := 356655;
cursor c1 is select my_id from dual;
begin
open c1;
end;
This runs fine - but how do I see the results? Are there any tools that deal with this as 'nicely' as SSMS? I am used to being able to do a lot of this, including stuff like
(perhaps not exactly the right syntax? but you get the idea...)
declare
my_id number := 356655;
cursor c1 is select name from my_table where id = my_id;
begin
open c1;
And having the results displayed to me as text/grid. Ideally that there is a nice solution. Some spiffy new tool, maybe?
With SQL Developer or SQL*Plus you can use a bind variable declared before the PL/SQL block:
variable rc refcursor;
declare
my_id number := 356655;
begin
open :rc for select my_id from dual;
end;
/
print rc
RC
-------------------------------
356655
You can also use a bind variable within the query, which can be useful:
variable my_id number;
variable rc refcursor;
execute :my_id := 356655;
begin
open :rc for select :my_id from dual;
end;
/
print rc
The variable and print commands are covered in the SQL*Plus documentation, which largely applies to SQL Developer as well - that has its own documentation, including the commands that are carried over from SQL*Plus.
If you have a function that returns a ref cursor then you can call that in a query, as select func(val) from dual, and then the results can go in a grid; or you can call the function (or procedure) with the same :rc bind variable and print it. But I'm not sure either is helpful if you are only doing ad hoc queries.
On the other hand, using a PL/SQL block for an ad hoc query seems a little heavy-handed, even if your queries are complicated. You'd need a good reason to open a cursor for a select statement from within a block, rather than just running the select directly. (Not sure if that's a SQL Server thing or if you actually have a real need to do this!). If you're just running a query inside the block, you don't need the block, even if you want to keep a bind variable for the values you're using in the query:
variable my_id number;
execute :my_id := 356655;
select :my_id from dual;
:MY_ID
----------
356655
I use Oracle SQL Developer.
Anyway, this should work in any oracle sql client:
If you just want to see your results, you can use
dbms_output.put_line('Foo' || somevar || ' bar');
Before this, run
SET SERVEROUTPUT ON
Check the examples at docs.oracle.com
I would suggest using sql developer available free from the oracle website. There is a button which allows you to run sql as a script which will get back what you want. SSMS doesn't work with pl/sql.

PL/SQL, Cannot print variable

I'm trying to learn PL/SQL by simply assigning a variable from a select statement and then, to confirm it's working, print it sql output.
DECLARE ALLOW_STUFF NUMBER;
BEGIN
SELECT VAL_N INTO ALLOW_STUFF FROM MY_TABLE WHERE MY_KEY = 'ALLOW_ME';
DBMS_OUTPUT.PUT_LINE(ALLOW_STUFF);
END;
I'm using SQL Developer and/or SQL PLus. When I run this, all I get is
Anonymous block completed
Rather than than the value of MY_TABLE.VAL_N
You need to enable output, otherwise the DBMS_OUTPUT.PUT_LINE statements are ignored.
Output can be enabled using:
DBMS_OUTPUT.ENABLE();
For more information about DBMS_OUTPUT read Oracle documentation: http://docs.oracle.com/cd/B19306_01/appdev.102/b14258/d_output.htm#i1000634
As stated in the comments also set serveroutput on can be used.

Script doesn't generate any output

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?
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.