ORA-00933:使用INSERT INTO ... SELECT语句未正确结束SQL命令

时间:2017-03-18 03:13:28

标签: sql oracle oracle11gr2

我偶然解决了我的问题,但我真的想知道它为什么会起作用:),

这就是事情:

执行以下SQL语句时出现ORA-00933: SQL command not properly ended错误:

  INSERT INTO BASP_DX.QLR@GT(BDCDYH, QSZT)
  SELECT NVL(e.BDCDYH, ' '),b.LIFECYCLE AS QSZT
  FROM DJ_DY a
    LEFT JOIN DJ_XGDJGL d
      ON d.ZSLBH = a.SLBH
    LEFT JOIN DJ_DJB e
      ON e.SLBH = d.FSLBH
        AND e.SLBH = '0123456789'
    LEFT JOIN DJ_QLRGL f
      ON f.SLBH = e.SLBH
        AND f.QLRLX = 'Person1'
    LEFT JOIN DJ_QLRGL b
      ON b.SLBH = a.SLBH
        AND (b.QLRLX = 'Person2' OR (b.QLRLX = 'Person3' AND b.QLRID = f.QLRID))
  WHERE a.SLBH = '12345'
    AND e.SLBH IS NOT NULL
    -- add the condition to ensure that 
    -- this statement and the second statement get the same result 
    AND b.QLRID IS NOT NULL
    AND (a.LIFECYCLE = '0' OR a.LIFECYCLE IS NULL);

我从原始SQL语句中删除所有不必要的插入值,相关表和条件,以专注于问题部分。

然后我谷歌,从这post我知道原因可能是:

  1. 带有ORDER BY子句或INNER JOIN的INSERT语句
  2. 带有INNER JOIN或ORDER BY子句的DELETE语句
  3. 带有INNER JOIN的UPDATE语句
  4. 显然,这些不是我的类型。我没有使用INNER JOIN和ORDER BY,我使用的都是LEFT JOIN语句,所以我想我可能是因为我用LEFT JOIN语句设置了太多条件(例如LEFT JOIN DJ_QLRGL b),所以我尝试在WHERE子句之后移动条件,它看起来像这样:

      INSERT INTO BASP_DX.QLR@GT(BDCDYH, QSZT)
      SELECT NVL(e.BDCDYH, ' '),b.LIFECYCLE AS QSZT
      FROM DJ_DY a
        LEFT JOIN DJ_XGDJGL d
          ON d.ZSLBH = a.SLBH
        LEFT JOIN DJ_DJB e
          ON e.SLBH = d.FSLBH
            AND e.SLBH = '0123456789'
        LEFT JOIN DJ_QLRGL f
          ON f.SLBH = e.SLBH
            AND f.QLRLX = 'Person1'
        LEFT JOIN DJ_QLRGL b
          ON b.SLBH = a.SLBH
        -- this conditions move to WHERE clause
      WHERE a.SLBH = '12345'
        AND e.SLBH IS NOT NULL
        -- here is the original LEFT JOIN condition
        AND (b.QLRLX = 'Person2' OR (b.QLRLX = 'Person3' AND b.QLRID = f.QLRID))
        AND (a.LIFECYCLE = '0' OR a.LIFECYCLE IS NULL);
    

    然后它的作品! 但为什么呢?

    我只是想知道这种情况的原因。

    Solution

    问题是三角形连接,LEFT JOIN条件不能包含有关自连接表的条件,在这种情况下,它是b.QLRID = f.QLRID,所以当我删除b.QLRID = f.QLRID条件时,它的工作原理。

1 个答案:

答案 0 :(得分:1)

首先,“DJ_QLRGL b”的连接不再是LEFT,  如何WHERE条件排除没有找到b对应的行