How To Enable dbms_output in PL/SQL Developer - oracle

I know it's a simple thing but I can't seem to find a solution, how do I enable dbms_output in PL/SQL Developer. From googling all I got was how to enable it in sql developer but thats not what I'm looking for...
I thought it should be automatically enabled in PL/SQL Developer but for example this block outputs nothing for me.
declare
v_sample employees.first_name%type;
begin
select first_name
into v_sample
from employees
where employee_id = 100;
dbms_output.put_line(v_sample);
end;

By default, PL/SQL Developer automatically retrieves DBMS_OUTPUT and you don't have to do anything extra to retrieve it. You do not need to use a Command Window to see output. (And you should generally avoid the Command Window - it's a terrible way to program.)
Go to Configure --> Preferences --> Oracle --> Output, and ensure that the "Enabled" button is checked:
Another possibility is that the FIRST_NAME value is null and there is nothing to output. To avoid that confusion, I typically add a hard-coded value before outputting variables:
dbms_output.put_line('Name: ' || v_sample);

Related

Debugging PL/SQL Procedure for reservation service report

For a school project, unable to figure out how to debug the code, I've managed to figure out that the problem is in this line of code: Reservation_id:=: reservation_ID; but I am unsure how to fix it
Set serveroutput ON;
Create or replace procedure Reservation_Services_Report (reservation_ID IN number)
As
Service_number reservation.service_type_id%type;
People_attending reservation.numb_people_attend%type;
Begin
Reservation_id:=: reservation_ID;
Select s.service_name, s.service_type, s.service_type_food, s.service_type_entertainment, r.numb_people_attend from services s, reservation r
where services.service_type_id = reservation.service_type_id;
Exception
When no_data_found then
dbm_output.put_line(‘No services for this reservation’);
End;
Oh, it only it were your only problem!
as you declared an IN parameter, why don't you use it? It is here to be passed to the procedure, not to accept it at runtime (which is what you planned to do with preceding its name with a colon).
besides, it is a good idea to distinguish parameter name from column name, otherwise you'll have problems as Oracle won't know which is which. That's why I renamed it to par_reservation_id
select in PL/SQL requires into clause. In your case, you need to declare all those variables which match select column list. I named them all with the l_ prefix (as a l_ocal variable); some people use v_; pick whichever you want, just try to stick to some standards - it'll make your code easier to read, debug and maintain
select you wrote would probably return too_many_rows error as there's no restriction to number of rows; I presume you "forgot" to include the where clause so I put it there; I don't know whether it is correct or not as I don't have your tables. Fix it, if necessary
when joining tables, try to switch to modern ANSI syntax and separate joins from (where) conditions, as I tried
unless you're running that code in a tool that supports dbms_output to be displayed on the screen, you won't see anything. Exception is handled (kind of), but nobody will know what happened. Consider raising the error instead, e.g. raise_application_error(-20000, 'No services for this reservation')
Here's how the procedure might look like; hopefully, it is somewhat better than the original version. See if it helps.
Create or replace procedure
Reservation_Services_Report (par_reservation_ID IN number)
As
l_service_name services.service_name%type;
l_service_type services.service_type%type;
l_service_type_Food services.service_type_Food%type;
l_service_type_entertainment services.service_type_entertainment%type;
l_numb_people_attend reservation.numb_people_attend%type;
Begin
Select s.service_name,
s.service_type,
s.service_type_food,
s.service_type_entertainment,
r.numb_people_attend
into l_service_name,
l_service_type,
l_service_type_food,
l_service_type_entertainment,
l_numb_people_attend
from services s join reservation r on s.service_type_id = r.service_type_id
where r.reservation_id = par_reservation_id;
Exception
When no_data_found then
dbms_output.put_line(‘No services for this reservation’);
End;

how to compile all procedures with standard format in oracle sql developer

