如何创建为数据库中的每个视图调用 sp_refreshview 的存储过程?
今天我运行这个
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您的视图名称中存在 [] 字符的危险,您可能需要查看 QUOTENAME 函数。
或者也可以使用光标
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
检查系统过程
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.