How to execute an oracle stored procedure? - oracle

I am using oracle 10g express edition. It has a nice ui for db developers. But i am facing some problems executing stored procedures.
Procedure:
create or replace procedure temp_proc is
begin
DBMS_OUTPUT.PUT_LINE('Test');
end
it is created successfully. But when i execute:
execute temp_proc;
it shows ORA-00900: invalid SQL statement
So help needed here

Execute is sql*plus syntax .. try wrapping your call in begin .. end like this:
begin
temp_proc;
end;
(Although Jeffrey says this doesn't work in APEX .. but you're trying to get this to run in SQLDeveloper .. try the 'Run' menu there.)

Oracle 10g Express Edition ships with Oracle Application Express (Apex) built-in. You're running this in its SQL Commands window, which doesn't support SQL*Plus syntax.
That doesn't matter, because (as you have discovered) the BEGIN...END syntax does work in Apex.

Both 'is' and 'as' are valid syntax. Output is disabled by default. Try a procedure that also enables output...
create or replace procedure temp_proc is
begin
DBMS_OUTPUT.ENABLE(1000000);
DBMS_OUTPUT.PUT_LINE('Test');
end;
...and call it in a PLSQL block...
begin
temp_proc;
end;
...as SQL is non-procedural.

I use oracle 12 and it tell me that if you need to invoke the procedure then use call keyword.
In your case it should be:
begin
call temp_proc;
end;

Have you tried to correct the syntax like this?:
create or replace procedure temp_proc AS
begin
DBMS_OUTPUT.PUT_LINE('Test');
end;

In Oracle SQL Developer (GUI), using 12c, also don't forget to enable the DMBS Output window (click View => Dbms Output, then click "+" sign, and select your connection), by default the window is not enabled.
The following syntax will then output to this window:
begin
temp_proc
end;

You can do simply the following on the Command Window:
Connected to Oracle Database 19c Enterprise Edition Release 19.0.0.0.0
Connected as XXX#YYY
SQL> call temp_proc();
Or:
SQL> execute temp_proc();

Related

character '#' in comment: procedure is not compiling

Can anybody explain to me, why this syntax is not working at all, when I try to compile it in a SQL Developer worksheet? It seems as the '#' char is kind of preprocessed by th SQL Developer, but I did not find any explanations, why what is happening here.
CREATE OR REPLACE PROCEDURE PROC_TEST
IS
/*
# example
*/
BEGIN
DBMS_OUTPUT.PUT_LINE('Test # Procedure');
END;
/
this works fine:
CREATE OR REPLACE PROCEDURE PROC_TEST
IS
/*
- example
*/
BEGIN
DBMS_OUTPUT.PUT_LINE('Test # Procedure');
END;
/
SQL Developer Version 17.2.0.188
Oracle Database 12c
thanks a lot.
When you say it's not working, can you show the error that SQL Developer is giving you?
It could be a SQL Developer bug. If I try the same code in version 18.3, it compiles just fine.

How to execute procedure in APEX SQL script?

I am trying to understand how to use multiple procedures in APEX SQL script. First I don't really need stored procedure, but not sure how to declare simple procedure in APEX SQL script. So this is my attempt:
create or replace procedure test1 as
begin
DBMS_OUTPUT.ENABLE;
dbms_output.put_line('test1');
end;
execute test1;
This gives me an error:
Error at line 7: PLS-00103: Encountered the symbol "EXECUTE"
So questions - how to create regular/not stored/ procedures in one SQL script and then call them. What is the entry point of execution in APEX SQL script?
UPD (At the first time I understood question totally wrong)
Correct version of a script:
create or replace procedure test1 as
begin
DBMS_OUTPUT.ENABLE;
dbms_output.put_line('test1');
end;
/
begin
test1;
end;
/
Documentation says, that script can contain inly SQL and PL/SQL commands. Commands of sqlplus will be ignored.
OLD VERSION (Let stay here)
In APEX pages you can use PL/SQL anonymous blocks. For example, you can create process (APEX has some types of them) or PL/SQL region, and use following:
declare
...
begin
some_proc(:P_MY_ITEM);
end;
Here you can invoke any procedure and do anything else that allowed by PL/SQL. Also you can use parameters like :P_ITEM_NAME to get and set values of page and application items.

Oracle stored procedure - difference between 10g and 11g perhaps?

