如何创建仅为已登录用户存储数据的视图: 这不起作用:SYS关系上不允许查询重写“
CREATE MATERIALIZED VIEW UserPerspective
ENABLE QUERY REWRITE AS
SELECT * FROM EMP e, DEPT d, Salgrade s
WHERE e.DEPTNO = d.DEPTNO
AND e.ENAME like (SELECT User FROM Dual)
AND e.Sal BETWEEN s.Losal AND s.Hisal
;
看起来我无法在视图中使用(SELECT User FROM Dual)...如何正确执行:
AND e.ENAME like (SELECT User FROM Dual)
答案 0 :(得分:2)
您可以在视图中引用USER
,如下所示(请注意,删除Salgrade
因为我没有该表。):
CREATE VIEW V AS
SELECT e.EMPNO, e.ENAME, e.JOB, e.MGR, e.HIREDATE
, e.SAL, e.COMM, e.DEPTNO, d.DNAME, d.LOC
FROM EMP e
INNER JOIN DEPT d ON e.DEPTNO = d.DEPTNO
WHERE e.ENAME = USER;
在物化视图中喜欢这样:
CREATE MATERIALIZED VIEW MV AS
SELECT e.EMPNO, e.ENAME, e.JOB, e.MGR, e.HIREDATE
, e.SAL, e.COMM, e.DEPTNO, d.DNAME, d.LOC
FROM EMP e
INNER JOIN DEPT d ON e.DEPTNO = d.DEPTNO
WHERE e.ENAME = USER;
但是,在启用了查询重写的物化视图中,您无法引用USER
:
CREATE MATERIALIZED VIEW MVEQR
ENABLE QUERY REWRITE AS
SELECT e.EMPNO, e.ENAME, e.JOB, e.MGR, e.HIREDATE
, e.SAL, e.COMM, e.DEPTNO, d.DNAME, d.LOC
FROM EMP e
INNER JOIN DEPT d ON e.DEPTNO = d.DEPTNO
WHERE e.ENAME = USER;
<强> ORA-30353: expression not supported for query rewrite 强>
原因: select子句引用了UID,USER,ROWNUM,SYSDATE,CURRENT_TIMESTAMP,MAXVALUE,序列号,绑定变量,相关变量,设置结果,触发器返回变量,并行表队列列,集合迭代器等。
操作:删除有问题的表达式或禁用实体化视图上的REWRITE选项。
以下是否能满足您的需求?
CREATE MATERIALIZED VIEW MV_II
ENABLE QUERY REWRITE AS
SELECT e.EMPNO, e.ENAME, e.JOB, e.MGR, e.HIREDATE
, e.SAL, e.COMM, e.DEPTNO, d.DNAME, d.LOC
FROM EMP e
INNER JOIN DEPT d ON e.DEPTNO = d.DEPTNO;
CREATE VIEW V_II AS
SELECT EMPNO, ENAME, JOB, MGR, HIREDATE
, SAL, COMM, DEPTNO, DNAME, LOC
FROM MV_II
WHERE ENAME = USER;