如何创建为数据库中的每个视图调用 sp_refreshview 的存储过程?

发布于 2024-08-26 01:47:34 字数 324 浏览 2 评论 0原文

今天我运行这个

select 'exec sp_refreshview N''['+table_schema+'].['+table_name+']'''
from information_schema.tables
where table_type = 'view'

这会生成很多:exec sp_refreshview N'[SCHEMA].[TABLE]'。然后,我将结果复制到查询编辑器窗口并运行所有这些执行程序。

我如何一次完成这一切? 我想要一个名为 dev.RefreshAllViews 之类的存储过程,我可以执行它来执行此操作...

Today I run this

select 'exec sp_refreshview N''['+table_schema+'].['+table_name+']'''
from information_schema.tables
where table_type = 'view'

This generates a lot of: exec sp_refreshview N'[SCHEMA].[TABLE]'. I then copy the result to the query editor window and run all those execs.

How do I do this all at once?
I would like to have a stored procedure called something like dev.RefreshAllViews which I can execute to do this...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦中楼上月下 2024-09-02 01:47:34
DECLARE @RefreshScript varchar(max)
set @RefreshScript = ''


select @RefreshScript= @RefreshScript + 'exec sp_refreshview N''['+table_schema+'].['+table_name+']''
'
from information_schema.tables
where table_type = 'view'



exec (@RefreshScript)

如果您的视图名称中存在 [] 字符的危险,您可能需要查看 QUOTENAME 函数。

或者也可以使用光标

DECLARE @viewName AS VARCHAR(255)

    DECLARE listOfViews CURSOR
        FOR SELECT  '[' + SCHEMA_NAME(uid) + '].[' + name + ']'
            FROM    sysobjects
            WHERE   xtype = 'V'


    OPEN listOfViews

    FETCH NEXT FROM listOfViews INTO @viewName

    WHILE ( @@FETCH_STATUS <> -1 )
        BEGIN


            FETCH NEXT FROM listOfViews INTO @viewName

            BEGIN TRY
                EXEC sp_refreshview @viewName
                PRINT @viewName + ' refreshed OK'
            END TRY
            BEGIN CATCH
                PRINT @viewName + ' refresh failed'
            END CATCH
        END

    CLOSE listOfViews

    DEALLOCATE listOfViews
DECLARE @RefreshScript varchar(max)
set @RefreshScript = ''


select @RefreshScript= @RefreshScript + 'exec sp_refreshview N''['+table_schema+'].['+table_name+']''
'
from information_schema.tables
where table_type = 'view'



exec (@RefreshScript)

If there is ever any danger of your views having the [] characters in their names you might want to look at the QUOTENAME function.

Or Also with a cursor

DECLARE @viewName AS VARCHAR(255)

    DECLARE listOfViews CURSOR
        FOR SELECT  '[' + SCHEMA_NAME(uid) + '].[' + name + ']'
            FROM    sysobjects
            WHERE   xtype = 'V'


    OPEN listOfViews

    FETCH NEXT FROM listOfViews INTO @viewName

    WHILE ( @@FETCH_STATUS <> -1 )
        BEGIN


            FETCH NEXT FROM listOfViews INTO @viewName

            BEGIN TRY
                EXEC sp_refreshview @viewName
                PRINT @viewName + ' refreshed OK'
            END TRY
            BEGIN CATCH
                PRINT @viewName + ' refresh failed'
            END CATCH
        END

    CLOSE listOfViews

    DEALLOCATE listOfViews
聚集的泪 2024-09-02 01:47:34
DECLARE @Sql VARCHAR(MAX) = ''

SELECT @Sql += 'EXEC sys.sp_refreshview @viewname = N''' + QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) + '''' + CHAR(10) 
FROM INFORMATION_SCHEMA.VIEWS

PRINT @Sql
EXEC(@Sql)
DECLARE @Sql VARCHAR(MAX) = ''

SELECT @Sql += 'EXEC sys.sp_refreshview @viewname = N''' + QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) + '''' + CHAR(10) 
FROM INFORMATION_SCHEMA.VIEWS

PRINT @Sql
EXEC(@Sql)
り繁华旳梦境 2024-09-02 01:47:34

检查系统过程SP_ExecuteSQL,其中接受一个字符串并执行它。

您可以编写一个存储过程,在上面的查询上打开游标,生成正确的字符串,然后执行它们。

Check the system procedure SP_ExecuteSQL, which accepts a string and executes it.

You could write a stored procedure that opens a cursor on the query above, generates the proper strings, and executes them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文