Warning: Package Body created with compilation errors - oracle

Please check my package and procedures.
My package:
create or replace package transaction1 as
procedure enter_transaction(acc number, kind varchar2, amount number);
procedure apply_transaction;
end;
/
This is my body:
create or replace package body transaction1 as
procedure enter_transaction(acc in number, kind in varchar2, amount in number)
is
begin
end;
procedure apply_transaction
is
begin
end;
end;
/
What is the warning? Why?

If you see a warning: Errors: check compiler log
then run show errors command and you will see the error log :
7/5 PLS-00103: Encountered the symbol "END" when expecting one of the following:
( begin case declare exit for goto if loop mod null pragma
raise return select update while with <an identifier>
<a double-quoted delimited-identifier> <a bind variable> <<
continue close current delete fetch lock insert open rollback
savepoint set sql execute commit forall merge pipe purge
14/5 PLS-00103: Encountered the symbol "END" when expecting one of the following:
( begin case declare exit for goto if loop mod null pragma
raise return select update while with <an identifier>
<a double-quoted delimited-identifier> <a bind variable> <<
continue close current delete fetch lock insert open rollback
savepoint set sql execute commit forall merge pipe purge
In case of your package body Oracle complains because BEGIN END blocks don't contain any commands.
In Oracle the BEGIN-END block must contain at least one command. Could be NULL, if you don't want to run anything (and don't forget to place a semicolon after NULL command):
PROCEDURE ......
IS
BEGIN
NULL;
END;

Related

Error when declaring a number variable in a function block in Oracle SQL

SQL Worksheet
create or replace FUNCTION checkSubmitted (pStudentId IN VARCHAR, pCourseCode IN VARCHAR, pAssignmentNumber IN NUMBER)
RETURN VARCHAR
IS output VARCHAR(20);
DECLARE mark_result NUMBER;
BEGIN
SELECT assignment.mark INTO mark_result FROM Assignment WHERE student_id = pStudentId AND course_code = pCourseCode AND assignment_number = pAssignmentNumber;
IF (mark_result IS NOT NULL) THEN
RETURN 'Submitted';
ELSE
RETURN 'Not Submitted';
END IF;
END;
Compiler - Log
LINE/COL ERROR
--------- -------------------------------------------------------------
4/5 PLS-00103: Encountered the symbol "DECLARE" when expecting one of the following: begin function pragma procedure subtype type <an identifier> <a double-quoted delimited-identifier> current cursor delete exists prior The symbol "begin" was substituted for "DECLARE" to continue.
12/4 PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following: ( begin case declare end exception exit for goto if loop mod null pragma raise return select update while with <an identifier> <a double-quoted delimited-identifier> <a bind variable> << continue close current delete fetch lock insert open rollback savepoint set sql execute commit forall merge pipe purge
Errors: check compiler log
I want to create a SQL check function and i need to declare a variable in the function.
Please advise how can i achieve this.
As commented, your current problem is easy to fix.
However, I'd like to point you to another possible "mistake" - what will happen if there's no data for combination of those parameters' values? SELECT won't return NULL - it will raise NO_DATA_FOUND exception, and you should handle it. (I presume that it isn't likely for query to return more than a single value; if it is, handle it as well).
Also, try to avoid multiple RETURN statements. Procedures can be long and you might get lost in where you return and what you return. Set output variable's value (which you declared, but never used), and return it at the end of the procedure.
Therefore, consider something like this:
create or replace function checksubmitted
(pstudentid in varchar, pcoursecode in varchar, passignmentnumber in number)
return varchar
is
mark_result number;
output varchar(100);
begin
select a.mark
into mark_result
from assignment a
where a.student_id = pstudentid
and a.course_code = pcoursecode
and a.assignment_number = passignmentnumber;
if mark_result is not null then
output := 'Submitted';
else
output := 'Not Submitted';
end if;
return output;
exception
when no_data_found then
output := 'There is no data for these input parameters';
return output;
end;
/

error PLSQL creating procedure with exception

I'm trying to create a procedure in PL/SQL and I receive the following error:
Errors: PROCEDURE BORRAR_PELI
Line/Col: 10/1 PLS-00103: Encountered the symbol "EXCEPTION" when expecting one of the following:
( begin case declare end exit for goto if loop mod null
pragma raise return select update while with <an identifier>
<a double-quoted delimited-identifier> <a bind variable> <<
continue close current delete fetch lock insert open rollback
savepoint set sql execute commit forall merge pipe purge
json_exists json_value json_query json_object json_array***
My procedure:
CREATE OR REPLACE PROCEDURE borrar_peli(delete_id peliculas.id%TYPE)
IS
corrupto EXCEPTION;
precio_dia_v peliculas.precio_dia%TYPE;
BEGIN
SELECT precio_dia INTO precio_dia_v FROM peliculas WHERE id = delete_id;
IF precio_dia_v > 4 THEN
RAISE corrupto;
ELSE
DELETE FROM pelĂ­culas WHERE id = delete_id;
COMMIT;
END IF;
EXCEPTION
WHEN corrupto THEN
DBMS_OUTPUT.put_line('es corrupto');
WHEN OTHERS THEN
DBMS_OUTPUT.put_line('Error code ' || SQLCODE || ': ' || SQLCODE);
END;

Running PLSQL ( Oracle ) Stored Procedure in Liquibase error : Package or function X is in an invalid state

I am trying to run the following stored procedure:
CREATE OR REPLACE PROCEDURE RETRY_TRANS_EXCEPTION
AS
BEGIN
FOR i IN 1..5 LOOP
DBMS_OUTPUT.PUT('Try #' || i);
ALTER TABLE CIS_CASE ADD TEST01 varchar2(1) NOT NULL;
END;
END;
/
and calling it in changelog.xml as:
<sql>CALL RETRY_TRANS_EXCEPTION();</sql>
i get error:
Liquibase update Failed: Migration failed for change set eldata-changelog.xml::2016-08-25-cn-01::Ch Will:Reason: liquibase.exception.DatabaseException: Error executing SQL CALL RETRY_TRANS_EXCEPTION(): ORA-06575: Package or function RETRY_TRANS_EXCEPTION is in an invalid state
What i am trying to achieve is to be able to run a stored procedure through Liquibase with a loop in it.
Thanks for your help Prashant. What worked in my case was your solution plus one change:
CREATE OR REPLACE PROCEDURE RETRY_TRANS_EXCEPTION
AS
v_query varchar2(100);
BEGIN
FOR i IN 1..500 LOOP
DBMS_OUTPUT.PUT('Try #' || i);
v_query := 'ALTER TABLE CIS_CASE ADD TEST01 varchar2(1) NULL';
execute immediate v_query;
END loop;
END;
/
and then calling the Stored Procedure from the changelog, as:
<changeSet id="2016-08-25-cw-01" author="Ch Will">
<comment>
Testing retry logic on liquibase
</comment>
<sql>CALL RETRY_TRANS_EXCEPTION();</sql>
</changeSet>
You can't call it because the procedure does not compile correctly. Go back and fix the compilation errors, then try again.
Here are a couple of errors that stand out to me:
the for loop should end with end loop;, not end;
You can't have DDL statements directly in the code. You need dynamic SQL to execute a DDL statement from a procedure: execute immediate 'ALTER TABLE CIS_CASE ADD TEST01 varchar2(1) NOT NULL';
Additional note: I don't understand why you are trying to execute the same DDL statement multiple times inside a loop. Obviously, you won't be able to add the same column with the same name over and over. You will get a runtime error.
SQL> CREATE OR REPLACE PROCEDURE RETRY_TRANS_EXCEPTION
2 AS
3 BEGIN
4 FOR i IN 1..5 LOOP
5 DBMS_OUTPUT.PUT('Try #' || i);
6 ALTER TABLE CIS_CASE ADD TEST01 varchar2(1) NOT NULL;
7 END;
8 END;
9 /
Warning: Procedure created with compilation errors
SQL> show err
Errors for PROCEDURE PRASHANT-MISHRA.RETRY_TRANS_EXCEPTION:
LINE/COL ERROR
-------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
6/4 PLS-00103: Encountered the symbol "ALTER" when expecting one of the following: ( begin case declare end exit for goto if loop mod null pragma raise return select update while with <an identifier> <a double-quoted delimited-identifier> <a bind variable> << continue close current delete fetch lock insert open rollback savepoint set sql execute commit forall merge pipe purge
Did required fixes :
CREATE OR REPLACE PROCEDURE RETRY_TRANS_EXCEPTION
AS
v_query varchar2(100);
BEGIN
FOR i IN 1..5 LOOP
DBMS_OUTPUT.PUT('Try #' || i);
v_query := 'ALTER TABLE CIS_CASE ADD TEST01 varchar2(1) NOT NULL' ;
execute immediate v_query;
END loop;
END;
PLSQL stored procedure can't use DDL statements, like
alter table ...
so the
execute immediate ("...")
statement is required because in fact it creates an autonomous implicit transition that can't be rollbacked

Error(5,1): PLS-00103: Encountered the symbol "CREATE" error while creating function

Error(5,1): PLS-00103:
Encountered the symbol "CREATE" when expecting one of the following:
( begin case declare exit for goto if loop mod null pragma raise return select update while with <an identifier> <a double-quoted delimited-identifier> <a bind variable> << continue close current delete fetch lock insert open rollback savepoint set sql execute commit forall merge pipe purge
Below the code I've written.
CREATE OR replace FUNCTION First_three_records
RETURN NUMBER AS
BEGIN
CREATE TEMPORARY TABLE temp_emp ON COMMIT DROP AS
SELECT *
FROM emp
WHERE deptno=20;
INSERT INTO tgt
SELECT *
FROM temp_emp;
END;
Oracle does not have local temporary tables, and you can't create objects within a PL/SQL block unless you use dynamic SQL; and it's very rarely necessary or a good idea. Your schema should be created in a controlled way, not on the fly.
You could use a collection instead but there is no point here, you can just do:
INSERT INTO tgt
SELECT *
FROM emp
WHERE deptno=20;
I'm not sure why you're wrapping that in a function at all; your function is also declared to return a number, but you have no return statement.

Error is occurring during execution of procedure in oracle

I am getting the error during executing the below procedure.
CREATE OR REPLACE PROCEDURE P_SUMIT (P_FEED IN FEED.FEED_ID%TYPE, P_OPCO_ID IN FEED.OPCO_ID%TYPE)
AS
BEGIN
DECLARE V_PCF_PATTERN FEED.PCF_PATTERN%TYPE;
DECLARE CURSOR C_FEED FOR SELECT PCF_PATTERN FROM FEED WHERE FEED_ID=P_FEED AND OPCO_ID=P_OPCO_ID;
OPEN C_FEED;
LOOP
FETCH C_FFED INTO V_PCF_PATTERN;
EXIT WHEN C_FEED%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(V_PCF_PATTERN);
END LOOP;
CLOSE C_FEED;
END;
Error logs :
3/1 PLS-00103: Encountered the symbol "DECLARE" when expecting one of the following:
begin function pragma procedure subtype type <an identifier>
<a double-quoted delimited-identifier> current cursor delete
exists prior external language
The symbol "begin" was substituted for "DECLARE" to continue.
4/1 PLS-00103: Encountered the symbol "DECLARE" when expecting one of the following:
begin function pragma procedure subtype type <an identifier>
<a double-quoted delimited-identifier> current cursor delete
exists prior
Please assist.
DECLARE statements should be after AS keyword.
CREATE OR REPLACE PROCEDURE P_SUMIT (P_FEED IN FEED.FEED_ID%TYPE, P_OPCO_ID IN FEED.OPCO_ID%TYPE)
AS
V_PCF_PATTERN FEED.PCF_PATTERN%TYPE;
CURSOR C_FEED IS SELECT PCF_PATTERN FROM FEED WHERE FEED_ID=P_FEED AND OPCO_ID=P_OPCO_ID;
BEGIN
OPEN C_FEED;
LOOP
FETCH C_FEED INTO V_PCF_PATTERN;
EXIT WHEN C_FEED%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(V_PCF_PATTERN);
END LOOP;
CLOSE C_FEED;
END P_SUMIT;
/
There's no need for an explicit cursor here, or for parameters to be prefixed with "p_". Implicit cursors are quicker and more robust to write, and perform better.
create or replace procedure p_sumit (
feed_id in feed.feed_id%type,
opco_id in feed.opco_id%type)
as
begin
for c_feed in (
select pcf_pattern
from feed
where feed_id = p_sumit.feed_id and
opco_id = p_sumit.opco_id)
loop
dbms_output.put_line(c_feed.pcf_pattern);
end loop;
end;

Resources