• Home
  • About Me
  • Contact Us
  • Gallery
  • Resume

WikiDBA

by Virendra Yaduvanshi – Microsoft SQL Server Database Architect | Consultant | Blogger | Specialist | DBA | Speaker
Stay updated via RSS

  • SQL Authors

  • Articles

  • Blog Stats

    wordpress visitor counter View My Stats
  • Search

  • Social

    • View YaduvanshiVirendra’s profile on Facebook
    • View v_yaduvanshi’s profile on Twitter
    • View virendrayaduvanshi’s profile on LinkedIn
  • wikidba

    wikidba

Refreshing VIEW’s Columns

Posted: November 26, 2012 by Virendra Yaduvanshi in Database Administrator
Tags: Alter View, Metadata Update, Problems with view, Refreshing View Column, Refreshing View Columns, SCHEMABINDING, SP_RefreshView, SQL Server, Update View Column, Update View Column as per Base Table, View WITH SCHEMABINDING. View Creation with SCHEMABINDING, WITH SCHEMABINDING, Working with View
0

Hi, Sometimes its happened that we have wrongly written ‘SELECT * from Tablename’ instead of ‘SELECT Col1, Col2,… Col(n) from TableName’ in a View.
Suppose after creation of View any changes happened at the base table/ underlying table like Addition of Columns or Deletion of any column, View will not changed as per base table and as a resultant it will return wrong output.

The reason is “The column information/definition of VIEW – metadata of VIEW, gets stored in system tables at the time of creation of VIEW and that metadata doesn’t being refresh when you alter the underlying table. For that, we have to explicitly refresh the metadata of VIEW”

To prevent, this we can use either ALTER VIEW or System Stored Procedure SP_ REFRESHVIEW, as per BOL, SP_ REFRESHVIEW – Updates the metadata for the specified non-schema-bound view. Persistent metadata for a view can become outdated because of changes to the underlying objects upon which the view depends.

There may be so many situations like Column Addition, Column Deletion, worst case as Deletion of one or more columns and addition of one or more columns.

Let First Create a sample Table and Insert some Data,

— Create a Sample Table ‘EMPLOYEE’

    USE [VirendraTest]
    GO

CREATE TABLE[dbo].[Employee]( [EmpID] [int] IDENTITY(1,1) NOT NULL,
                              [Name] [nchar](10) NULL,
                              [BasicSalary] [numeric](18, 2) NULL)
                              ON 
[PRIMARY]
                              GO

— Insert Some Sample data

SET IDENTITY_INSERT [dbo].[Employee] ON

INSERT [dbo].[Employee] ([EmpID], [Name], [BasicSalary]) VALUES (1, N’Manish ‘,CAST(11000.00 AS Numeric(18, 2))) INSERT [dbo].[Employee] ([EmpID], [Name], [BasicSalary]) VALUES (2, N’Ajay ‘, CAST(22000.00 AS Numeric(18, 2)))
INSERT
[dbo].[Employee] ([EmpID], [Name], [BasicSalary]) VALUES (3, N’Kumar ‘, CAST(33000.00 AS Numeric(18, 2)))
INSERT 
[dbo].[Employee] ([EmpID], [Name], [BasicSalary]) VALUES (4, N’Manish ‘, CAST(11000.00 AS Numeric(18, 2)))
INSERT 
[dbo].[Employee] ([EmpID], [Name], [BasicSalary]) VALUES (5, N’Ajay ‘, CAST(44000.00 AS Numeric(18, 2)))
INSERT 
[dbo].[Employee] ([EmpID], [Name], [BasicSalary]) VALUES (6, N’Virendra ‘, CAST(25000.00 AS Numeric(18, 2)))
INSERT 
[dbo].[Employee] ([EmpID], [Name], [BasicSalary]) VALUES (8, N’Ram ‘, CAST(25000.00 AS Numeric(18, 2)))
SET 
IDENTITY_INSERT [dbo].[Employee] OFF
–Lets Check Records


— Create a VIEW on Employee Table

Create View Vw_Employee As
Select 
* from Employee
— Lets Check Records from view ‘Vw_Employee’
Select *  from Vw_Employee

— Now , Add a new column DEPT
Alter 
Table Employee Add DEPT varchar(20)
— Now Lets Check Records from view ‘Vw_Employee’

Select * from Vw_Employee 

Here, you can see, DEPT column not get updated with VIEW

— To resolve the problem / sync columns with base/underlying tables

    Execute SP_RefreshView ‘Vw_Employee’

     — OR —

    Alter View  Vw_Employee as
    Select 
* from Employee

Now Lets Check View, its now as per base table.


Now let the worst case, if drop one or more columns and add equal or more number columns to the table then,

Here, you can see, as per table, View is showing wrong output, and to sync both we have to either run SP_RefreshView or ALTER VIEW command.

Hence, prevention is don’t use wild card “*” while creating VIEWS. But even listing out columns is just a good practice not a solution, it may be even after listing out the columns, if table altered as drop a column from that has been used in VIEW is again same problem.

The permanent solution is creating the view using “WITH SCHEMABINDING” option.
lets, I will explain it in my next blog.

Share this:

  • Facebook
  • LinkedIn
  • Twitter
  • Tumblr
  • Print
  • Email

Like this:

Like Loading...

Related

Leave a Reply Cancel reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

You are commenting using your WordPress.com account. ( Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. ( Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. ( Log Out /  Change )

Cancel

Connecting to %s

Similarity and Difference between Truncate and Delete
Running Queries Details

  • Upcoming Events

    No upcoming events

  • Follow WikiDBA on WordPress.com
  • Virendra Yaduvanshi

    Sector - 46, Gurgaon
    +91 8130396263
    Lunch: 11am - 2pm
    Dinner: M-Th 5pm - 11pm, Fri-Sat:5pm - 1am
Blog at WordPress.com.
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy
  • Follow Following
    • WikiDBA
    • Join 691 other followers
    • Already have a WordPress.com account? Log in now.
    • WikiDBA
    • Customize
    • Follow Following
    • Sign up
    • Log in
    • Copy shortlink
    • Report this content
    • View post in Reader
    • Manage subscriptions
    • Collapse this bar
%d bloggers like this: