If Exists Condition Not Working with Plsql

IF EXISTS condition not working with PLSQL

IF EXISTS() is semantically incorrect. EXISTS condition can be used only inside a SQL statement. So you might rewrite your pl/sql block as follows:

declare
l_exst number(1);
begin
select case
when exists(select ce.s_regno
from courseoffering co
join co_enrolment ce
on ce.co_id = co.co_id
where ce.s_regno=403
and ce.coe_completionstatus = 'C'
and ce.c_id = 803
and rownum = 1
)
then 1
else 0
end into l_exst
from dual;

if l_exst = 1
then
DBMS_OUTPUT.put_line('YES YOU CAN');
else
DBMS_OUTPUT.put_line('YOU CANNOT');
end if;
end;

Or you can simply use count function do determine the number of rows returned by the query, and rownum=1 predicate - you only need to know if a record exists:

declare
l_exst number;
begin
select count(*)
into l_exst
from courseoffering co
join co_enrolment ce
on ce.co_id = co.co_id
where ce.s_regno=403
and ce.coe_completionstatus = 'C'
and ce.c_id = 803
and rownum = 1;

if l_exst = 0
then
DBMS_OUTPUT.put_line('YOU CANNOT');
else
DBMS_OUTPUT.put_line('YES YOU CAN');
end if;
end;

How to use if exists- if not exists in PL/SQL?

Your code is mostly good, but you would have to modify it either like this:

DECLARE
l_count NUMBER;
l_count_2 NUMBER;
BEGIN
select count(*) into l_count from table_1 where name='NAME_1';
IF l_count = 0 then
BEGIN
select count(*) into l_count_2 FROM dba_tab_cols WHERE TABLE_NAME = 'table_1' AND COLUMN_NAME='NAME_2';

IF l_count_2 > 0 THEN
sql_cnt := 'INSERT INTO table_1 (xycolumn1, xycolumn2, xycolumn3) VALUES (''value1'', ''select max(column) from table_2'', ''20'')';
ELSE
sql_cnt := 'INSERT INTO table_1 (xycolumn1, xycolumn2) VALUES (''value1'', ''select max(column) from table_2'')';
END IF;
BEGIN
EXECUTE IMMEDIATE sql_cnt ;
END;
END;
END IF;
END;

or like this:

DECLARE
l_count NUMBER;
l_count_2 NUMBER;
BEGIN
select count(*) into l_count from table_1 where name='NAME_1';
IF l_count = 0 then
BEGIN
select count(*) into l_count_2 FROM dba_tab_cols WHERE TABLE_NAME = 'table_1' AND COLUMN_NAME='NAME_2';

IF l_count_2 > 0 THEN
INSERT INTO table_1 (xycolumn1, xycolumn2, xycolumn3) VALUES ('value1', 'select max(column) from table_2', '20' );
ELSE
INSERT INTO table_1 (xycolumn1, xycolumn2) VALUES ('value1', 'select max(column) from table_2');
END IF;
END;
END IF;
END;

The first option is using the correct Oracle spelling for string creations and dynamic SQL and the second option is avoiding dynamic SQL altogether by executing INSERT on the spot (the option I prefer).

EDIT : The error you got was because you did not encapsulate your INSERT inside a string. That is what I changed for you in my first option when I mentioned correct Oracle spelling for string creations and dynamic SQL.

I hope I helped!

Oracle Function With - IF EXISTS -

You can use a variable for counting elements like this:

SELECT count(EXEMPLO.EXEMPLOID) into v_count FROM EXEMPLO WHERE EXEMPLO.EXEMPLOID = 
p_processoId

IF v_count > 0 THEN
--do something
ELSE
--do something
END IF;

Of course, you have to define the variable v_count as a number before you use it.

If you do not use the v_count variable you can get a exception like NO_DATA_FOUND.

If, ELSIF and ELSE condition in PL SQL not working

Try to add SET SERVEROUTPUT ON at the beginning of your script.

OR operator in if condition is not working in plsql block

That's because you most probably wanted AND, not OR:

if branch <> 'surat' and branch <>'vadodra' and branch <>'ahmedabad' then

Alternatively, use IN:

if branch not in ('surat', 'vadodra', 'ahmedabad') then


Related Topics



Leave a reply



Submit