learning plsql in sqldeveloper - oracle

I just started to learn PLSQL. I had only SQLDEVELOPER to access Oracle Database.
When I run following code:
DECLARE
title VARCHAR(8);
salary NUMBER;
BEGIN
title := 'DBA';
salary := 50000;
DBMS_OUTPUT.PUT_LINE('Job Title: '|| title);
DBMS_OUTPUT.PUT_LINE('Expected Salary'|| TO_CHAR(salary));
END;
/
I get the output "anonymous block completed".
Also, On running the command "set serveroutput on", I get a blank output.
I had other option is to download oracle database express edition and install on the local system.

In SQL Developer there is a menu option View -> Dbms output which you need to follow to set the serveroutput on for your database connection.
Once done, you will see the DBMS Output window pane, you then need to click the green plus "+" icon to enable serveroutput for your connection.
Other than that, you haven't asked a specific question....

put just above this line of code :
SET SERVEROUTPUT ON

Related

"declare" works with sqlplus and sql developer but why not with sql navigator?

I tried to declare/initialize variables on sqlplus and sql developer and it worked.
But when I copy/paste the same code and execute on sql navigator it doesnt work.
Could you tell me why and what I need to change so it works also on sql navigator?
variable g_firstname varchar2(30)
variable g_lastname varchar2(30)
declare
v_firstname varchar2(30);
v_lastname varchar2(30);
begin
v_firstname := 'Tony';
v_lastname := 'Stark';
:g_firstname := v_firstname;
:g_lastname := v_lastname;
end;
/
print g_firstname g_lastname
It is not declare, but variable.
SQL Plus is Oracle's command line tool that allows you to access an Oracle database. SQL Developer is Oracle's GUI tool; as such, it is capable of running number of SQL Plus commands.
However, that's not the case with other tools - they work well with pure SQL or PL/SQL, but not with SQL*Plus commands.
Therefore, remove the first two lines and execute the rest. It should work, though - not the way you'd want it to.

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.

SQL Developer not displaying dbms_output

I am trying to code PL/SQL in SQL Developer but it is not displaying the output. My program is compiling successfully. Here is the code:
set serveroutput on
declare
begin
dbms_output.put_line('Hi');
end;
Please suggest what should I do to display the output? Is there any settings I need to change?
View->Dbms Output->Enable DBMS_OUTPUT for connection->OK
It may not be appearing because you are on an older version of Oracle Database. Make sure you're on 11gR2 or higher.
And then as an alterntative to the previous answer, you can simply
set serveroutput on
declare
begin
dbms_output.put_line('Hi');
end;
And then use the Execute as Script (F5) button on the worksheet toolbar.
Once serveroutput is set to 'on' we'll grab the DBMS_OUTPUT for you in the output panel anytime you run a Script.

Get result rows in sql developer window

I run following procedure in sql developer window to search all oracle tables for specific string and at the end in Statement Output window it says "anonymous block completed" and now can't understand where is result rows, how to get it?
thanks
We have option in SQL Developer
and also you have to execute your procedure as below
SET SERVEROUTPUT ON;
begin
dbms_output.put_line('Testing output');
end;
/
Click ->View->Dbms Output and then click + symbol on Dbms output window. now you can run the procedure and can see output.

How do you create an Oracle Automatic Workload Repository (AWR) report?

How do you create an Oracle Automatic Workload Repository (AWR) report?
To generate AWR report follow below steps :
Take begin snap id
set serveroutput on;
DECLARE
v_snap_id number ;
begin
v_snap_id := DBMS_WORKLOAD_REPOSITORY.CREATE_SNAPSHOT;
dbms_output.put_line(v_snap_id);
end;
/
Run your batch or the program you want to monitor.
Take end snap id
set serveroutput on;
DECLARE
v_snap_id number ;
begin
v_snap_id := DBMS_WORKLOAD_REPOSITORY.CREATE_SNAPSHOT;
dbms_output.put_line(v_snap_id);
end;
/
Go to oracle directory. e.g. in my case
cd C:\oraclexe\app\oracle\product\11.2.0\server\rdbms\admin
go to sqlplus promt
sqlplus dbusername/dbpassword#host:port/dbenv
run #awrrpt command
It will ask for format of the report, default is html.
provide no of days, if you dont remember your snap id
enter begin snap
enter end snap
Give report name and press enter
Your report will be generated in "admin" e.g. in my case
C:\oraclexe\app\oracle\product\11.2.0\server\rdbms\admin
sqlplus into to Oracle as the DBA users. Run the report sql. Answer the questions prompted by the report to narrow down the time period
sqlplus / as sysdba
#$ORACLE_HOME/rdbms/admin/awrrpt.sql
The script will ask you some questions so you get a report for the time period you are interested in.
You can use dbms_workload_repository package without the need to log into the server itself.
For a text report, use e.g.:
select output
from table(dbms_workload_repository.awr_report_text(1557521192, 1, 5390, 5392);
Or to get a HTML report, use awr_report_text() instead.
The first paramter is the DBID which can be obtained using:
select dbid from v$database
The second one is the instance number. Only relevant for a RAC environment.
And the last two parameters are the IDs of the start and end snapshot. The available snapshots can be obtained using:
select snap_id,
begin_interval_time
end_interval_time
from dba_hist_snapshot
order by begin_interval_time desc;
Especially for the HTML return - which returns a CLOB - you must configure your SQL client to properly display the output. In SQL*Plus you would use set long
conn / as sysdba
SQL> #$ORACLE_HOME/rdbms/admin/awrrpt.sql
Specify the Report Type
AWR reports can be generated in the following formats. Please enter the
name of the format at the prompt. Default value is 'html'.
'html' HTML format (default)
'text' Text format
'active-html' Includes Performance Hub active report
Enter value for report_type:
old 1: select 'Type Specified: ',lower(nvl('&&report_type','html')) report_type from dual
new 1: select 'Type Specified: ',lower(nvl('','html')) report_type from dual
Type Specified: html
old 1: select '&&report_type' report_type_def from dual
new 1: select 'html' report_type_def from dual
old 1: select '&&view_loc' view_loc_def from dual
new 1: select 'AWR_PDB' view_loc_def from dual
Current Instance
2. you can schedule report by email alert also.

Resources