-1

使用以下方法生成视图:

CREATE OR REPLACE VIEW USER_AUD_VIEW (AUDIT_ID, USER_ID, TABLE_NAME_TXT, COLUMN_NAME_TXT, OLD_VALUE_TXT, NEW_VALUE_TXT, AUDIT_LAST_UPDATED_DT, AUDIT_UPDATED_USER_ID, EVALUATOR_ID, PRODUCT_ID)
AS
SELECT acm.audit_id,
CAST(GET_TOKEN(GET_TOKEN(acm.PRIMARY_KEY_VALUES,1,','),2,':') AS VARCHAR2(64)) AS user_id,
acm.table_name_txt,
CASE WHEN (acm.table_name_txt = 'XREF_USER_PRODUCT')
THEN (mstr_product.product_description_txt || '/' || acm.column_name_txt)
ELSE acm.column_name_txt
END AS column_name_txt,
CASE WHEN UPPER(acm.column_name_txt) = 'PASSWORD_TXT'
THEN '******'
ELSE acm.old_value_txt
END AS old_value_txt,
CASE WHEN UPPER(acm.column_name_txt) = 'PASSWORD_TXT'
THEN '******'
ELSE acm.new_value_txt
END AS new_value_txt,
acm.system_dt,
CASE WHEN UPPER(acm.audit_updated_user_id) = 'UNKNOWN'
 THEN acm.audit_updated_user_id
 ELSE (users.user_id
|| ' ('
|| DECODE(users.last_name_txt, NULL,' ' , users.last_name_txt)
|| ', '
|| DECODE(users.first_name_txt, NULL,' ' , users.first_name_txt)
|| ')' )
END AS audit_updated_user_id,
acm.evaluator_id,
CASE WHEN UPPER(acm.table_name_txt) = 'XREF_USER_PRODUCT'
 THEN TO_NUMBER(trim(GET_TOKEN(GET_TOKEN(acm.PRIMARY_KEY_VALUES,2,','),2,':')))
 ELSE 0
END AS product_id
FROM audit_config_maintenance acm,
users,
mstr_product
WHERE acm.table_name_txt in ('USERS','XREF_USER_PRODUCT','XREF_USER_FUNCTION')
AND UPPER(acm.audit_updated_user_id) = UPPER(users.user_id)
AND CASE WHEN UPPER(acm.table_name_txt) = 'XREF_USER_PRODUCT'
     THEN     TO_NUMBER(trim(GET_TOKEN(GET_TOKEN(acm.PRIMARY_KEY_VALUES,2,','),2,':')))
     ELSE -1
END = mstr_product.product_id
AND acm.primary_key_values is not null
/

当我执行 desc USER_AUD_VIEW 时,我得到

Name      Null?             Type
AUDIT_ID    NOT NULL        NUMBER
USER_ID                 VARCHAR2(64)
TABLE_NAME_TXT          VARCHAR2(30)
COLUMN_NAME_TXT         VARCHAR2(81)
OLD_VALUE_TXT           VARCHAR2(64)
NEW_VALUE_TXT           VARCHAR2(64)
AUDIT_LAST_UPDATED_DT   DATE
AUDIT_UPDATED_USER_ID   VARCHAR2(71)
EVALUATOR_ID            NUMBER
PRODUCT_ID              NUMBER

然而,当我从 USER_AUD_VIEW 中选择 * 时,我得到

ORA-01722: 无效号码

我在创建视图时做错了什么?

4

1 回答 1

1
SQL> select TO_NUMBER('1AB234') from dual;

ORA-01722: invalid number
01722. 00000 -  "invalid number"
*Cause:    
*Action:

SQL> select TO_NUMBER('1234') from dual;
TO_NUMBER('1234')
-----------------
         1234 

当您在视图上发出 a 时,视图将select在s 代码中执行。它更像是而不是再次编写整个大文件,您只需在视图上运行 - 这意味着,在视图上很好,但是当您尝试转换为字母数字的字段值时,创建视图中的数据会导致问题. 要求只有数字(字符形式)、任何字母(AZ、az)或其中的任何特殊字符都会导致.viewselectselectselectdescselectTO_NUMBERTO_NUMBER(val)valvalORA-01722

所以这是一个无赖,但等等!我们可以做点什么,只从字段中获取数字,而忽略其中的任何 alpha 或特殊字符。

SQL> select to_number(regexp_replace('1!@#2Aa33-*^4','[^[:digit:]]')) from dual;

VAL
------
12334 

警告- 上面的解决方案可能对十进制数不太友好。但要给出一个整体的想法。

因此,regexp拯救你的人将如下所示(假设GET_TOKEN运作良好)-

TO_NUMBER(regexp_replace(GET_TOKEN(GET_TOKEN(acm.PRIMARY_KEY_VALUES,2,','),2,':'),'[^[:digit:]]'))
于 2012-08-27T20:52:00.200 回答