How to update view when new column added in table in sql server

No.of Views1029
Bookmarked0 times
Downloads 
Votes0
By  RRaveen   On  13 Oct 2010 02:10:27
Tag : Sql Servers , View and Functions
In this snippet, help to learn,How to update view when new column added in table in sql server
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

When we are work with sql server view and tables, there is strange issue you will face. You have created table and a view for that table. Now you are going to alter your table with one more column, in that case, sql server does not update view automatically based  according your new column.

Case

Create a new table using following script

GO
CREATE TABLE [dbo].[TestHeader](
	[TestID] [int] NULL,
	[Descriotion] [nvarchar](50) NULL
) ON [PRIMARY]

Now create a simple view for this table

create view vTestHeaderAll
as
SELECT [TestID]
      ,[Descriotion]
  FROM [TestHeader]

Then I’m going to use this view to select records

select * from vTestHeaderAll

Output 

Image Loading

Now I want to add another column into the table. Let’s say I want to add SortID column into the table

alter table TestHeader add SortID int NULL

Output

When open table, i could see the new column inside the table. 

Image Loading

Now lets run select qurey to view again.

select * from vtestheaderAll

But output is same like earlier. So what we can do to update new column in the view. There is three ways

 

1. Remove and recreate view
2. Alter the view

alter view vTestHeaderAll  
as  
SELECT *
FROM [TestHeader]

3.refresh the view using inbuilt stored procedure.

EXEC sp_refreshview vTestHeaderAll

Once you are done one of above action, let's run select query again for view.

select * from vtestheaderAll

Output 

Image Loading

Now see you able to see the new column inside the view. I have used here third way to do add new column in view. I hope this is help to all.

 
Sign Up to vote for this article
 
About Author
 
RRaveen
Occupation-Software Engineer
Company-TGS
Member Type-Gold
Location-Singapore
Joined date-03 Jun 2009
Home Page-codegain.com
Blog Page-www.codegain.com
- B.Sc. degree in Computer Science. - 4+ years experience in Visual C#.net and VB.net - Obsessed in OOP style design and programming. - Designing and developing Network security tools. - Designing and developing a client/server application for sharing files among users in a way other than FTP protocol. - Designing and implementing GSM gateway applications and bulk messaging. - Windows Mobile and Symbian Programming - Having knowledge with ERP solutions
 
 
Other popularSectionarticles
Comments
By:lovetideDate Of Posted:4/18/2011 2:18:33 AM
sp_refreshview
Nice article! I would prefer the 3rd one: sp_refreshview
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top