如果存在,如何使用-如果PL / SQL中不存在?

时间:2019-09-18 14:12:52

标签: oracle if-statement plsql dynamic-sql

我正在尝试将if存在语句从SQL Server转换为PL / SQL,但是出现错误。

我正在尝试检查NAME_1中是否不存在table_1,如果不存在,那么我正在检查COLUMN_NAME='NAME_2'中是否存在table_1 ,如果存在的话   insert (NAME_1 and NAME_2)进入我的table_2。 谢谢

T-SQL(SQL服务器):

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;        

错误

  

错误报告
  ORA-06550:第11行,第27列:
  PLS-00103:期望其中一个

时遇到符号“ INSERT”

3 个答案:

答案 0 :(得分:2)

您的代码大部分都很好,但是您必须像这样修改它:

<li><a id="navLinks" onclick="swapStyleSheet('/stylesheets/light.css')">Light Style!</a></li>
<li><a id="navLinks" onclick="swapStyleSheet('/stylesheets/main.css')">Dark Style!</a></li>

或类似这样:

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;      

第一个选择是使用正确的Oracle拼写进行字符串创建和动态SQL,第二个选择是当场执行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; (我更喜欢该选项),从而完全避免了动态SQL。

编辑::您得到的错误是因为您没有将INSERT封装在字符串中。当我提到INSERT时,这就是我为第一个选择为您所做的更改。

希望我能帮上忙!

答案 1 :(得分:2)

提供的table_2没有第三列的DEFAULT值,您可以像这样简单得多:

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;

没有理由使用动态SQL。

答案 2 :(得分:0)

(将Oracle版本和错误消息插入该问题之前给出的答案)

如您在此处看到的:IF-THEN-ELSE Statement,Oracle的语法不同

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

即,没有BEGIN ... END块。相反,该语句以END IF

结尾