create view with select *

懵懂的女人 提交于 2021-01-28 06:41:03

问题


I've created a view with CREATE VIEW dbo.myView AS SELECT * FROM dbo.myTable. myView does not select new columns when I add columns to dbo.myTable. Is there a way I can make my view select all columns from a table even after I add columns without having to update the view?


回答1:


No. You have to update the view either by recreating it or using sp_refreshview.

This is documented in CREATE VIEW:

If a view is not created with the SCHEMABINDING clause, sp_refreshview should be run when changes are made to the objects underlying the view that affect the definition of the view. Otherwise, the view might produce unexpected results when it is queried.

And SCHEMABINDING doesn't help you here either - because that prevents you even making breaking changes to the base tables.


In general, SELECT * ... is a lazy shorthand and the only place you should be using it is inside IF EXISTS() tests. Everywhere else, you're basically setting yourself up for poor performance (especially if someone else later comes along and adds a new column to your table containing the complete works of shakespeare for every row).



来源:https://stackoverflow.com/questions/22716598/create-view-with-select

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