ORA-01722:创建物化视图时的编号无效

时间:2012-06-08 10:16:59

标签: oracle oracle10g materialized-views

我正在创建一个视图,同一视图在同一系统中转换为物化视图。但是在另一个系统中做同样的事情我在创建物化视图时遇到了错误ORA-01722: invalid number。为什么呢?

create materialized view MV_EMP_VALI 
 refresh complete with rowid start with SYSDATE+1/24 AS 
 (select * from V_CHA1);

查看: -

CREATE OR REPLACE VIEW V_CHA1 AS(SELECT EMPNO,
       MONTHYEAR,
       to_number(SUM(CPFEMO)) AS EMOLUMENTS,
       to_number(SUM(CPEPF)) AS EMPPFSTATUARY,
       to_number(SUM(AEMO)) AS AEMO,
       to_number(SUM(APEPF)) AS APEPF,
       MAX(recsts) AS recsts
  FROM ((SELECT RECDATE,
                   (CASE WHEN (REPFEMOFLAG='N') THEN
                round(NVL(trim(EMO), 0))
                ELSE
                round(NVL(REVISEMO, 0)) END ) as CPFEMO,
                round(NVL(trim(EPF), 0)) AS CPEPF,
                0 as AEMO,
                0 as APEPF,
                '' as recsts,
                EMPNO
           FROM EMP_VALI
          WHERE EFLAG = 'Y' AND SFLAG = 'N' AND EMPNO IS NOT NULL and
                RECDATE >'01-Apr-2011')
        union all
        (SELECT NDT.RECDATE AS RECDATE,
                sum(round(NVL(trim(NDT.EMO), 0))) as CPFEMO,
                sum(round(NVL(trim(NDT.EPF), 0))) as CPEPF,
                0 as AEMO,
                0 AS APEPF,
                NDT.EMPNO
           FROM EMP_VALI VAL, EMP_SUPP NDT
          WHERE VAL.EMPNO = NDT.EMPNO AND VAL.EFLAG = NDT.EFLAG AND
                VAL.EFLAG = 'Y' AND VAL.SFLAG = 'Y' AND
                NDT.SLIFLAG='N' and
                 VAL.EMPNO is not null and
                NDT.RECDATE = VAL.RECDATE
           GROUP BY NDT.RECDATE, NDT.EMPNO) UNION ALL
        (SELECT DT.RECPAIDDATE AS RECDATE,
                0           as CPFEMO,
                0           as CPEPF,
                sum(round(NVL(trim(DT.EMO), 0))) as AEMO,
                sum(round(NVL(trim(DT.EPF), 0))) AS APEPF,
        max('') as recsts,
                DT.EMPNO
           FROM EMP_VALI VAL, EMP_SUPP DT
          WHERE VAL.EMPNO = DT.EMPNO AND VAL.EFLAG = DT.EFLAG AND
                VAL.EFLAG = 'Y' AND VAL.SFLAG = 'Y'  AND
                VAL.EMPNO IS NOT NULL and dt.RECDATE=val.RECDATE AND DT.SFLAG IS NOT NULL AND DT.SFLAG not in ('N','F')
           GROUP BY DT.RECPAIDDATE, DT.EMPNO)UNION ALL
        (SELECT DT.RECPAIDDATE AS RECDATE,
      SUM((CASE
             WHEN (DT.ECR4FLAG = 'C') then
              round(NVL(trim(DT.EMO), 0))
             else
             0
           end)) as CPFEMO,
       sum((CASE
             WHEN DT.ECR4FLAG = 'C' then
              round(NVL(trim(DT.EPF), 0))
             else
              0
           end)) as CPEPF,
          sum((CASE
             WHEN DT.ECR4FLAG = 'A' then
              round(NVL(trim(DT.EMO), 0))
             else
              0
           end)) as AEMO,
       sum((CASE
             WHEN DT.ECR4FLAG = 'A' then
              round(NVL(trim(DT.EPF), 0))
             else
              0
           end)) as APEPF,
       max(EMPRECOVERYSTS) as recsts,
       DT.EMPNO
  FROM EMP_VALI VAL, EMP_SUPP DT
WHERE VAL.EMPNO = DT.EMPNO AND VAL.EFLAG = DT.EFLAG AND
       VAL.EFLAG = 'Y' AND VAL.SFLAG = 'Y' AND
       VAL.EMPRECSTS = 'DEP' AND VAL.EMPNO IS NOT NULL and
       dt.RECDATE = val.RECDATE AND DT.SFLAG IS NOT NULL AND
       DT.SFLAG in ('F')
GROUP BY DT.RECPAIDDATE, DT.EMPNO))
GROUP BY RECDATE, EMPNO)
/

1 个答案:

答案 0 :(得分:5)

很难从声明中说出来,但如果我不得不猜测,我把钱放在表达上:

RECDATE >'01-Apr-2011'

假设列RECDATE实际上是DATE类型。因此Oracle也尝试将字符值'01-Apr-2011'转换为DATE。由于您没有为此指定格式掩码,因此使用默认的NLS设置。如果他们定义了一个月的数字,那么上述值将无法转换。

您应该从不依赖隐式数据类型转换。 特别是没有日期。改为使用ANSI文字:

RECDATE > DATE '2011-04-01'

或使用带格式掩码的to_date()函数:

RECDATE > to_date('01-Apr-2011', 'dd-mon-yyyy')

请注意,对于NLS_LANG的某些设置,这仍然可能失败。在法语中,您需要指定'Avr'而不是'Apr'。因此,除非您绝对确定,否则您可以一直控制所有NLS_XXX设置,我强烈建议您使用月份数字。如果使用to_date()比使用ANSI文字更舒服,可以使用:

RECDATE > to_date('01-04-2011', 'dd-mm-yyyy')

修改

如果不是日期列,则需要检查任何其他列以进行隐式数据转换。

这些表达:

sum(round(NVL(trim(DT.EMO), 0)))
round(NVL(trim(EMOLUMENTS), 0))
round(NVL(trim(DT.EMO), 0))
round(NVL(trim(DT.EPF), 0))
看起来很可疑。如果其中的列是实数,则trim()无效且无用。如果这些不是数字,则可能会导致该错误,具体取决于列的内容。

这个表达式to_number(SUM(CPFEMO))也没用,因为sum()已经返回一个数字,没有理由在number()上调用to_number。虽然我怀疑它可能会引起你的错误,你仍然应该避免它,因为它没有任何意义。