WHERE查询中发生错误

时间:2013-06-26 02:18:22

标签: sql oracle

主要查询效果很好。但WHERE查询似乎是问题所在。 以下是WHERE查询。

#startDt# <!--[CDATA[<=]]--> TO_CHAR(TO_DATE(DAY, 'YYYY-MM-DD'), 'YYYYMMDD') AND #endDt# <!--[CDATA[-->=]]> TO_CHAR(TO_DATE(DAY, 'YYYY-MM-DD'), 'YYYYMMDD')

然后它给了'ORA-01841 :(完整)年必须在-4713和+9999之间,而不是0' 所以我把TO_CHAR弄清楚如下。

#startDt# <= (TO_DATE(DAY, 'YYYY-MM-DD'), 'YYYYMMDD') AND #endDt# >= (TO_DATE(DAY, 'YYYY-MM-DD'), 'YYYYMMDD')

然后它给了我ora-017'97这个运算符必须跟随任何或所有'

DAY的数据类型是String。

任何人都可以解决这个问题吗?

哦,这是我的全部问题。

<select id="ContentStatis.statis" parameterClass="map" resultMap="ContentStatis">
SELECT X.*
  FROM
      ( 
        SELECT 
                    (CASE
                             WHEN DAY.DAY = '단위 합계' THEN DAY.DAY
                             ELSE TO_CHAR (TO_DATE (DAY.DAY, 'YYYY-MM-DD'), 'YYYYMMDD') 
                       END
                    ) AS DAY,
                    DAY.WEEK,
                    MNOT.SUM_STD_CNT AS MNOT_CNT,
                    RSC.MOVIE_500K AS MOVIE_500K_CNT,
                    RSC.MOVIE_1M AS MOVIE_1M_CNT,
                    RSC.MOVIE AS MOVIE_CNT,
                    RSC.EXAM AS EXAM_CNT,
                    RSC.WEB AS WEB_CNT,
                    RSC.IMG AS IMG_CNT,
                    RSC.INTERRAC AS INTERRACTIVE_CNT,
                    RSC.DOC AS DOC_CNT,
                    MNOT.SUM_STD_CNT + RSC.MOVIE_500K + RSC.MOVIE_1M + RSC.MOVIE + RSC.EXAM + RSC.WEB + RSC.IMG + RSC.INTERRAC + RSC.DOC TOT_CNT
          FROM 
                    /* 날짜 */
                   (SELECT 
                                TO_CHAR (TO_DATE ('20130501','YYYY-MM-DD')+LEVEL- 1, 'YYYYMMDD') AS DAY
                               ,CEIL(
                                      (
                                        to_number(substrb(TO_CHAR (TO_DATE ('20130501','YYYY-MM-DD')+LEVEL- 1, 'YYYYMMDD'), -2, 2)) 
                                      + 7 
                                      - to_number(TO_CHAR(TO_DATE(TO_CHAR (TO_DATE ('20130501','YYYY-MM-DD')+LEVEL- 1, 'YYYYMMDD'),'YYYYMMDD'),'D'))
                                      )/7 
                                     )|| ' 주차'  WEEK
                      FROM DUAL
                      CONNECT BY TO_DATE('20130501', 'YYYY-MM-DD') + LEVEL - 1  <![CDATA[<=]]>  TO_DATE('20130530', 'YYYY-MM-DD')
                      UNION ALL
                      SELECT '단위 합계', '' FROM DUAL
                    ) DAY LEFT OUTER JOIN
                   /* 리소스 */
                    (
                     SELECT 
                                 NVL(DT_G.COMM_DT, '단위 합계') COMM_DT
                                ,NVL(SUM(DT_G.MOVIE_500K), 0) AS MOVIE_500K
                                ,NVL(SUM(DT_G.MOVIE_1M), 0) AS MOVIE_1M
                                ,NVL(SUM(DT_G.MOVIE), 0) AS MOVIE
                                ,NVL(SUM(DT_G.EXAM), 0) AS EXAM
                                ,NVL(SUM(DT_G.DOC), 0) AS DOC 
                                ,NVL(SUM(DT_G.IMG), 0) AS IMG
                                ,NVL(SUM(DT_G.WEB), 0) AS WEB
                                ,NVL(SUM(DT_G.INTERRAC), 0) AS INTERRAC
                       FROM 
                                (
                                 SELECT
                                              COMM_DT
                                            , CASE SUMT.RSC_TP_DSCD WHEN 'RTP10' THEN SUMT.SUM_500K END MOVIE_500K
                                            , CASE SUMT.RSC_TP_DSCD WHEN 'RTP10' THEN SUMT.SUM_1M END AS MOVIE_1M
                                            , CASE SUMT.RSC_TP_DSCD WHEN 'RTP10' THEN SUMT.SUM_500K+SUMT.SUM_1M END AS MOVIE
                                            , CASE SUMT.RSC_TP_DSCD WHEN 'RTP11' THEN SUMT.SUM_STD_CNT END AS EXAM
                                            , CASE SUMT.RSC_TP_DSCD WHEN 'RTP12' THEN SUMT.SUM_STD_CNT END AS DOC
                                            , CASE SUMT.RSC_TP_DSCD WHEN 'RTP13' THEN SUMT.SUM_STD_CNT END AS IMG
                                            , CASE SUMT.RSC_TP_DSCD WHEN 'RTP14' THEN SUMT.SUM_STD_CNT END AS WEB
                                            , CASE SUMT.RSC_TP_DSCD WHEN 'RTP01' THEN SUMT.SUM_STD_CNT END AS INTERRAC
                                  FROM (
                                              SELECT RSC_TP_DSCD, SUM(STDY_CNT) AS SUM_STD_CNT, SUM(MOVIE_STDY_CNT_N1M) AS SUM_1M, SUM(MOVIE_STDY_CNT_N500K) AS SUM_500K, COMM_DT
                                              FROM (
                                                        SELECT RSC_SNO, STDY_CNT, MOVIE_STDY_CNT_N1M, MOVIE_STDY_CNT_N500K, COMM_DT
                                                          FROM LRMS.V_EBSM_PKG_RSC_COMM_CNT
                                                        WHERE PKG_SNO = 0  AND RSC_SNO != 0
                                                        AND COMM_DT BETWEEN TO_CHAR(TO_DATE('20130501', 'YYYY-MM-DD'), 'YYYYMMDD') AND TO_CHAR(TO_DATE('20130530', 'YYYY-MM-DD'), 'YYYYMMDD')
                                                        ) CNT
                                                   LEFT OUTER JOIN LRMS.V_LRRM_RSC RSC ON CNT.RSC_SNO = RSC.RSC_SNO
                                              GROUP BY RSC_TP_DSCD, COMM_DT
                                            ) SUMT
                                ) DT_G
                         GROUP BY ROLLUP(DT_G.COMM_DT)
                    ) RSC ON DAY.DAY = RSC.COMM_DT
                    /* M노트 */
                    LEFT OUTER JOIN
                    (
                      SELECT NVL(SUM(STDY_CNT), 0) AS SUM_STD_CNT, NVL(COMM_DT, '단위 합계') COMM_DT
                        FROM LRMS.V_EBSM_PKG_RSC_COMM_CNT
                      WHERE PKG_SNO != 0  AND RSC_SNO = 0
                                AND COMM_DT BETWEEN TO_CHAR(TO_DATE('20130501', 'YYYY-MM-DD'), 'YYYYMMDD') AND TO_CHAR(TO_DATE('20130530', 'YYYY-MM-DD'), 'YYYYMMDD')
                      GROUP BY ROLLUP(COMM_DT)
                    ) MNOT  ON DAY.DAY = MNOT.COMM_DT
        ) X

 WHERE 1=1
 and X.TOT_CNT IS NOT NULL 

