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 ...
随机推荐
- 查看buffer cache命中率
SQL> select name,value from v$sysstat where name in('db block gets','consistent gets','physical r ...
- liux之sed用法
sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换.删除.新增.选取等特定工作,下面先了解一下sed的用法sed命令行格式为: sed ...
- 简单易用的AOP/IOC框架
Source: http://www.codeproject.com/KB/Articles/684613/Working/AopIoc.zip Introduction Supper framewo ...
- codeforces 691F Couple Cover 暴力
分析:开一个300w的数组,统计,然后nlogn统计每个值在在序对第一个出现有多少种情况 时间复杂度:O(nlogn) n在3e6数量级 #include<cstdio> #include ...
- CABasicAnimation 使用
1. 基本使用 UIView * view = [[UIView alloc]initWithFrame:CGRectMake(50, 50, 50,50)]; view.backgroundColo ...
- [Irving]字符串相似度-字符编辑距离算法(c#实现)
编辑距离(Edit Distance),又称Levenshtein距离,是指两个字串之间,由一个转成另一个所需的最少编辑操作次数.许可的编辑操作包括将一个字符替换成另一个字符,插入一个字符,删除一个字 ...
- CSS 3的display:盒类型详解
在CSS中,使用display属性来定义盒的类型.总体来说,盒类型分为两类:inline和block.如div默认是block,span默认是Inline.可以通过display修改默认的表现方式. ...
- BootStrap入门教程 (三) :可重用组件(按钮,导航,标签,徽章,排版,缩略图,提醒,进度条,杂项)
上讲回顾:Bootstrap的基础CSS(Base CSS)提供了优雅,一致的多种基础Html页面要素,包括排版,表格,表单,按钮等,能够满足前端工程师的基本要素需求. Bootstrap作为完整的前 ...
- 《Genesis-3D开源游戏引擎完整实例教程-2D射击游戏篇04:碰撞检测》
4.碰撞检测 碰撞概述: 游戏世界里,游戏对象不能做出如同在真实世界里的物理运动效果.对于大部分游戏来说,都要为其添加物理系统,让其可以模拟真实世界发生的物理运动.但是在这个打飞机游戏Demo中,是用 ...
- 读取jar内的配置文件
读取jar包内的配置文件,可以使用ResourceBundle,具体具体例子如下 import java.io.BufferedInputStream; import java.io.IOExcept ...