为什么sp_refreshview会在非模式绑定的视图上引发异常?

时间:2014-12-29 17:30:10

标签: sql-server exception view refresh

我使用具有sysadmin角色的登录连接到SQL Server 2012实例,但是当我运行以下脚本时,我会为每个视图获得此异常:

  

Msg 15165,Level 16,State 1,Procedure sp_refreshsqlmodule_internal,   第55行   找不到对象< view name>或者你没有得到许可。

请注意,我正在筛选出任何架构绑定视图:

DECLARE @ViewName NVARCHAR(max);

DECLARE myCursor CURSOR LOCAL FAST_FORWARD TYPE_WARNING FOR
    SELECT  s.name + '.' + v.name
      FROM  sys.views v
            JOIN sys.schemas s ON v.schema_id = s.schema_id
     WHERE  OBJECTPROPERTY(v.object_id, 'IsSchemaBound') = 0;

OPEN myCursor;
RAISERROR('Refreshing views:', 0, 1) WITH NOWAIT;

WHILE (1=1) BEGIN
    FETCH NEXT FROM myCursor INTO @ViewName;
    IF @@FETCH_STATUS < 0 BREAK;

    RAISERROR(@ViewName, 0, 1) WITH NOWAIT;
    EXEC msdb.dbo.sp_refreshview @ViewName;
END;

RAISERROR('Done', 0, 1) WITH NOWAIT;
CLOSE       myCursor;
DEALLOCATE  myCursor;

1 个答案:

答案 0 :(得分:2)

在我看来,应该从包含视图的数据库运行sp_refreshview。因此,将执行行更改为:

 EXEC dbo.sp_refreshview @ViewName;