PL/SQL IF loop not working in my package - oracle

I want to write my procedure as follows:
begin
if(condn1) then
statements;
end if ;
exception part
end ;
begin
if(condn2) then
statements;
end if ;
exception part
end ;
When I try to compile this package I get the following error:
LINE/COL ERROR
-------- -----------------------------------------------------------------
509/5 PLS-00103: Encountered the symbol "WHEN" 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
The symbol "case" was substituted for "WHEN" to continue.
545/10 PLS-00103: Encountered the symbol ";" when expecting one of the
following:
case

If you want to include several exception parts, you must include a whole block for each exception part you want. The block structure is:
DECLARE [Optional]
... [Optional]
BEGIN
...
EXCEPTION
WHEN ... THEN
...
END;
Thus, your code with 2 exception parts should be something similar to the following code:
DECLARE
your_variables;
BEGIN
BEGIN
IF condition_1 THEN
statements;
END IF;
EXCEPTION
WHEN your_exceptions_for_part_1 THEN
...
END;
BEGIN
IF condition_2 THEN
statements;
END IF;
EXCEPTION
WHEN your_exceptions_for_part_2 THEN
...
END;
EXCEPTION
WHEN common_exceptions THEN
...
END;

Related

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;

Handling ORA-01403: no data found

I want to handle no data found. Whenever this exception is raised I want the program to continue, without stopping on error. Below is code snippet
BEGIN
OPEN C_TABLE_PARTITON_LIST;
LOOP
FETCH C_TABLE_PARTITON_LIST INTO TABLE_PARTITION_LIST;
EXIT WHEN C_TABLE_PARTITON_LIST%NOTFOUND;
SELECT COLUMN_NAME INTO PARTITION_COLUMN_NAME from ALL_PART_KEY_COLUMNS
sqlstring :='SELECT ( '|| PARTITION_COLUMN_NAME ||'from test';
EXECUTE IMMEDIATE sqlstring INTO F_RESULT;
exception when no_data_found then
dbms_output.put_line('no data found.');
DBMS_OUTPUT.put_line( F_RESULT);
END LOOP;
CLOSE C_TABLE_PARTITON_LIST;
END;
When I add Exception, my code is breaking with below error
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
<<
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
ORA-06550: line 29, column 3:
PLS-00103: Encountered the symbol "CLOSE" when expecting one of the following:
end not pragma final instantiable order overriding static
member constructor map
You have to enclose offending part of the script into its own BEGIN-EXCEPTION-END block, e.g.
BEGIN
OPEN C_TABLE_PARTITON_LIST;
LOOP
FETCH C_TABLE_PARTITON_LIST INTO TABLE_PARTITION_LIST;
EXIT WHEN C_TABLE_PARTITON_LIST%NOTFOUND;
begin --> you need this ...
SELECT COLUMN_NAME INTO PARTITION_COLUMN_NAME from ALL_PART_KEY_COLUMNS
sqlstring :='SELECT ( '|| PARTITION_COLUMN_NAME ||'from test';
EXECUTE IMMEDIATE sqlstring INTO F_RESULT;
exception when no_data_found then
dbms_output.put_line('no data found.');
DBMS_OUTPUT.put_line( F_RESULT);
end; --> ... and this
END LOOP;
CLOSE C_TABLE_PARTITON_LIST;
END;
Note that I just showed the way to do that. Code you posted
is incomplete (misses the DECLARE section)
is invalid (SELECT statement lacks semi-colon, and probably a WHERE clause
SQLSTRING variable won't work; 'from test' should have a leading space, otherwise that statement will be invalid
I suggest you first DBMS_OUTPUT the SQLSTRING to make sure it is correct; then execute it.

How do I handle the exception in this pl/sql for loop correctly?

First off, I'm a relative newbie to PL/SQL so I might be missing something trivial.
Here is a snippet of code that I'm having issues with running -
FOR indx IN 1 .. arr.COUNT
LOOP
SELECT COUNT(*), ca.cities
INTO tmp_count, affected_cities
FROM PDB.utilities ca
WHERE (ca.app_city_id = cityid
AND ca.app_plumbing_id = arr(indx))
AND( BITAND(options1,2) = 2
OR BITAND(options1,1) = 1)
GROUP BY ca.cities;
IF tmp_count >=0 THEN
-- We have an affected app so collect metrics
IF plumbings(indx_mv) ='0Ci30000000GsBN' THEN
count_wrigley:= count_wrigley+tmp_count;
END IF;
counter:= counter+tmp_count; --overall count.
tmp_count:=0;
affected_cities:=null;
END IF;
EXCEPTION -- error thrown here !
WHEN NO_DATA_FOUND THEN
CONTINUE;
END;
END LOOP; -- Error thrown here too.
And here is my error trace -
Error report:
ORA-06550: line 64, column 13:
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
ORA-06550: line 68, column 11:
PLS-00103: Encountered the symbol "LOOP" when expecting one of the following:
;
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
It's worth noting that the block fails only with the exception handling and compiles successfully otherwise. So my guess is I'm doing something wrong there?
Any help would be greatly appreciated! Thanks
EXCEPTION aligns with BEGIN ... END blocks. There is no BEGIN inside your loop, so there should be no exception either.
It seems the purpose of the exception is to suppress NO_DATA_FOUND errors inside the loop. So to fix this error you need to put a BEGIN / END block in the loop too. (Ah, you have an END just no BEGIN - your code would hurl with the EXCEPTION block).
FOR indx IN 1 .. arr.COUNT
LOOP
BEGIN
SELECT COUNT(*), ca.cities
INTO tmp_count, affected_cities
FROM PDB.utilities ca
....
EXCEPTION
WHEN NO_DATA_FOUND THEN
CONTINUE;
END;
END LOOP;

oracle update stored procedure with user defined exception

I am trying to create this stored procedure which should take customer no and email address as input. Then update the email address for that customer. if new email address is same as old then exception should be raised.
CREATE OR REPLACE PROCEDURE UpdateEmail
(CUSTOMERID IN CUSTOMER.CUSTOMERNO%TYPE,
N_EMAIL IN CUSTOMER.EMAIL%TYPE)
IS
DECLARE
e_same EXCEPTION;
V_ROWCOUNT NUMBER;
BEGIN
SELECT COUNT(EMAIL)
INTO V_ROWCOUNT
FROM CUSTOMER#FIT5148B
WHERE CUSTOMER.EMAIL = N_EMAIL
AND CUSTOMER.CUSTOMERNO = CUSTOMERID;
IF V_ROWCOUNT > 0
THEN RAISE e_same;
ELSE
UPDATE CUSTOMER
SET EMAIL = N_EMAIL
WHERE CUSTOMER.CUSTOMERNO = CUSTOMERID;
END IF;
EXCEPTION
WHEN e_same THEN dbms_output.put_line ('email address exist');
END;
/
But it is throwing error. Not sure if I am doing it right. I am using the latest Oracle SQL Developer.
Error(6,1): PLS-00103: Encountered the symbol "DECLARE" when expecting
one of the following: begin function pragma procedure subtype type
current
cursor delete exists prior external language The symbol "begin" was
substituted for "DECLARE" to continue.
Error(28,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 << continue close current delete fetch lock insert open
rollback savepoint set sql execute commit forall merge pipe purge
Remove DECLARE .
You should not use Declare in a Procedure. If you are writing a PLSQL block then only you should use Declare. Not in an actual Procedure body.

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