相当于oracle中的sp_refreshview

时间:2010-12-17 13:59:33

标签: oracle plsql

Oracle是否在SQL Server上有类似sp_refreshview的东西?

提前致谢, 戈兰

2 个答案:

答案 0 :(得分:4)

在Oracle中有一个等价物,但没有必要使用它。这是一个例子:

表格:

SQL> create table t (id,name)
  2  as
  3  select 1, 'StackOverflow' from dual
  4  /

Table created.

观点:

SQL> create view v
  2  as
  3  select id
  4       , name
  5    from t
  6  /

View created.

哪个有效:

SQL> select *
  2    from v
  3  /

        ID NAME
---------- -------------
         1 StackOverflow

1 row selected.

SQL> select status
  2    from user_objects
  3   where object_name = 'V'
  4  /

STATUS
-------
VALID

1 row selected.

现在对基础表做一些事情:

SQL> alter table t add (description varchar2(100))
  2  /

Table altered.

由于Oracle存储依赖项(请参阅DBA / ALL / USER_DEPENDENCIES视图),视图将标记为无效:

SQL> select status
  2    from user_objects
  3   where object_name = 'V'
  4  /

STATUS
-------
INVALID

1 row selected.

但你仍然可以从中进行选择。 Oracle无论如何都会尝试执行它,即使它知道视图标记为INVALID:

SQL> select *
  2    from v
  3  /

        ID NAME
---------- -------------
         1 StackOverflow

1 row selected.

通过从视图中进行选择,它再次标记了视图VALID:

SQL> select status
  2    from user_objects
  3   where object_name = 'V'
  4  /

STATUS
-------
VALID

1 row selected.

您也可以编译视图,以确保视图提前有效:

SQL> alter table t add (description2 varchar2(100))
  2  /

Table altered.

SQL> select status
  2    from user_objects
  3   where object_name = 'V'
  4  /

STATUS
-------
INVALID

1 row selected.

SQL> alter view v compile
  2  /

View altered.

“编译”视图将其标记为有效:

SQL> select status
  2    from user_objects
  3   where object_name = 'V'
  4  /

STATUS
-------
VALID

1 row selected.

如果更改影响视图的方式使基础查询不再执行:

SQL> alter table t drop column name
  2  /

Table altered.

SQL> select status
  2    from user_objects
  3   where object_name = 'V'
  4  /

STATUS
-------
INVALID

1 row selected.

然后你会得到一个运行时错误:

SQL> select *
  2    from v
  3  /
  from v
       *
ERROR at line 2:
ORA-04063: view "OWNER.V" has errors


SQL> select status
  2    from user_objects
  3   where object_name = 'V'
  4  /

STATUS
-------
INVALID

1 row selected.

希望这有帮助。

此致 罗布。

答案 1 :(得分:2)

据我所知不是。我通常只从数据库中保存的扩展表单中查看select *之类的视图。我认为您不可能将列添加到基础表并将此列放入视图而不是更改视图。

对于ORACLE用户,这里有一个示例,您可以使用SQL-Server(自SQL2008以来),但未修改的定义也存储在sysbase中的sysbase中:

警告这是T-SQL

create table t1 (i int, c1 int);
create view v1 as select * from t1;
alter table t1 add c2 int;
select * from v1;   -- c2 is not present here
sp_refreshview v1;
select * from v1;   -- c2 is present here 

我想由于Oracle不存储定义,因此无法刷新视图。