my-view-isnt-reflecting-changes-ive-made-to-the-underlying-tables
FROM http://sqlstudies.com/2013/01/20/my-view-isnt-reflecting-changes-ive-made-to-the-underlying-tables/
Problem: You’ve added columns to the base table of one of your views, but the view isn’t reflecting the change.
Over the years I’ve seen lot’s of views created similar to this one.
1
2
|
CREATE VIEW vw_TableView AS SELECT * FROM TableName |
Generally the argument is that if I put “SELECT *” rather than an explicit field list, then when my table changes so will my view. Unfortunately it doesn’t work that way.
Let’s try an example.
Create a test table and populate it with some data.
1
2
3
4
5
6
7
8
9
10
|
CREATE TABLE TableName (Column1 varchar (10)) GO INSERT INTO TableName VALUES ( 'abcdefg' ) INSERT INTO TableName VALUES ( 'hij' ) INSERT INTO TableName VALUES ( 'klmnop' ) INSERT INTO TableName VALUES ( 'qrstuvwxy' ) INSERT INTO TableName VALUES ( 'zabcde' ) INSERT INTO TableName VALUES ( '123456' ) GO |
Create a test view.
1
2
3
|
CREATE VIEW vw_TableView AS SELECT * FROM TableName GO |
Test the view to make sure we are getting the data we expect.
1
2
|
SELECT * FROM vw_TableView GO |
So far so good. The output is exactly what we expected. Now let’s add a column to the table and populate it.
1
2
3
4
|
ALTER TABLE TableName ADD Column2 INT GO UPDATE TableName SET Column2 = 3 GO |
And try out the view again.
1
2
|
SELECT * FROM vw_TableView GO |
Column1 |
abcdefg |
hij |
klmnop |
qrstuvwxy |
zabcde |
123456 |
Now wait just a minute. The output I’m getting looks exactly like it did before I added Column2. All I’m seeing is Column1. Now the first thing I do when debugging something like this is make sure the view should in fact be pulling the new column. So:
1
|
EXEC sp_helptext vw_TableView |
1
2
3
4
5
|
Text --------------------------------------------------------------- CREATE VIEW vw_TableView AS SELECT * FROM TableName |
Ok, so the code still looks correct. So why aren’t we pulling all of the columns even though we are using a *? From what I understand the metadata for the view is not automatically updated when the tables are modified.
The fix is to either drop and re-create or alter the view or to use the sp_refreshview stored procedure. Sp_refreshview has the combined benefit of being the simplest method and not messing up any explicit permissions on the view caused by dropping it.
1
2
|
EXEC sp_RefreshView vw_TableView GO |
And test the view again.
1
2
|
SELECT * FROM vw_TableView GO |
Column1 | Column2 |
abcdefg | 3 |
hij | 3 |
klmnop | 3 |
qrstuvwxy | 3 |
zabcde | 3 |
123456 | 3 |
And now we have the correct number of columns for our view.
Next let’s try going the other way. We remove a column from the table.
1
2
|
ALTER TABLE TableName DROP Column2 GO |
And we try querying the view again. (I’m hoping no one expects it to work correctly.)
1
2
|
SELECT * FROM vw_TableView GO |
This time we get an error.
1
2
|
Msg 4502, Level 16, State 1, Line 1 View or function 'vw_TableView' has more column names specified than columns defined. |
If we again run sp_refreshview then the view will once again show the expected data.
1
2
3
4
|
EXEC sp_RefreshView vw_TableView GO SELECT * FROM vw_TableView GO |
Column1 |
abcdefg |
hij |
klmnop |
qrstuvwxy |
zabcde |
123456 |
And last but not least some cleanup code.
1
2
3
|
DROP VIEW vw_TableView DROP TABLE TableName GO |
my-view-isnt-reflecting-changes-ive-made-to-the-underlying-tables的更多相关文章
- SQL VIEW 使用语法
之前一直都不知道VIEW有什么作用,写程序的时候也很少遇到过,复习SQL语句的时候碰到了,就记录下来吧. 什么是视图? 在 SQL 中,视图是基于 SQL 语句的结果集的可视化的表. 视图包含行和列, ...
- Dynamic view
Views are a useful feature of SQL databases, letting us create virtual tables based on SQL select st ...
- What is the difference between database table and database view?
The database table has a physical existence in the database. A view is a virtual table, that is one ...
- [Hive - LanguageManual] Create/Drop/Alter -View、 Index 、 Function
Create/Drop/Alter View Create View Drop View Alter View Properties Alter View As Select Version info ...
- MySQL/MariaDB数据库的视图(VIEW)
MySQL/MariaDB数据库的视图(VIEW) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.视图概述 1>.什么是视图 视图就是一个虚拟的表,保存有实表的查询结果 ...
- [转]Code! MVC 5 App with Facebook, Twitter, LinkedIn and Google OAuth2 Sign-on (C#)
本文转自:https://www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-app-with-facebook-and-google-o ...
- SubSonic指南中文版
翻译:王鹏程张原 王伟策划:毛凌志2009年1月北京工业大学软件学院PS:有问题反馈至http://lexus.cnblogs.comGetting Started with SubSonicBy S ...
- Thinking in Java——笔记(18)
I/O The original byte-oriented library was supplemented with char-oriented, Unicode-based I/O classe ...
- MySql学习(MariaDb)
资料 http://www.cnblogs.com/lyhabc/p/3691555.html http://www.cnblogs.com/lyhabc/p/3691555.html MariaDb ...
- 直接放个DB2 SQL STATEMENT大全好了!
SQL statements This topic contains tables that list the SQL statements classified by type. SQL sch ...
随机推荐
- 【转】iOS开发UITableViewCell的选中时的颜色设置
原文网址:http://mobile.51cto.com/hot-404900.htm 1.系统默认的颜色设置 //无色 cell.selectionStyle = UITableViewCellSe ...
- Jabber/XMPP协议与架构
一.概述 由Jeremie Miller于1998年开始这个项目.Jabber是一个开放源码形式组织产生的网络实时通信协议,第一个公开版本于2000年5月发行.Jabber已经由IETF XMPP协议 ...
- 原生Ajax书写
1.创建XMLHttpRequest对象 function createXMLHTTPRequest() { //1.创建XMLHttpRequest对象 //这是XMLHttpReuquest对象无 ...
- 嵌入式 hi3518c平台网卡模式MII与RMII模式在Uboot和kernel中切换小结
由于公司项目的需要,我们需要在原有的MII的基础上,修改为RMII模式,针对hi3518c平台,我的网卡是LAN8701需要修改的地方有如下几个: 首先我的uboot中env是: bootargs=m ...
- java web 学习三(Tomcat 服务器学习和使用2)
一.打包JavaWeb应用 在Java中,使用"jar"命令来对将JavaWeb应用打包成一个War包,jar命令的用法如下:
- Metaspace 之一--java8 去掉 perm 用 Metaspace 来替代
正如大家所知,JDK 8 Early Access版已经提供下载.这使开发者可以体验Java8的新特性.其中之一,是Oracle从JDK7发布以来就一直宣称的要完全移除永久代空间.例如,字符串内部池, ...
- C# delegate 学习 (练这么久终于悟出来点东东了,继续加油! ^_^)
前言 从事开发工作两年有余了,但还是对Delegate,Event神马的看见就头疼,文章看过无数,自己也练习过好多遍,但到用的时候或者人家换了一种形式之后就又不懂了,哎~智商捉急啊!! 但是,这两天的 ...
- Robotium简要学习
Robotium是一款国外的Android自动化测试框架,主要针对Android平台的应用进行黑盒自动化测试,它提供了模拟各种手势操作(点击.长按.滑动等).查找和断言机制的API,能够对各种控件进行 ...
- qt文本编辑器
示例代码: mainwindow.cpp #include "mainwindow.h" #include "ui_mainwindow.h" #include ...
- 【随便走走】Vietnam
从来没有一个地方让我如此留念过. 初到越南印象就是乱,满街轰轰轰的摩托车,狭窄的街道,各种小酒店小商店.从机场出来的路上还看到了不少中国品牌如豪爵摩托等等. 落地办理了落地签,从大陆是不能办的.越 ...