Call Oracle stored procedures from a main oracle stored procedure (syntax error) - oracle

I am trying to create a main Oracle stored procedure, calling other Oracle stored procedures. I am getting a syntax error in Toad.
The environment is Windows Server 2012R2 and the Oracle edition is 12.1.0.
The main stored procedure is simple:
CREATE OR REPLACE PROCEDURE TESTDB.MAIN
(
ID IN NUMBER
)
IS
BEGIN
CALL PROCEDURE1(ID);
CALL PROCEDURE2(ID);
CALL PROCEDURE3(ID);
CALL PROCEDURE4(ID);
END;
I am getting a syntax error while compiling for each procedure:
PLS-00103: Encountered the symbol "PROCEDURE1" when
expecting one of the following:
:= . ( # % ; The symbol ":=" was substituted for
"PROCEDURE1" to continue.
What is the correct syntax in order to call the procedures from one central procedure?

You have to remove CALL, that can only be used
to execute a routine […] from within SQL
BEGIN
PROCEDURE1(ID);
PROCEDURE2(ID);
PROCEDURE3(ID);
PROCEDURE4(ID);
END;
For example:
SQL> create or replace procedure proc(n IN OUT number) is begin n := 10; end;
2 /
Procedure created.
SQL> var x number
SQL> call proc(:x);
Call completed.
SQL> print :x
X
----------
10
SQL> declare
2 y number;
3 begin
4 call proc(y);
5 end;
6 /
call proc(y);
*
ERROR at line 4:
ORA-06550: line 4, column 8:
PLS-00103: Encountered the symbol "PROC" when expecting one of the following:
:= . ( # % ;
The symbol ":=" was substituted for "PROC" to continue.
SQL>

Related

What actually PROMPT<?> syntax do in oracle procedure?

So, in this code what exactly PROMPT syntax do?
PROMPT create or replace procedure abc (p_name, p_id)
AUTHID CURRENT_USER
as
begin
dbms_output.put_line('hi');
end;
PROMPT has a meaning if you run some code in SQL*Plus (not many people do that nowadays). It displays text what follows that keyword.
SQL> help prompt
PROMPT
------
Sends the specified message or a blank line to the user's screen.
PRO[MPT] [text]
SQL> prompt hello there!
hello there!
SQL>
In your case, it produces unwanted result as it displays the create procedure (instead of creating it):
SQL> PROMPT create or replace procedure abc (p_name, p_id)
create or replace procedure abc (p_name, p_id)
SQL> AUTHID CURRENT_USER
SP2-0734: unknown command beginning "AUTHID CUR..." - rest of line ignored.
SQL> as
SP2-0042: unknown command "as" - rest of line ignored.
SQL> begin
2 dbms_output.put_line('hi');
3 end;
4 /
hi
PL/SQL procedure successfully completed.
SQL>
You got the result, but just as pure accident as
begin
dbms_output.put_line('hi');
end;
was a valid PL/SQL block.
Code you posted (without prompt) is invalid:
SQL> create or replace procedure abc (p_name, p_id)
2 AUTHID CURRENT_USER
3 as
4 begin
5 dbms_output.put_line('hi');
6 end;
7 /
Warning: Procedure created with compilation errors.
SQL> show err
Errors for PROCEDURE ABC:
LINE/COL ERROR
-------- -----------------------------------------------------------------
1/22 PLS-00103: Encountered the symbol "," when expecting one of the
following:
in out <an identifier> <a double-quoted delimited-identifier>
... long double ref char time timestamp interval date binary
national character nchar
3/1 PLS-00103: Encountered the symbol "AS" when expecting one of the
following:
with authid cluster order deterministic parallel_enable
pipelined result_cache
6/4 PLS-00103: Encountered the symbol "end-of-file" when expecting
one of the following:
end not pragma final instantiable order overriding static
member constructor map
SQL>
What does it mean? Procedure's parameters have to have datatype:
SQL> create or replace procedure abc (p_name in varchar2, p_id in number)
2 AUTHID CURRENT_USER
3 as
4 begin
5 dbms_output.put_line('hi');
6 end;
7 /
Procedure created.
SQL> exec abc(null, null);
hi
PL/SQL procedure successfully completed.
SQL>

Cannot execute a stored procedure in Oracle

Here is a simple example using Toad for Data Analysts 3.0.1.1734. I have full permissions on the schema JSWEENEY.
Create the table
CREATE TABLE JSWEENEY.TEMP_SQL
(
SQL VARCHAR2(3000)
);
Create the procedure
CREATE OR REPLACE PROCEDURE JSWEENEY.SP_INSERT_SQL
IS
BEGIN
INSERT INTO JSWEENEY.TEMP_SQL(SQL) VALUES('SELECT * FROM TEMP_SQL');
COMMIT;
END JSWEENEY.SP_INSERT_SQL;
/
Execute the procedure:
BEGIN
JSWEENEY.SP_INSERT_SQL;
END;
The first error:
ORA-06550: line 2, column 11:
PLS-00905: object JSWEENEY.SP_INSERT_SQL is invalid
ORA-06550: line 2, column 2: PL/SQL: Statement ignored
Execute the procedure:
BEGIN
EXECUTE JSWEENEY.SP_INSERT_SQL;
END;
The second error:
ORA-06550: line 2, column 10:
PLS-00103: Encountered the symbol "JSWEENEY" when expecting one of the following: := . ( # % ; immediate The symbol ":=" was substituted for "JSWEENEY" to continue.
Any suggestions would be greatly appreciated.
When you compile the procedure you will get an error; if your client doesn't display that then you can query the user_errors view (or all_errors if you're creating it in a different schema) to see the problem. Here it will be complaining that:
LINE/COL ERROR
-------- -----------------------------------------------------------------
6/13 PLS-00103: Encountered the symbol "." when expecting one of the following:
;
It's valid to use the schema name in the create call; but not as part of the end. So if you need to specify the schema at all - which you don't if you're creating an object in your own schema, but your reference to permissions makes it sound like you aren't - then it should be:
CREATE OR REPLACE PROCEDURE JSWEENEY.SP_INSERT_SQL
IS
BEGIN
INSERT INTO JSWEENEY.TEMP_SQL(SQL) VALUES('SELECT * FROM TEMP_SQL');
COMMIT;
END SP_INSERT_SQL;
/
Your second error is because execute on its is a client command (in SQL*Plus and relations), not a PL/SQL statement. The error refers to immediate because PL/SQL does have an execute immediate statement which is used for dynamic SQL, not for making static calls to procedures. Your first syntax to run the procedure is correct, once the procedure itself is valid:
BEGIN
JSWEENEY.SP_INSERT_SQL;
END;
/
try this edited the SQL statement.
create table TEMP_SQL ( col1 varchar2(100));
CREATE OR REPLACE PROCEDURE SP_INSERT_SQL
AS
BEGIN
INSERT INTO TEMP_SQL SELECT * FROM TEMP_SQL;
COMMIT;
END SP_INSERT_SQL;

when try to excure procedure it said compliation error in oracle?

I try to write select procedure in oracle.but it compile success, when I try to execute it given error.
set serveroutput on;
CREATE OR REPLACE PROCEDURE retrieve_decrypt(
custid in NUMBER,
column_name in VARCHAR2,
test_value OUT VARCHAR2
)
AS
BEGIN
-- enc_dec.decrypt(column_name,password) into test_value from employees where custid=5;
COMMIT;
END;
/
set serveroutput on;
EXEC retrieve_decrypt(5,'creditcardno');
the error says ,
This is your procedure:
SQL> create or replace procedure retrieve_decrypt
2 (custid in number,
3 column_name in varchar2,
4 test_value out varchar2
5 )
6 as
7 begin
8 -- your code goes here
9 null;
10 end;
11 /
Procedure created.
SQL>
This is how you call it (and get the error):
SQL> exec retrieve_decrypt(5, 'creditcardno');
BEGIN retrieve_decrypt(5, 'creditcardno'); END;
*
ERROR at line 1:
ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'RETRIEVE_DECRYPT'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
SQL>
The cause of the error is:
the procedure contains 3 parameters:
2 of them are IN - you provided their values
1 of them is OUT - you didn't provide it and got the error
Here's what you should have done: as the 3rd parameter is OUT, you'll have to DECLARE it:
SQL> declare
2 l_out varchar2(20);
3 begin
4 retrieve_decrypt(5, 'creditcardno', l_out);
5 end;
6 /
PL/SQL procedure successfully completed.
SQL>
EXEC you used is a SQL*Plus command so it might not work everywhere; DECLARE-BEGIN-END block will so I'd suggest you use it.
Alternatively, in SQL*Plus, it could be rewritten as
SQL> var l_out varchar2
SQL>
SQL> exec retrieve_decrypt(5, 'creditcardno', :l_out);
PL/SQL procedure successfully completed.
SQL>
but - once again - you'd better use DECLARE-BEGIN-END PL/SQL block.
The initial error is:
wrong number or types of arguments in call to 'RETRIEVE_DECRYPT'
The procedure requires 3 parameters. You are only passing 2 (or apparently only 1, in the attempt that generated the error message shown).
Why do you also see the message "Usually a PL/SQL compilation error"? The EXEC command in SQLPlus creates a PL/SQL block containing the text you provide, and sends that to Oracle for execution. Oracle attempts to compile that PL/SQL block (just like it compiles the procedure when you create it). In this case, the compilation fails because of the mismatch in the number of arguments.

How to call Oracle Procedure which has one OUT parameter

My oracle procedure structure,
CREATE OR REPLACE PACKAGE BODY NLS_ADMIN."MY_PKG"
AS
PROCEDURE DATA_PRC (oresult OUT NUMBER )
IS
varKeyValue varchar2(1);
BEGIN
...
...
END;
I tried to call above procedure by executing below statement,
declare
oresult NUMBER;
begin
EXECUTE DATA_PRC(oresult);
end;
But getting below exception. Please help me how to call this procedure.
ORA-06550: line 8, column 9:
PLS-00103: Encountered the symbol "DATA_PRC" when expecting one of the following:
:= . ( # % ; immediate
The symbol ":=" was substituted for "DATA_PRC" to continue.
Simply this:
declare
oresult NUMBER;
begin
MY_PKG.DATA_PRC(oresult);
end;

Compilation errors on stored procedure

I am typically a SQL Server developer however am now working with a system that uses Oracle. I have created a new procedure and am getting a runtime error. Here is the procedure:
CREATE OR REPLACE PROCEDURE CHK_LASTAPPTIME
(
LASTAPPTIME OUT VARCHAR2
)
IS
v_appappid varchar2(20) null;
v_lastapptime number null;
BEGIN
select max(APPID) into v_appappid from applicationtable;
select trunc(v_lastapptime = (((sysdate - capturedate) * 24)) * 60) from applicationtable where APPID = v_appappid;
LASTAPPTIME := to_char(v_lastapptime);
END CHK_LASTAPPTIME;
Here is the error that I am getting:
SQL> var x varchar2;
SQL> exec CHK_LASTAPPTIME(:x);
BEGIN CHK_LASTAPPTIME(:x); END;
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at "CAPDEV.CHK_LASTAPPTIME", line 19
ORA-06512: at line 1
Your proecedure doesn't have 19 lines, so the message is odd, or you've shown different code to what you're actually running. Since what you've shown doesn't compile I'm assuming you've tried to cut it down for the question, and I'll also assume your actual procedure is valid and does what you want.
The problem is in your run-time variable declaration. You haven't specified a size for x, so it uses a default. On my DB, in SQL*Plus that seems to allow three characters but errors with four or more:
var x varchar2;
exec :x := 'a';
PL/SQL procedure successfully completed.
exec :x := 'ab';
PL/SQL procedure successfully completed.
exec :x := 'abc';
PL/SQL procedure successfully completed.
exec :x := 'abcd';
BEGIN :x := 'abcd'; END;
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at line 1
The limit might be related to your character set, perhaps. SQL Developer allows more by default.
Anyway, specify the size for your variable:
var x varchar2(30);
exec CHK_LASTAPPTIME(:x);
You seem to be generating a number in your procedure, and then returning that as a string. Using the same data type would make this a bit simpler.

Resources