The following stored procedure code works in our DEV and TEST environments which run with Oracle 11G but will not work in our 10G PROD environment:
first, I created my own data structure in Oracle to hold any array of VARCHAR2:
create or replace
type MAT_MULTIPLES_ARRAY as table of VARCHAR2(100);
here is the procedure code:
create or replace PROCEDURE MAT_SUBMIT_JOB (v_multiples_columns_to_add IN our_schema.MAT_MULTIPLES_ARRAY)
v_jobno number;
v_job_name VARCHAR2(100);
v_error_message VARCHAR2(32000);
begin
v_job_name := 'doesnt matter right now';
dbms_scheduler.create_job(v_job_name,program_name=>'MAT_JOB_PROGRAM');
dbms_scheduler.set_job_anydata_value(v_job_name,1,sys.anydata.convertCollection(v_multiples_columns_to_add));
dbms_scheduler.enable(v_job_name);
end;
again, this same code works in 11G in our DEV and TEST environments, and it compiles in our 10G environment, but then it appears to barf during runtime, on the second dbms_scheduler line (in bold).
Does dbms_scheduler work in 10G? Or perhaps there is a problem with 'sys.anydata.convertCollection(v_multiples_columns_to_add)'
Here is the error message:
ORA-22370: incorrect usage of method originated from line 19 in my procedure.
line 19 is the line with convertCollection() call.
Please help!
I found this in the documentation:
http://docs.oracle.com/cd/B19306_01/appdev.102/b14258/d_sched.htm#i1000820
SET_JOB_ANYDATA_VALUE requires that you be the owner of the job or have ALTER privileges on that job. You can also set a job argument value if you have the CREATE ANY JOB privilege.
This might also be related as well:
ANYDATA with Collections based on rowtype

oracle procedure ussing Host() command

I am having a problem using host() command in oracle procedure.
I have written very simple oracle code.
CREATE OR REPLACE PROCEDURE
run_command(command_i IN VARCHAR2)
IS
l_message VARCHAR2 (100);
BEGIN
l_message := 'cmd ' || command_i;
host(l_message);
END run_command;
when host(l_message); is eliminated works fine.
Whats the problem and is there anyway to create a routine which uses host()?
The HOST command is only available in SQL*Plus and not from PL/SQL.
You can use Java stored procedure to call call OS commands. Oracle released a white paper on calling OS commands from within PL/SQL back in 2008 but there's plenty of other stuff out there (including Oracle Base, which is quite good)
Another clunky, but non-Java way would be to create DBMS_SCHEDULER ad-hoc EXECUTABLE job which is your HOST command (e.g. ls ), and then execute the job.
Note these run on the database server, not on your remote/local client.

Running Oracle stored Procedure

I have a sql file that contains a simple procedure to print "Hi" like,
CREATE OR REPLACE PROCEDURE skeleton
IS
BEGIN
DBMS_OUTPUT.PUT_LINE('Hi');
END;
When I try to execute this from sql file itself, it just gets compiled and it is not running.
I added the as,
CREATE OR REPLACE PROCEDURE skeleton
IS
BEGIN
DBMS_OUTPUT.PUT_LINE('Hi');
END;
/ /* this is required else it throws me compilation error*/
set serveroutput on
EXECUTE skeleton;
I tried calling the same skeleton from other sql file also and even from the sqldeveloper GUI, that also didnt execute this. Only the sqlplus commandline helps me. Let me know what I am missing and the solution for this.
Here are the steps I took using SQL Plus
SQL> CREATE OR REPLACE PROCEDURE skeleton
2 IS
3 BEGIN
4 DBMS_OUTPUT.PUT_LINE('Hi');
5 END;
6 /
Procedure created.
SQL> set serveroutput on
SQL> EXECUTE skeleton;
Hi
PL/SQL procedure successfully completed.
SQL>
Can you start a new sqlplus session replicate these steps and post the content?
The only change I had to make to your sql to allow running it as an #file was to remove the comment. This is the whole content of the the .sql file:
CREATE OR REPLACE PROCEDURE skeleton
IS
BEGIN
DBMS_OUTPUT.PUT_LINE('Hi');
END;
/
set serveroutput on
EXECUTE skeleton;
You should get an output something like this:
C:\Temp>sqlplus username/password #skeleton.sql
SQL*Plus: Release 11.1.0.6.0 - Production on Mon Oct 5 17:10:46 2009
Copyright (c) 1982, 2007, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Procedure created.
Hi
PL/SQL procedure successfully completed.
SQL>
Try setting set serveroutput on before the dbms_output.
In SQL Developer, you need the "execute script" button which is the second button at the top left (little green arrow in front of document) instead of the one with the big green arrow.
That threw me the first time I used it.
(source: devshed.com)
The reason your first example gets compiled and not run, is that you are not actually asking it to run. You are creating a procedure inside the database, which works correctly, but not calling it. In your second block you have an EXECUTE which is where it actually runs.

Resources