SQL Server View showing outdated / wrong data after changing underlying dependencies

烂漫一生 提交于 2021-01-27 03:56:10

问题


We have a View (call it X) that is the base view called by 2 other views(call them Y and Z).

Today we made a change to view X, after that view Y and Z started bringing back data that was incorrect. When we were in Management Studio and ran SELECT * FROM Y(which is exactly how the view is called in code) it would get back data that was incorrect. However, when we ran the actual SQL that the view contained it was fine. We tried a number of things until a colleague suggested adding a space to view X and Z and then running Alter, which worked. Everything returned to normal and ran fine.

My question is: Does MSSQL cache its views? and if so how do you force them not to OR force them to re-compile?

Also, any additional reading about this would be helpful.


回答1:


See the sp_refreshview command.

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.




回答2:


SQL Server does not cache view data (at least, not in the way you are referring to it).

If a view definition contains 'SELECT *', then the actual column list is defined when the view is created, i.e. the 'SELECT *' is replaced by the actual column list than exists at the time you create the view. That means if you add columns to underlying tables referenced by that view, that won't appear in the view.



来源:https://stackoverflow.com/questions/373093/sql-server-view-showing-outdated-wrong-data-after-changing-underlying-dependen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!