语法错误:期望的name_wo_function_call

时间:2014-06-12 09:46:07

标签: sql plsql syntax-error

我正在尝试编写一个脚本,将两个表(table2table3)的内容合并为一个(table1)。

我在Oracle实例上。

这是我的代码。

DECLARE
region VARCHAR2 :='REG1'; -- just for testing
BEGIN
  CASE
      WHEN region = 'REG1' THEN
          BEGIN
              MERGE INTO table1 USING (
                  SELECT 
                      field1 table2field1,
                      field2 table2field2
                  FROM table2
              ) ON (
                  field1 = table2field1
              )
              WHEN MATCHED THEN 
                  UPDATE SET
                      field2 = table2field2
              WHEN NOT MATCHED THEN
                  INSERT (
                      field1,
                      field2
                  ) VALUES (
                      table2field1,
                      table2field2
                  );
              COMMIT;
          END;
      WHEN region = 'REG2' THEN
          BEGIN
              MERGE INTO table1 USING (
                  SELECT 
                      field1 table3field1,
                      field2 table3field2
                  FROM table3
              ) ON (
                  field1 = table3field1
              )
              WHEN MATCHED THEN 
                  UPDATE SET
                      field2 = table3field2
              WHEN NOT MATCHED THEN
                  INSERT (
                      field1,
                      field2
                  ) VALUES (
                      table3field1,
                      table3field2
                  );
              COMMIT;
          END;
  END;
  EXCEPTION
      WHEN NO_DATA_FOUND THEN
          NULL;
      WHEN OTHERS THEN
         RAISE;
END;

SQL Developer说:

Syntax error. Expected:
    name_wo_function_call
    name
    procedure_call

现在,我对plsql很新,我敢肯定必须有一些我不知道的东西。

如何修复该错误?

1 个答案:

答案 0 :(得分:2)

小变化:

WHEN region ='REG2'部分,您有:

   SELECT field1 table3field1,
          field2 table3field2,  -- extra comma
    FROM table3

from子句之前的额外逗号不正确。

同样

WHEN NOT MATCHED THEN
                  INSERT (
                      field1,
                      field2,  -- extra comma
                  ) VALUES (
                      table3field1,
                      table3field2,  -- extra comma
                  );
              COMMIT;

虽然我怀疑这更多是复制+粘贴+拙劣的匿名化错误..

此外,CASE语句应以END CASE结尾,因此它应该看起来像

DECLARE
  region  VARCHAR2 := 'REG1';                                                              -- just for testing
BEGIN
  CASE
    WHEN region = 'REG1' THEN
      BEGIN
        MERGE INTO table1
             USING (SELECT field1 table2field1, field2 table2field2 FROM table2)
                ON (field1 = table2field1)
        WHEN MATCHED THEN
          UPDATE SET field2 = table2field2
        WHEN NOT MATCHED THEN
          INSERT     (field1, field2)
              VALUES (table2field1, table2field2);

        COMMIT;
      END;
    WHEN region = 'REG2' THEN
      BEGIN
        MERGE INTO table1
             USING (SELECT field1 table3field1, field2 table3field2 FROM table3)
                ON (field1 = table3field1)
        WHEN MATCHED THEN
          UPDATE SET field2 = table3field2
        WHEN NOT MATCHED THEN
          INSERT     (field1, field2)
              VALUES (table3field1, table3field2);

        COMMIT;
      END;

      NULL;
  END CASE; -- end case, not just end
EXCEPTION
  WHEN NO_DATA_FOUND THEN
    NULL;
  WHEN OTHERS THEN
    RAISE;
END;