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 ...
随机推荐
- LeetCode: Interval
(1)Merge Intervals https://leetcode.com/problems/merge-intervals/ Given a collection of intervals, m ...
- 【转】iOS类似Android上toast效果
原文网址:http://m.blog.csdn.net/article/details?id=50478737 做过Android开发的人都知道toast,它会在界面上显示一排黑色背景的文字,用于提示 ...
- TCP/IP详解学习笔记(11)-TCP交互数据流,成块数据流
目前建立在TCP协议上的网络协议特别多,有telnet,ssh,有ftp,有http等等.这些协议又可以根据数据吞吐量来大致分成两大类:(1)交互数据类型,例如telnet,ssh,这种类型的协议在大 ...
- HNU 13108-Just Another Knapsack Problem (ac自动机上的dp)
题意: 给你一个母串,多个模式串及其价值,求用模式串拼接成母串(不重叠不遗漏),能获得的最大价值. 分析: ac自动机中,在字典树上查找时,用dp,dp[i]拼成母串以i为结尾的子串,获得的最大价值, ...
- bzoj1036: [ZJOI2008]树的统计Count 树链剖分+线段树
入门题 + 熟悉代码 /************************************************************** Problem: 1036 User: 96655 ...
- IOS 类别与扩展的区别 (category & extensions)
类别 .h @interface NSString(XXXXXX) -(NSInteger)getLen; @end .m @implementation NSString(XXXXXX) -(NSI ...
- linux常用命令之--目录与文件的操作命令
1.linux的目录与文件的增.删.改.复制 pwd:用于显示当前所在的目录 ls:用于显示指定目录下的内容 其命令格式如下: ls [-option] [file] 常用参数: -l:显示文件和目录 ...
- iOS数据存储之属性列表理解
iOS数据存储之属性列表理解 数据存储简介 数据存储,即数据持久化,是指以何种方式保存应用程序的数据. 我的理解是,开发了一款应用之后,应用在内存中运行时会产生很多数据,这些数据在程序运行时和程序一起 ...
- ASP.net MVC基础
简单了解了下MVC的基本开发步骤后,又对MVC的语法和模版详细看看了,小小总结下 对mvc开发,首先是要对布局有个基本的认识.Razor引擎使页面元素更加清晰 简单认识下 可以加载css和js等文件, ...
- android:照片涂画功能实现过程及原理
这个功能可以帮你实现,在图片上进行随意的涂抹,可以用于SNS产品. 绘图本身很简单,但是要实现在图片上指定的部分精确(位置,缩放)的绘图,就有点麻烦了. 下面讲讲实现过程及原理: UI构图 这个UI, ...