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

混江龙づ霸主 提交于 2020-01-30 11:31:47

问题


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


回答1:


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!




回答2:


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.




回答3:


(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



来源:https://stackoverflow.com/questions/57994834/how-to-use-if-exists-if-not-exists-in-pl-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!