Oracle:列名称以双引号开头时,物化视图快速刷新失败

问题描述

全部!

版本:Oracle 12.2

在WHERE子句中,Mat.view快速刷新失败 有column,以双引号和下划线开头,如下所示: “_ID”。 除了重命名基本表中的列之外,您是否知道其他解决方法

解决方法

不重命名基表中的列;重命名实例化视图中的列:

SQL> desc test
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 _ID                                                VARCHAR2(20)
 SOME_COL                                           VARCHAR2(3)

SQL> create materialized view mv_test as
  2    select "_ID" as id,--> this
  3           some_col
  4  from test;

Materialized view created.

SQL> desc mv_test
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 ID                                                 VARCHAR2(20)
 SOME_COL                                           VARCHAR2(3)

SQL>
,

我按照您的尝试进行了尝试,并且根据您对问题的理解,我创建了一个小演示。

请在下面的观察中找到

--drop table test_mv;
create table test_mv("_ID" number primary key,NAME varchar2(100));

--data
insert into test_mv values (1,'first row');
insert into test_mv values (2,'second row');
commit;

--trying to select without ""
select *
  from test_mv
where _ID = 1;
--result 
--Error report -
--SQL Error: ORA-00911: invalid character
--00911. 00000 -  "invalid character"

--trying to select with ""
select *
  from test_mv
where "_ID" = 1;
--result
_ID NAME                                                                                                
--- -----
1   first row

--mat view log
create materialized view log on test_mv;
--output: Materialized view log TEST_MV created.

--drop materialized view mv_test_mv;
--create materialized view giving "_id" in where clause as you described and `on commit`
create materialized view mv_test_mv 
refresh fast on commit
as
select *
  from test_mv
where "_ID" = 1;
--output: Materialized view MV_TEST_MV created.

--selecting from mat view
select * from mv_test_mv;

--output:
_id NAME                                                                                                
--- -----
1   first row

--update the base table record
update test_mv set name = 'modified first row' where "_ID" = 1;
commit;
--output
--1 row updated.
--Commit complete.

--selecting again from mat view to check if the changes propagated to the mat view or not
select * from mv_test_mv;

output:
_ID NAME                                                                                                
--- ------------------
1   modified first row

即使我有一个带有“ _ID”的where子句,也都可以正常工作。

这是您在说的还是我不理解的其他内容?