I have a lot of procedures and functions in a schema in oracle SQL developer,
I want to know how to compile all procedures and functions with standard format (that after that all of them have the same format like when press Ctrl + F7 manually) in oracle SQL developer automatically?
I have a lot of procedures and functions in a schema in oracle SQL developer, I want to know how to compile all procedures and functions
In the "Connections" view:
expand the connection to the schema
right click on "Procedures" (or "Functions")
in the context menu that pops up, chose "Compile All"
if you wish, you can view the PL/SQL block that is going to be run by looking at the "SQL" tab
press the "Apply" button to recompile everything.
I want to know how to [have] all procedures and functions with standard format
This is nothing to do with (re)compiling. You can apply whatever formatting (whitespace/case/etc.) rules you want to your code and so long as the code remains syntactically correct then it does not affect whether the code will recompile.
Go to "Tools" > "Preferences..." > "Database" > "SQL Formatter" and edit the appropriate formatting to your specification.
Then right-click on the procedure's/function's code and select "Format" (or press Ctrl + F7).
You will need to do this for each procedure and function as there does not appear to be a SQL Developer option to apply it to all objects in a schema.
Alternatively, you may use a public synonym referencing a procedure to compile through the DB by commands, created and authorized as below :
$ sqlplus / as sysdba
SQL> Create or Replace Procedure SYS.Pr_Compile_All Is
v_command varchar2(1500);
Begin
For c in
(
Select 'alter '||o.object_type||' '||o.owner||'.'|| o.object_name|| ' compile' command1,
'alter PACKAGE '||o.owner||'.'|| o.object_name|| ' compile' command2,
'alter PUBLIC SYNONYM '|| o.object_name|| ' compile' command3,
object_type,
owner
From dba_objects o
Where o.status = 'INVALID'
)
Loop
Begin
v_command := c.command1;
If c.object_type in ('FUNCTION','PROCEDURE','TRIGGER') Then v_command := v_command ||' debug'; End If;
If c.object_type in ('PACKAGE BODY') Then v_command := c.command2||' debug body'; End If;
If c.object_type in ('SYNONYM') and c.owner = 'PUBLIC' Then v_command := c.command3; End If;
Execute Immediate v_command;
Exception When Others Then null;
End;
End Loop;
End;
SQL> Create or Replace Public Synonym Pr_Compile_All For SYS.Pr_Compile_All;
SQL> grant execute on Pr_Compile_All to public;
SQL> conn myschema/pwd
SQL> begin Pr_Compile_All end; -- call from any schema you'd like, in this way.
The 'best' way to look at this is via source control, and hopefully the source of truth is a subversion or Git project.
You can feed all of the files in a directory to our CLI with the FORMAT command. It will then go through each file in that folder, format the code, and write it to the supplied output directory.
You would then check those files in to your source control system.
c:\Program Files\Oracle\sqldev\18.1\sqldeveloper\sqldeveloper\bin>sdcli format input=c:\users\jdsmith\unformatted output=c:\users\jdsmith\formatted
Command Completed.
So here I go from 3 files unformatted to 3 files formatted, and if I open the same 'object' before and after...
All this is nice, but know as soon as another developer checks out a file, they will immediately change the way it looks due to personal preferences. I'm not sure I've ever seen a successful 'formatting rules' system where everyone agrees to format the code the same. But, formatting it as it goes in your VCS seems to work OK...and will also help with DIFFs/Deltas.
You could also theoretically also write some js and use SQLcl to grab each object, format it, and then compile it. Some examples are here.
I don't like the idea of compiling objects w/o looking at them first, but that's just me.
The best way to solve this problem in oracle 19c/18c (tested) is to run this script on your DB server machine:
SQL> #Oracle_home/rdbms/admin/utlrp.sql
The link to the reference is here.

Oracle PL/SQL Trigger asking for binds

I'm trying to create my trigger but it's asking for binds EVERY time. It works the way I want it to when I click apply on the window that appears... However, it will log an error...
My trigger checks to see if a client is active or not and do NOT allow changes if it is found to be active...
CREATE Trigger Client_Activity
BEFORE Insert or Update or Delete ON Client
FOR EACH ROW
DECLARE
VAR_AC char(2);
BEGIN
IF UPDATING THEN
SELECT Activity INTO VAR_AC
FROM Client_Additionals
WHERE Activity = :Old.Activity;
IF Activity = 'AC'
THEN Raise_Application_Error(-20999, 'active')
END IF;
END;
/
ORACLE VERSION 12 USING SQLDEVELOPER
You have two syntax errors in your trigger:
The IF is missing an END IF
You need to compare the content of the variable var_ac
You are missing a ; after the Raise_Application_Error()
Putting that together, you can create the trigger without problems.
However, you need to use the "Run Script" button in SQL Developer to run a PL/SQL block like that.
SQL*Plus requires no special handling:

Creating a trigger in Oracle Express

