Oracle Query: Two/Multiple simple IF condition blocks in the same SQL file not executing

29 Views Asked by At

The SQL file contains Two simple IF conditions, but it is not executing in TOAD:

IF (SELECT COUNT(COLUMNNAME1) FROM TABLENAME1 WHERE COLUMNNAME1 = 'UNIQUEID1') > 0)
BEGIN
(UPDATE QUERY1)
END;

IF (SELECT COUNT(COLUMNNAME1) FROM TABLENAME1 WHERE COLUMNNAME1 = 'UNIQUEID2') > 0)
BEGIN
(UPDATE QUERY2)
END;

But these are individually executing successfully, please let me know if the query is missing something.

Executed successfully and IF condition passes to update the table in the database

1

There are 1 best solutions below

0
pmdba On

Should be more like this:

BEGIN        
    IF (SELECT COUNT(COLUMNNAME1) FROM TABLENAME1 
         WHERE COLUMNNAME1 = 'UNIQUEID1') > 0 THEN
        UPDATE QUERY1;
    END IF;

    IF (SELECT COUNT(COLUMNNAME1) FROM TABLENAME1 
         WHERE COLUMNNAME1 = 'UNIQUEID2') > 0 THEN
        UPDATE QUERY2;
    END IF;
END;

You may want to commit your changes somewhere in this script, too.