satcha
satcha

Reputation: 129

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

I am trying to convert if exists statement from SQL-Server to PL/SQL but having an error.

I am trying to check if NAME_1 doesn't exist in my table_1, if they don't exist then I am checking if COLUMN_NAME='NAME_2' exist in my table_1, if it exist then insert (NAME_1 and NAME_2) into my table_2. Thanks

T-SQL (SQL-Server):

IF NOT (EXISTS (SELECT * from table_name_1 where name='NAME_1'))
BEGIN
    IF (EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'table_1' AND COLUMN_NAME='NAME_2'))

        EXEC('insert into table_name_1 values (''value1'', (select max(column) from table_2), 20)');
    ELSE
        EXEC('insert into table_name_1 values (''value1'', (select max(column) from table_2))');
END

PL/SQL (Oracle):

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  VALUES ('value1', "select max(column) from table_2" , '20' );          
        ELSE             
            sql_cnt:= INSERT INTO table_1  VALUES ('value1', "select max(column) from table_2" );
        END IF;                    
       BEGIN
         EXECUTE IMMEDIATE sql_cnt ;
       END;
    END;
END IF;       
END;        

ERROR

Error report
ORA-06550: line 11, column 27:
PLS-00103: Encountered the symbol "INSERT" when expecting one of the

Upvotes: 0

Views: 8832

Answers (3)

Goran Kutlaca
Goran Kutlaca

Reputation: 2024

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!

Upvotes: 2

Wernfried Domscheit
Wernfried Domscheit

Reputation: 59476

Provided table_2 has no DEFAULT value for third column you can do it much simpler like this one:

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
        SELECT COUNT(*) INTO l_count_2 FROM dba_tab_cols  WHERE TABLE_NAME = 'table_1' AND COLUMN_NAME='NAME_2';

        INSERT INTO table_1
        SELECT 'value1', MAX(column), CASE WHEN l_count_2 > 0 THEN 20 ELSE NULL end 
        FROM table_2;

    END IF;       
END;

There is no reason for dynamic SQL.

Upvotes: 2

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112382

(answer given before the Oracle version and the error message were inserted into the question)

As you can see here: IF-THEN-ELSE Statement, the syntax is different for Oracle

IF condition THEN
   {...statements to execute when condition is TRUE...}
ELSE
   {...statements to execute when condition is FALSE...}
END IF;

I.e., there is no BEGIN ... END block. Instead the statement ends with END IF

Upvotes: 0

Related Questions