I was trying to do something like auto-increment in Oracle 11g Express and SQL Developer.
I know very little about Oracle and I am also new to triggers.
I tried running this, but I don't know how to do it properly.
CREATE TABLE theschema.thetable
(id NUMBER PRIMARY KEY,
name VARCHAR2(30));
CREATE SEQUENCE theschema.test1_sequence
START WITH 1
INCREMENT BY 1;
create or replace trigger insert_nums
before insert on theschema.thetable
for each row
begin
select test1_sequence.nextval into :new.id from dual;
end;
/
When I try to create the trigger, I get a screen which asks me for some "binds".
The dialog box has only one check box "null". What does this mean and how do I make
a script that works properly?
Any precautions to take while doing this kind of "auto-increment" ?
It seems that SQL Developer thinks that you are running a plain DML (data manipulation) script, not a DDL (data definition). It also thinks that :new.id is a bindable variable.
Why this happens, I don't know; I can't reproduce it in Oracle SQL Developer 2.1.
Try to open a new SQL worksheet window in the theschema schema and execute a "whole" script (not a statement) by pressing F5 (not F9).
This is how I have solved this problem, put "set define off;" before the command:
set define off;
create or replace trigger [...]
[...]
end;
/
Then highlight both commands and press F9 to run.
Or you could run all the commands with F5.
It seems, that if the commands are executed separetly with F9, then the set define off does not take affect.
For my case, solution was entering "newrow" for 'new' and "oldrow" for 'old' as values for the binds...
I am a novice at this so keep that in mind as I give my answer.
I think the issue is that the code
create or replace trigger insert_nums
before insert on theschema.thetable
for each row
begin
select test1_sequence.nextval into :new.id from dual;
end;
Is actually a script and not straight forward SQL statement. Hence you have to run the "Run Script". I discovered that when I had a worksheet open in SQL Developer that if I had anywhere in the worksheet the any code for trigger like above then even I just tried to run a statement that SQL Developer would look back in the worksheet and try to run the script. To stop that from happening I had to comment out the code.
And If I did want to run the code for the trigger than I had to open a new worksheet, place the code there and do a RUN SCRIPT.

Calling Oracle package procedures which return ref cursors in straight PL/SQL

I've got an Oracle 10g database which is accessed from an ASP.NET application. Although I've used SQL Server heavily in many different aspects and Oracle for querying and reporting, this is my first time using Oracle as the OLTP database for an application.
The database-level procedures in the packages are typically of the form:
-- TYPE refcur IS REF CURSOR;
PROCEDURE get_some_stuff(o_cursor OUT refcur, p_param1 IN INTEGER, p_param2 IN INTEGER) IS
BEGIN
OPEN o_cursor FOR
SELECT whatever
FROM whatever
END
I assume these are done this way for the benefit of the ADO.NET layer able to use the cursor from the output param and it is my understanding that this is the acceptable best practice for calling Oracle procs from .NET.
In SQL Server, for example, we don't have explicit ref cursors, if a proc returns a result set (or several result sets), that's accessible as an output result set in both ADO.NET and SSMS, and you can simply test the SPs by doing EXEC spname param1, param2.
The problem I'm having is that I don't know how to call these directly in SQL in Toad, for example, to be able to test changes at the PL/SQL level first before going to the app. I'm very used to being able to exercise and even re-mix stored procs and functions in SQL Server to be able to refactor the database interface layer without affecting the external interface to application-level code.
look at the link that OMG Ponies posted, but what you can do is
var x refcursor;
declare
PROCEDURE GET_SOME_STUFF(O_CURSOR OUT SYS_REFCURSOR, P_PARAM1 IN NUMBER, P_PARAM2 IN NUMBER) IS
BEGIN
OPEN O_CURSOR FOR
SELECT LEVEL, p_param1 ,P_PARAM2 FROM DUAL CONNECT BY LEVEL < 3;
END ;
BEGIN
GET_SOME_STUFF(:x , 5, 10);
END;
/
PRINT X;
you pretty much just wrap it in a anonymous block ad it will run. I use SQL Developer (highly recommmend, free with plenty of support) or SQL plus so I cannot help with TOAD, but I would expect it to be the same. In SQL Developer (and in SQL Navigator if memory serves correct) you can simply right click the package/method you wish and it will create the script for you.
in toad and navigator I believe you may be able to get the ref cursor in a pretty grid while in developer you get it in text.
SQL Developer you can unit test as well
Try this:
DECLARE
aCursor SYS_REFCURSOR;
someVariable SOME_TYPE;
FUNCTION SOME_FUNC_RETURNING_A_CURSOR RETURN SYS_REFCURSOR IS
csrLocal SYS_REFCURSOR;
BEGIN
OPEN csrLocal FOR SELECT whatever FROM wherever;
RETURN csrLocal;
END SOME_FUNC_RETURNING_A_CURSOR;
BEGIN
aCursor := SOME_FUNC_RETURNING_A_CURSOR;
WHILE TRUE LOOP
FETCH aCursor INTO someVariable;
EXIT WHEN aCursor%NOTFOUND;
...do whatever with variables...
END LOOP;
COMMIT;
END;
Share and enjoy.
I found an easier way to this ...try it (This will also generate script for you)
In the Procedure Editor, load your procedure. Click on the lightning
bolt to execute and you will see the Set Parameters window, which is
also available via the button on the Proc Editor toolbar that has an
image similar to (...) on it, next to the lightning bolt. Click on the
output options button and you'll see your options. If this is a weak ref
cursor then you must use the in-memory grid option. Results go to the
cursor results tab at the bottom of the PE after you execute.
http://toad.10940.n7.nabble.com/display-ref-cursor-in-toad-td1427.html

Resources