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的更多相关文章

  1. SQL VIEW 使用语法

    之前一直都不知道VIEW有什么作用,写程序的时候也很少遇到过,复习SQL语句的时候碰到了,就记录下来吧. 什么是视图? 在 SQL 中,视图是基于 SQL 语句的结果集的可视化的表. 视图包含行和列, ...

  2. Dynamic view

    Views are a useful feature of SQL databases, letting us create virtual tables based on SQL select st ...

  3. 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 ...

  4. [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 ...

  5. MySQL/MariaDB数据库的视图(VIEW)

     MySQL/MariaDB数据库的视图(VIEW) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.视图概述 1>.什么是视图 视图就是一个虚拟的表,保存有实表的查询结果 ...

  6. [转]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 ...

  7. SubSonic指南中文版

    翻译:王鹏程张原 王伟策划:毛凌志2009年1月北京工业大学软件学院PS:有问题反馈至http://lexus.cnblogs.comGetting Started with SubSonicBy S ...

  8. Thinking in Java——笔记(18)

    I/O The original byte-oriented library was supplemented with char-oriented, Unicode-based I/O classe ...

  9. MySql学习(MariaDb)

    资料 http://www.cnblogs.com/lyhabc/p/3691555.html http://www.cnblogs.com/lyhabc/p/3691555.html MariaDb ...

  10. 直接放个DB2 SQL STATEMENT大全好了!

    SQL statements   This topic contains tables that list the SQL statements classified by type. SQL sch ...

随机推荐

  1. 264分析两大利器:264VISA和Elecard StreamEye Tools

    学了264有将近3个月有余,好多时候都在学习老毕的书和反复看JM86的代码,最近才找到264分析两大利器:264VISA和Elecard StreamEye Tools.不由得感叹,恨不逢同时. 简单 ...

  2. dbms_stats.gather_table_stats与analyze table 的区别[转贴]

    Analyze StatementThe ANALYZE statement can be used to gather statistics for a specific table, index ...

  3. BasicDataSource配备

    BasicDataSource配置 commons DBCP 配置参数简要说明 前段时间因为项目原因,要在修改数据库连接池到DBCP上,折腾了半天,有一点收获,不敢藏私,特在这里与朋友们共享. 在配置 ...

  4. PostgreSQL 8.4.1

    PHP100资讯:PostgreSQL 是一种对象-关系型数据库管理系统(ORDBMS),也是目前功能最强大.特性最丰富和最复杂的自由软件数据库系统.它起源于伯克利(BSD)的数据库研究计划,目前是最 ...

  5. Java中实现复制文件或文件夹

     拷贝一个文件的算法比较简单,当然,可以对它进行优化,比如使用缓冲流,提高读写数据的效率等.但是在复制文件夹时,则需要利用Flie类在目标文件夹中创建相应的目录,并且使用递归方法. [java] vi ...

  6. Python 3 加密简介

    导读 Python 3 的标准库中是没多少用来解决加密的,不过却有用于处理哈希的库.在这里我们会对其进行一个简单的介绍,但重点会放在两个第三方的软件包:PyCrypto 和 cryptography ...

  7. Gridview LookupEdit gridLookUpEdit z

    LookupEdit一般用法: 绑定数据源:                                 lookUpEdit.Properties.ValueMember = 实际要用的字段;  ...

  8. 【剑指offer 面试题17】合并两个排序的链表

    思路: 比较两个链表端点值的大小,通过递归的方式排列. #include <iostream> using namespace std; struct ListNode { int val ...

  9. linux常用命令之--用户与用户组管理命令

    linux的用户与用户组管理命令 1.用户和群组 groupadd:用于添加新的组群 其命令格式如下: groupadd [-option] 群组名 常用参数: -g GID:指定创建群组的GID(G ...

  10. 警惕javascript代码中的“</script>”!

    之前在写<博客园自定义博客侧边栏公告的过滤漏洞>的时候遇到了一个javascript代码报错“语法错误”的问题,一直不得以解决,感谢Arliang发现了并为我进行了耐心的解释,现整理如下: ...