从CTE插入

时间:2014-06-03 06:18:03

标签: sql oracle oracle11g common-table-expression

  WITH DTL AS
  (SELECT CMPI_CODE,
         CMN_CDTY_MTRL,
         CMI_WT_FACTOR,
         CMI_CNTRCT_RATE,
         'PL',
         PRESENT_PRICE,
         TRM_CODE,
         ROUND(((NVL(PRESENT_PRICE,1)*CMI_WT_FACTOR) / CMI_CNTRCT_RATE),2) AS PL_FACTOR
  FROM VW_CMD_MATERIAL WHERE TRM_CODE = 41)
  INSERT iNTO IPA_PRCADJ_HDR(TRM_CODE,IPAPH_ADJ_FACTOR,IPAPH_AMT_CUR,IPAPH_REMARKS)
  SELECT TRM_CODE,SUM(PL_FACTOR) AS PL_FACTOR,((SUM(PL_FACTOR)*10)) AS AMT_CUR,'asdf' 
  FROM DTL GROUP BY (TRM_CODE);

显示错误ORA-00928: missing SELECT keyword

2 个答案:

答案 0 :(得分:26)

这是从CTE插入表格的语法:

-- CREATE TABLE tmp ( tmp_id NUMBER(10) );

INSERT INTO tmp( tmp_id )
  WITH cte AS (
    SELECT 1 AS tmp_id FROM dual
  )
  SELECT tmp_id
  FROM cte;

答案 1 :(得分:4)

似乎在公用表表达式之后,Insert不能用作第一个语句。试试这个,

    INSERT iNTO IPA_PRCADJ_HDR(TRM_CODE,IPAPH_ADJ_FACTOR,IPAPH_AMT_CUR,IPAPH_REMARKS)
    SELECT * FROM 
    (

      WITH DTL AS
      (SELECT CMPI_CODE,
             CMN_CDTY_MTRL,
             CMI_WT_FACTOR,
             CMI_CNTRCT_RATE,
             'PL',
             PRESENT_PRICE,
             TRM_CODE,
             ROUND(((NVL(PRESENT_PRICE,1)*CMI_WT_FACTOR) / CMI_CNTRCT_RATE),2) AS PL_FACTOR
      FROM VW_CMD_MATERIAL WHERE TRM_CODE = 41
      )
      SELECT TRM_CODE,SUM(PL_FACTOR) AS PL_FACTOR,((SUM(PL_FACTOR)*10)) AS AMT_CUR,'asdf' 
      FROM DTL GROUP BY (TRM_CODE)
    );