在CURSOR中使用Insert语句中的where子句

时间:2018-02-01 02:23:27

标签: sql database oracle plsql

我正在运行此代码并收到此错误ORA-00933 SQL command not properly ended

您如何编写此代码以便此错误不存在? member_addresses_to_remove是CURSOR

 FOR curr_element IN member_addresses_to_remove 
  LOOP
    INSERT INTO schema.tablename(ID,NAME, PHONE_NUMBER)
    VALUES (curr_element.ID, curr_element.NAME,curr_element.PHONE_NUMBER)
    WHERE NOT EXISTS (SELECT 1
                      FROM SCHEMA.OTHERTABLE OT
                      WHERE OT.ID = curr_element.ID);                        
  END LOOP;
COMMIT;

2 个答案:

答案 0 :(得分:2)

您的语法不正确,因为您正在将values子句与where子句组合在一起。

试试这个:

 FOR curr_element IN member_addresses_to_remove 
  LOOP
    INSERT INTO schema.tablename(ID,NAME, PHONE_NUMBER)
    SELECT curr_element.ID, curr_element.NAME,curr_element.PHONE_NUMBER
    FROM DUAL
    WHERE NOT EXISTS (SELECT 1
                      FROM SCHEMA.OTHERTABLE OT
                      WHERE OT.ID = curr_element.ID);                        
  END LOOP;
COMMIT;

答案 1 :(得分:0)

插入语句语法错误,请查看以下示例。

INSERT INTO trg_tbl (col1, col2, col3)
SELECT col1,
       col2,
       col3
FROM src_tbl
WHERE col4 = myvale;

通过在SQLPlus中执行单个SQL语句来测试SQL,然后添加它的过程