<isNotEmpty property="startDt" prepend="AND">
    <isNotEmpty property="endDt">
        (#startDt# <![CDATA[<=]]> TO_CHAR(TO_DATE(DAY, 'YYYY-MM-DD'), 'YYYYMMDD') AND #endDt# <![CDATA[>=]]> TO_CHAR(TO_DATE(DAY, 'YYYY-MM-DD'), 'YYYYMMDD'))

    </isNotEmpty>
</isNotEmpty>
</select>

错误是

org.springframework.dao.DataIntegrityViolationException:SqlMapClient操作; SQL [];

---应用参数映射时发生错误。

---检查ContentStatis.statis-InlineParameterMap。

---检查语句(查询失败)。 ---原因:java.sql.SQLDataException:'ORA-01841 :(完整)年必须在-4713之间 +9999,而不是0';嵌套异常是com.ibatis.common.jdbc.exception.NestedSQLException: ---应用参数映射时发生错误。

---检查ContentStatis.statis-InlineParameterMap。

---检查语句(查询失败)。

---原因:java.sql.SQLDataException:ORA-01841:'ORA-01841 :(完整)年必须介于-4713和+9999之间,而不是0'

我使用ORACLE

1 个答案:

答案 0 :(得分:2)

您没有使用指定格式的日期。例如:

TO_DATE('20130501', 'YYYY-MM-DD')

应该是

TO_DATE('2013-05-01', 'YYYY-MM-DD')

TO_DATE('20130501', 'YYYYMMDD')