Plsql 在oracle PL/SQL中更新并插入if-else条件

Plsql 在oracle PL/SQL中更新并插入if-else条件,plsql,oracle11g,plsqldeveloper,Plsql,Oracle11g,Plsqldeveloper,请通过以下代码段查找: BEGIN IF (in_config1 IS NOT NULL OR in_config1 !='') THEN UPDATE question_table SET comment = in_config1 WHERE id= id AND questionid = 1; ELSE INSERT INTO question_table( tid ,questionid

请通过以下代码段查找:

BEGIN
  IF (in_config1 IS NOT NULL OR in_config1 !='') THEN
      UPDATE question_table
      SET comment = in_config1 
      WHERE id= id
      AND questionid = 1;
  ELSE
      INSERT INTO question_table(
      tid
      ,questionid 
      ,comments) 
      VALUES( id
      , 1
      , in_config1);
 END IF;
 END;
我的要求是根据某些条件更新问题表。如果更新失败(如果记录不存在,则更新失败),则需要在else块中添加insert语句。
在上面的代码中,更新正在工作。但是insert语句没有执行。请告诉我出了什么问题?

如果我理解您的意思,您需要upsert语句,如果记录与某个值匹配,则更新该语句,如果不匹配,则插入该语句。在这种情况下,最好的选择是条款。它高效、灵活、易读。以下是一个通用脚本,它可能需要根据从何处获取值以及表结构进行细微更改

MERGE INTO question_table a   
USING (SELECT id, your_key, in_config1 FROM DUAL) b   
ON (a.id = b.id)   
WHEN MATCHED THEN   
UPDATE question_table   
SET comment = in_config1   
WHEN NOT MATCHED THEN   
INSERT INTO question_table(   
      tid  
     ,questionid    
     ,comments)   
     VALUES( id  
     , 1  
     , in_config1);   

只需使用sql%notfound就可以这样做

  BEGIN
    IF (in_config1 IS NOT NULL OR in_config1 != '') THEN
      UPDATE question_table
         SET comment = in_config1
       WHERE id = id
         AND questionid = 1;
      if sql%notfound then
        INSERT INTO question_table (tid, questionid, comments) VALUES (id, 1, in_config1);
      end if;
    END IF;
  exception
    when others then
      dbms_output.put_line(sqlerrm);
  END;

你有什么错误吗?您尝试了哪些样本数据?没有,我没有收到任何错误。基本上我想把它修改成upsert语句。如果行匹配,则更新行,否则插入行