raise Exception inside if condition in plsql - sql

declare
cnt number;
begin
select count(*)
into cnt
FROM employee;
IF cnt = 0 THEN
DELETE employee;
COMMIT;
EXCEPTION
WHEN others THEN
log_error_local(k_sub_module, l_step||' '||sqlerrm);
raise;
END IF;
end
getting syntax error like below..
Error(188,13): PLS-00103: Encountered the symbol "EXCEPTION" when expecting one of the following: ( begin case declare else elsif 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

To fix your syntax issues, you can wrap the contents of the IF .. THEN statement in a BEGIN/END block:
DECLARE
cnt number;
BEGIN
select count(*)
into cnt
FROM employee;
IF cnt = 0 THEN
BEGIN
DELETE FROM employee;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
log_error_local(k_sub_module, l_step||' '||sqlerrm);
raise;
END;
END IF;
END;
/
Note: the DELETE statement is missing the FROM keyword and you do not appear to have declared the k_sub_module or l_step variables.
or, since you are re-raising the exception and execution will be halted anyway, catch the exception outside the IF statement in the main block:
DECLARE
cnt number;
BEGIN
select count(*)
into cnt
FROM employee;
IF cnt = 0 THEN
DELETE FROM employee;
COMMIT;
END IF;
EXCEPTION
WHEN OTHERS THEN
log_error_local(k_sub_module, l_step||' '||sqlerrm);
raise;
END;
/
db<>fiddle here

Related

it is showing error that ORA-00933: SQL command not properly ended, What to do?

select * from student_79;
DECLARE
total_rows number(2);
BEGIN
UPDATE student_10
SET age = age + 2;
IF sql%notfound THEN
dbms_output.put_line('no student updated');
ELSIF sql%found THEN
total_rows :=sql%rowcount;
dbms_output.put_line(total_rows || ' student updated ');
END IF;
END;
i check your code it is working goods no any error return.

How to write a condition to do something where a for loop returns no rows

In this loop I would like to capture the function not returning data in the loop to execute an else command or an exception when there is no data. But this does not work. Please help me out with the correct syntax or a work around thanks!!
begin
--looping
for i in (
select x, y, z, rownum
from period
where x = 0 -- here this query does not return a row
) loop
begin
if sql%rowcount >=1 -- tried row count
then
dbms_output.put_line ('blablabla');
else
dbms_output.put_line ('blueeeeeee');
end if;
exception when no_data_found --tried this exception
then
dbms_output.put_line ('black');
end;
end loop;
end;
If your query in for-loop doesn't return rows, you will never get to body of this loop, so
you should use either use open-cursor-fetch-close or manual checks like:
declare
loop_flag boolean:=false;
loop_n int:=0;
begin
--looping
for i in (
select x, y, z, rownum
from period
where x = 0 -- here this query does not return a row
)
loop
-- processing fetched row:
loop_n:=loop_n+1;
loop_flag:=true;
if loop_n = 1
then
dbms_output.put_line ('first row: ' || r.x );
else
dbms_output.put_line ('other rows...');
end if;
end loop;
if not loop_flag then
dbms_output.put_line ('no data found...');
end;
end;

Encountered the symbol "EXCEPTION" error in stored procedure

I am programming a procedure in an Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production.
I have an exception inside a LOOP because I don't want the procedure to exit the LOOP if an exception is thrown.
create or replace procedure PARSE_REGISTER_MESSAGE
IS
HOTELS_TO_PROCESS number := 5000;
cursor unparsed_messages is
SELECT REGISTER_psd_id, message
FROM
( SELECT REGISTER_psd_id, message
FROM cc_owner.REGISTER_psd
WHERE parsed != 1
OR parsed IS NULL
ORDER BY CREATION_DATE DESC)
WHERE rownum < HOTELS_TO_PROCESS;
BEGIN
FOR psd_rec in unparsed_messages
LOOP
p_msg.parse_msg (psd_rec.REGISTER_psd_id, null, psd_rec.message);
EXCEPTION
WHEN OTHERS
THEN
DECLARE
l_code INTEGER := SQLCODE;
BEGIN
of_owner.p_db_trc.add_error
( 'PARSE_REGISTER_MESSAGE','',
l_code,
sys.DBMS_UTILITY.format_error_stack,
sys.DBMS_UTILITY.format_error_backtrace,
sys.DBMS_UTILITY.format_call_stack );
END;
END LOOP;
END;
But I can't compile the package due this error:
Error(25,10): 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
I Also tried:
create or replace procedure PARSE_REGISTER_MESSAGE
IS
HOTELS_TO_PROCESS number := 5000;
cursor unparsed_messages is
SELECT REGISTER_psd_id, message
FROM
( SELECT REGISTER_psd_id, message
FROM cc_owner.REGISTER_psd
WHERE parsed != 1
OR parsed IS NULL
ORDER BY CREATION_DATE DESC)
WHERE rownum < HOTELS_TO_PROCESS;
psd_rec unparsed_messages%ROWTYPE;
BEGIN
FOR psd_rec in unparsed_messages
LOOP
BEGIN
p_msg.parse_msg (psd_rec.REGISTER_psd_id, null, psd_rec.message);
EXCEPTION
WHEN OTHERS
THEN
DECLARE
l_code INTEGER := SQLCODE;
BEGIN
of_owner.p_db_trc.add_error
( 'PARSE_REGISTER_MESSAGE','',
l_code,
sys.DBMS_UTILITY.format_error_stack,
sys.DBMS_UTILITY.format_error_backtrace,
sys.DBMS_UTILITY.format_call_stack );
END;
END LOOP;
END;
But then I got this error:
Error(48,4): PLS-00103: Encountered the symbol ";" when expecting one of the following: loop
Try using Begin after loop keyword as one BEGIN is missing
FOR psd_rec in unparsed_messages
LOOP
BEGIN
p_msg.parse_msg (psd_rec.REGISTER_psd_id, null, psd_rec.message);
EXCEPTION
WHEN OTHERS
THEN
DECLARE
l_code INTEGER := SQLCODE;
BEGIN
of_owner.p_db_trc.add_error
( 'PARSE_REGISTER_MESSAGE','',
l_code,
sys.DBMS_UTILITY.format_error_stack,
sys.DBMS_UTILITY.format_error_backtrace,
sys.DBMS_UTILITY.format_call_stack );
END;
The syntax for a PLSQL block / procedure is :
DECLARE
-- Here you declare all the varaible used in block
BEGIN
-- Here you write the body of the Block
EXCEPTION
-- Here you write the exceptions which you want to handle.
END;
Now when i look at your code, you have written Exception block inside the FOR LOOP, which will work only if you use the above syntax. In you case the scope of Exception block is not identified by Oracle and hence it throws error.
FOR psd_rec IN unparsed_messages
LOOP
p_msg.parse_msg (psd_rec.REGISTER_psd_id, NULL, psd_rec.MESSAGE);
EXCEPTION --<-- Wrong way of using Excepton block. Scope of this Exception block is not resolved
WHEN OTHERS
THEN
DECLARE
l_code INTEGER := SQLCODE;
BEGIN
of_owner.p_db_trc.add_error
( 'PARSE_REGISTER_MESSAGE','',
l_code,
sys.DBMS_UTILITY.format_error_stack,
sys.DBMS_UTILITY.format_error_backtrace,
sys.DBMS_UTILITY.format_call_stack );
END;
You must modify your code as below to include the Exception block in for loop;
CREATE OR REPLACE PROCEDURE PARSE_REGISTER_MESSAGE
IS
HOTELS_TO_PROCESS NUMBER := 5000;
l_code INTEGER := SQLCODE;
CURSOR unparsed_messages
IS
SELECT REGISTER_psd_id, MESSAGE
FROM ( SELECT REGISTER_psd_id, MESSAGE
FROM cc_owner.REGISTER_psd
WHERE parsed != 1 OR parsed IS NULL
ORDER BY CREATION_DATE DESC)
WHERE ROWNUM < HOTELS_TO_PROCESS;
BEGIN
FOR psd_rec IN unparsed_messages
LOOP
BEGIN
p_msg.parse_msg (psd_rec.REGISTER_psd_id, NULL, psd_rec.MESSAGE);
EXCEPTION
WHEN OTHERS
THEN
of_owner.p_db_trc.add_error (
'PARSE_REGISTER_MESSAGE',
'',
l_code,
sys.DBMS_UTILITY.format_error_stack,
sys.DBMS_UTILITY.format_error_backtrace,
sys.DBMS_UTILITY.format_call_stack);
END;
END LOOP;
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line (SQLERRM);
END;
You second try has missing END statement and thats why you were getting error. See below:
CREATE OR REPLACE PROCEDURE PARSE_REGISTER_MESSAGE
IS
HOTELS_TO_PROCESS NUMBER := 5000;
l_code INTEGER := SQLCODE;
CURSOR unparsed_messages
IS
SELECT REGISTER_psd_id, MESSAGE
FROM ( SELECT REGISTER_psd_id, MESSAGE
FROM cc_owner.REGISTER_psd
WHERE parsed != 1 OR parsed IS NULL
ORDER BY CREATION_DATE DESC)
WHERE ROWNUM < HOTELS_TO_PROCESS;
psd_rec unparsed_messages%ROWTYPE;
BEGIN
FOR psd_rec IN unparsed_messages
LOOP
BEGIN
p_msg.parse_msg (psd_rec.REGISTER_psd_id, NULL, psd_rec.MESSAGE);
EXCEPTION
WHEN OTHERS
THEN
BEGIN
of_owner.p_db_trc.add_error (
'PARSE_REGISTER_MESSAGE',
'',
l_code,
sys.DBMS_UTILITY.format_error_stack,
sys.DBMS_UTILITY.format_error_backtrace,
sys.DBMS_UTILITY.format_call_stack);
END;
END;
END LOOP;
END;

PL/SQL if table not exist create

Hello i use oracle SQL developer
I have create a procedure, and i need to check if a table exist, if not exist i must create how can do?
I have try this
DECLARE v_emp int:=0;
BEGIN
SELECT count(*) into v_emp FROM dba_tables;
if v_emp = 0 then
EXECUTE IMMEDIATE 'create table EMPLOYEE ( ID NUMBER(3), NAME VARCHAR2(30) NOT NULL)';
end if;
END;
but give me an error 00103 because not find table
Just execute the create and watch the exception if thrown. Oracle would never replace the DDL of a table.
declare
error_code NUMBER;
begin
EXECUTE IMMEDIATE 'CREATE TABLE EMPLOYEE(AGE INT)';
exception
when others then
error_code := SQLCODE;
if(error_code = -955)
then
dbms_output.put_line('Table exists already!');
else
dbms_output.put_line('Unknown error : '||SQLERRM);
end if;
end;
You can run this for example:
if (select count(*) from all_tables where table_name = 'yourTable')>0 then
-- table exists
else
-- table doesn't exist
end if;
You should try following,
declare
nCount NUMBER;
v_sql LONG;
begin
SELECT count(*) into nCount FROM dba_tables where table_name = 'EMPLOYEE';
IF(nCount <= 0)
THEN
v_sql:='
create table EMPLOYEE
(
ID NUMBER(3),
NAME VARCHAR2(30) NOT NULL
)';
execute immediate v_sql;
END IF;
end;

Getting an error in sql, when executing code below.How to declare a table type in plsql. Am a beginner . Please suggest

create or replace procedure BAS_NUM_UPD is
cursor cur is
select distinct o.oi_b,mpr.pa_ke_i,ltrim(substr(convert_171_to_711(cp.p_t_num),1,7),'0') bs_nbr
from t_obj o, mat_pa_rel mp, cor_pa cp
where o.ob_t = 'something'
and o.oi_b = mp.oi_b
and mp.pa_ke_i = cp.pa_ke_i;
l_ba_num_at_i number(10) := get_attribute_id('Ba timber');
flag1 VARCHAR2(10);
type t1 is table of varchar2(10);
par_k t1;
BEGIN
for x in cur loop
BEGIN
select pa_ke_i into par_k from mat_pa_rel where oi_b=x.oi_b ;
if par_k.count=null then
insert into cs_val (oi_b, at_i, value, flag, ) values (x.oi_b, l_ba_num_at_i, null, 1);
end if;
select flag into flag1 from cs_val where at_i = l_ba_num_at_i and oi_b = x.oi_b
and value = x.bs_nbr;
EXCEPTION
when NO_DATA_FOUND THEN
insert into cs_val (oi_b, at_i, value, flag, )
values (x.oi_b, l_ba_num_at_i, x.bs_nbr, 1);
flag1 :='Nothing';
when OTHERS
then
raise_application_error(-20011,'Unknown Exception in PROCEDURE');
END;
end loop;
end BAS_NUM_UPD;
error:
PLS-00642: local collection types not allowed in SQL statements
You should get it running if you do a bulk collect
select pa_ke_i bulk collect into par_k from mat_pa_rel where oi_b=x.oi_b ;
Then I think the if is not right. I think you need to do
if par_k.count = 0 then
But to be honest you might just make a count
select count(*) into l_cnt from mat_pa_rel where oi_b=x.oi_b;
If l_cnt = 0 then ...
Of course l_cnt has to be defined.
You should create type t1 in the schema and not in the pl/sql block.