問題描述
我的數(shù)據(jù)庫中有一個視圖,有人用一個表中的 * 定義了這個視圖.我剛剛在該表中添加了一個新列,并且我希望視圖反映新列.除了重新執(zhí)行視圖創(chuàng)建腳本之外,還有其他方法可以重建視圖嗎?我正在尋找類似于 sp_recompile 如何重新編譯存儲過程(或者更準(zhǔn)確地將其標(biāo)記為下次調(diào)用時編譯)的方法.
更新:從長遠(yuǎn)來看,我嘗試在視圖上調(diào)用 sp_recompile,雖然調(diào)用有效,但它沒有重建視圖.
更新 2: 我希望能夠從腳本中執(zhí)行此操作.因此,將列添加到表的腳本也可以更新視圖.所以就像我說的,類似于 sp_recompile.
我相信您正在尋找的是
sp_refreshview [ @viewname = ] 'viewname'
<塊引用>
更新指定的元數(shù)據(jù)非模式綁定視圖.執(zhí)著的視圖的元數(shù)據(jù)可以變成由于更改而過時視圖所基于的底層對象取決于.
見微軟文檔
There is a view in my DB that someone defined with a * from one table. I just added a new column to that table and I want the view to reflect the new column. Besides re-executing the view creation script, is there another way to rebuild the view? I am looking for something similar to how sp_recompile will recompile a stored procedure (or more accurately flag it to be compiled next time it is called).
Update: On a long shot I tried calling sp_recompile on the view and while the call worked, it didn't rebuild the view.
Update 2: I would like to be able to do this from a script. So the script that adds the columns to the table could also update the view. So like I said, something similar to sp_recompile.
I believe what you're looking for is
sp_refreshview [ @viewname = ] 'viewname'
Updates the metadata for the specified non-schema-bound view. Persistent metadata for a view can become outdated because of changes to the underlying objects upon which the view depends.
See Microsoft Docs
這篇關(guān)于如何在 SQL Server 2008 中重建視圖的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!