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))GOINSERT 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 TableNameGO |
Test the view to make sure we are getting the data we expect.
|
1
2
|
SELECT * FROM vw_TableViewGO |
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 INTGOUPDATE TableName SET Column2 = 3GO |
And try out the view again.
|
1
2
|
SELECT * FROM vw_TableViewGO |
| 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_TableViewGO |
And test the view again.
|
1
2
|
SELECT * FROM vw_TableViewGO |
| 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 Column2GO |
And we try querying the view again. (I’m hoping no one expects it to work correctly.)
|
1
2
|
SELECT * FROM vw_TableViewGO |
This time we get an error.
|
1
2
|
Msg 4502, Level 16, State 1, Line 1View 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_TableViewGOSELECT * FROM vw_TableViewGO |
| Column1 |
| abcdefg |
| hij |
| klmnop |
| qrstuvwxy |
| zabcde |
| 123456 |
And last but not least some cleanup code.
|
1
2
3
|
DROP VIEW vw_TableViewDROP TABLE TableNameGO |
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 ...
随机推荐
- .NET/ASP.NET Routing路由(深入解析路由系统架构原理)http://wangqingpei557.blog.51cto.com/1009349/1312422
阅读目录: 1.开篇介绍 2.ASP.NET Routing 路由对象模型的位置 3.ASP.NET Routing 路由对象模型的入口 4.ASP.NET Routing 路由对象模型的内部结构 4 ...
- Java与WCF交互(一):Java客户端调用WCF服务
最近开始了解WCF,写了个最简单的Helloworld,想通过java客户端实现通信.没想到以我的基础,居然花了整整两天(当然是工作以外的时间,呵呵),整个过程大费周折,特写下此文,以供有需要的朋友参 ...
- android moveTaskToback 应用退到后台,类似最小化
方法:public boolean moveTaskToBack(boolean nonRoot) activity里有这个方法,参数说明如下: nonRoot=false→ 仅当activity为t ...
- [FIX BUG]获取theme中自定义textColor时报的错误
我在Fragment中inflate它都可以,可是一旦使用ListView来inflate就会报错,说找不到我自定义的attr!研究了半天发现是我的inflate的context有问题: view = ...
- POJ 2549 Sumsets
Sumsets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10593 Accepted: 2890 Descript ...
- C#+AE 用MapControl加载栅格格式文件
需要引入DataSourceRaster命名空间. 具体步骤如下: ①:利用工作控件工厂抽象类定义接口变量,利用工作空间工厂的子类RatserWorkspaceFactory对应的组件类RasterW ...
- PIG的配置
Pig是一个客户端应用程序,就算你要在Hadoop集群上运行Pig,也不需要在集群上装额外的东西.Pig的配置非常简单: 1.下载pig,网址http://pig.apache.org/ 2.在机器上 ...
- 《Python 学习手册4th》 第十章 Python语句简介
''' 时间: 9月5日 - 9月30日 要求: 1. 书本内容总结归纳,整理在博客园笔记上传 2. 完成所有课后习题 注:“#” 后加的是备注内容 (每天看42页内容,可以保证月底看完此书) “重点 ...
- 分析特定类的python脚本
今天接触了下pyUSB,事先没看对象内部成员资料,直接用python的dir函数看了看pyUSB的内部构成.突然间想到自己可不可以写个简单的脚本,利用dir或其他函数遍历某个对象内部的所有成员,并打印 ...
- Some_sort_algorithms
复习下数据结构常用的排序算法,更多内容上wiki 快速排序(不稳定 O(n log n)) 快速排序(Quicksort)是对冒泡排序的一种改进.由C. A. R. Hoare在1962年提出.它的基 ...