基于带有 "WITH "子句的SELECT的视图。

问题描述 投票:3回答:3

我已经用'WITH'子句选择了。

with 
alias1 as (select...),
alias2 as (select ... from alias1),
alias3 as (select col1, col2 ... from alias2)
select col1,col2 from alias3 

我尝试使用 "WITH "子句来创建视图。

create view ex_view as (
with 
alias1 as (select...),
alias2 as (select ... from alias1),
alias3 as (select col1, col2 ... from alias2)
select col1,col2 
from alias3
)

当我试图执行这个创建语句时,得到了 "不支持使用WITH子句

如何根据我的select语句正确地创建视图?

sql oracle sql-view
3个回答
10
投票

试着丢掉括号。

create view ex_view as
with 
    alias1 as (select...),
    alias2 as (select ... from alias1),
    alias3 as (select col1, col2 ... from alias2)
from alias3;

4
投票

你不应该在查询的周围加上括号,改成:

create view ex_view as (
with 
alias1 as (select...),
alias2 as (select ... from alias1),
alias3 as (select col1, col2 ... from alias2)
select col1,col2 
from alias3
)

例如:

create view ex_view as
with 
alias1 as (select dummy from dual),
alias2 as (select dummy as col1, dummy as col2 from alias1),
alias3 as (select col1, col2 from alias2)
select col1,col2 
from alias3;

View ex_view created.

同样的结构,查询周围的括号得到ORA-32034:不支持使用with子句。

如果你实际上没有在多个级别的查询中使用任何子查询--因此 "通用表表达式 "的 "通用 "部分并不真正相关--你可以使用内联视图来代替。

create view ex_view as
select alias3.col1, alias3.col2
from (
  select col1, col2
  from (
    select dummy as col1, dummy as col2
    from (
      select dummy from dual
    ) alias1
  ) alias2
) alias3;

但是... with 子句是有效的,而且通常更容易阅读和维护。


0
投票

有没有办法创建一个带有 "WITH FUNCTION "子句的视图。

CREATE OR REPLACE VIEW test$v AS 
    WITH
      FUNCTION with_function(p_id IN VARCHAR2) RETURN VARCHAR2 IS
      BEGIN
        RETURN p_id;
      END;
    SELECT with_function(dummy) AS dummy
    FROM   dual;
    /

这产生一个错误。

ORA-06553: PLS-103: Encountered the symbol "end-of-file" 
© www.soinside.com 2019 - 2024. All rights reserved.