my-view-isnt-reflecting-changes-ive-made-to-the-underlying-tables

本文探讨了当基础表更改后,使用SELECT *创建的SQL视图未能自动反映这些更改的问题。通过实例演示了如何使用sp_RefreshView存储过程来解决此问题。

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
Column1Column2
abcdefg3
hij3
klmnop3
qrstuvwxy3
zabcde3
1234563

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

转载于:https://www.cnblogs.com/wanxun1987/p/3513441.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值