PL/SQL procedure issue - sql

I'm new to Oracle PL/SQL and have been trying to run this code but only getting the following message - PL/SQL procedure successfully completed.
SQL> DECLARE
v_num NUMBER:=10;
BEGIN
FOR v_num IN 1..3
LOOP
DBMS_OUTPUT.PUT_LINE(v_num);
END LOOP;
DBMS_OUTPUT.PUT_LINE(v_num);
END;

SQL/Plus doesn't print output by default; you have to enable it:
set serveroutput on
Afterwards, DBMS_OUTPUT.PUT_LINE will print output as expected.

If you want you can add the line
set serveroutput on
to the file
$ORACLE_HOME/sqlplus/admin/glogin.sql
This will cause the server output to be on after you login to SQL*Plus -- you won't have to type it in each time you login.
HTH

Frank Schmitt has suggested a method to connect to the pl/sql server.
However, you can also achieve this by using dbms_output view! I found this
useful blog that explains the required steps to activate the
dbms_output package
SQL Developer DBMS_OUTPUT Configuration

Related

getting 'ORA-00922: missing or invalid option' error when I'm trying to run the below plsql code. how to solve it?

SET SERVEROUTPUT ON;
DECLARE
var_test1 VARCHAR2(30) := 'RebellionRider';
BEGIN
DBMS_OUTPUT.PUT_LINE (var_test1);
END;
Whenever im running the above code im getting the following error. Im using oracle apex to run my code. how to solve it?
Error:-
ORA-00922: missing or invalid option
BEGIN
DBMS_OUTPUT.PUT_LINE (var_test1);
END;
As you use Apex, where exactly did you run that code? It works just fine in its SQL Workshop (SQL Commands).
Just remove SET SERVEROUTPUT ON, it is SQL*Plus command to enable the output, it is
meaningless in Apex
raises ORA-00922 (in Apex)
When you are on developer environment, you can to select only the text you want run, so press F5 or click run. This will also working.

Oracle measuring execution time stored procedure

I would like to measure the execution time of a stored procedure in Oracle. I have learned about the technique of writing an entry in a temporary logging table at the start and the end but am unable to use this technique.
Can you refer me to an open source/free tool with which I'm able to do this?
Thanks!
The answer depends on your environment you are using.
If you are using SQLPlus, you can enable a timer as follows :t
SQL> set timing on
Then, just execute your procedure, like :
SQL> exec my_procedure;
When the procedure will complete, a summary line will be displayed, like :
PL/SQL procedure successfully completed.
Elapsed: 00:00:03.05
From within PL/SQL, you can use dbms_utility.get_time :
DECLARE
start_time pls_integer;
BEGIN
start_time := dbms_utility.get_time;
exec my_procedure;
dbms_output.put_line((dbms_utility.get_time - start_time)/100 || ' seconds');
END;
/
Should output something like :
3 seconds
PL/SQL procedure successfully completed.
See this excellent explanation from Tom Kyte.
Execution of the stored procedure's start /end time can be logged using
DBMS_UTILITY.get_cpu_time or DBMS_UTILITY.get_time
E.g.
CREATE OR REPLACE PROCEDURE test_proc IS
BEGIN
DBMS_OUTPUT.put_line('start time '||DBMS_UTILITY.get_time);
<your statement>
DBMS_OUTPUT.put_line('end time '||DBMS_UTILITY.get_time);
END;
There are of course other ways of finding the execution time using Profiler
Have a look at this as well
If you want to get details that can actually help you diagnose issues, I highly recommend dbms_profiler.
It is easy to use and provides statistics for every line in the pl/sql including number of executions, total time, min and max.

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.

PL/SQL: Having trouble creating procedure

I'm having issues creating a simple procedure in oracle 10g
All I'm trying to do is this procedure:
create procedure greetings
is
begin
dbms_output.put_line('Hello');
end;
I type this into the prompt:
#proc1.sql
and I get only this:
6
and I'm unable to get back to the prompt again without ctrl+c. I know it's ghetto but I'm using SSH if that helps solve the problem.
You need a / on a line by itself after the final end; so sqlplus will know it's time to execute. (sqlplus somehow fails to know how to parse the language which is its sole purpose for existing, so it doesn't know that the last end; you entered is the one that terminates the create command.)

oracle - run stored procedure from script

I've a script that I am using to build/drop tables and basically setting up the entire schema.
After googling, I still can't figure out how to run a stored procedure.
The script is a .txt file, and I run it using Apex SQL Oracle.
If I write only this line in a script:
execute procedurename(1); --where 1 is paramter.
You have requested to run a script that does not contain any runnable
statements.
SQL>create or replace procedure procedurename(p_num number)
as
begin
null;
end;
/
Procedure created.
SQL>execute procedurename(1);
PL/SQL procedure successfully completed.
everything seems ok on SQLPLUS with oracle 11.
so it must be an apex thing.
Since execute is a sqlplus statement ,try calling the procedure using begin-end PLSQL block in Apex SQL
BEGIN
procedurename(1);
END;
/
save this in a file proc_call.sql and then call it in your script like
#C:\proc_call.sql
where C: is the sample path
For some information refer the below link
https://forums.oracle.com/forums/thread.jspa?threadID=618393