Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

Search

XML Feeds

Google Ads

« SQL Server Stored Procedure with nvarchar parameterSQL Server - Set based random numbers »
comments
Rate Post:
submit to reddit Digg!FacebookDotnetkicks

sp_refreshview or how to make sure that the view will have the underlying table changes

Got a question about this on our Microsoft SQL Server Programming Forum so you know it is time for a quick blog post.

Did you know that when you create a view and then later change the table the view is not updated?
let me show you what I mean.
Run the following block of code

  1. CREATE TABLE TestTable (id INT,SomeCol VARCHAR(666))
  2. GO
  3.  
  4. INSERT TestTable VALUES(1,'ABC')
  5. GO
  6.  
  7. SELECT * FROM TestTable
  8. GO
  9.  
  10. CREATE VIEW TestView
  11. AS
  12. SELECT * FROM TestTable
  13. GO
  14.  
  15. SELECT * FROM TestView
  16. GO

Now we will change that table by adding another column

  1. ALTER TABLE TestTable
  2. ADD Col2 DATETIME DEFAULT CURRENT_TIMESTAMP
  3. GO
  4.  
  5. INSERT TestTable(id,SomeCol) VALUES(2,'XYZ')
  6. GO

Now run the selects again

  1. SELECT * FROM TestTable
  2. GO
  3.  
  4. SELECT * FROM TestView
  5. GO

See what happened? The TestView does not include the Col2 column. So what can you do? There are at least two things that you can do. You can recreate the view with a create or alter statement or you can use sp_refreshview, run the code below to see how that works

  1. SP_REFRESHVIEW TestView
  2. GO
  3.  
  4. --All good now
  5. SELECT * FROM TestView
  6. GO
  7.  
  8.  
  9. --Clean up this mess--
  10. DROP VIEW TestView
  11. GO
  12.  
  13.  
  14. DROP TABLE TestTable
  15. GO

And yes I know 'real' SQL programmers never use SELECT * and 'real' SQL programmers name their defaults ;-)




*** If you have a SQL related question try our Microsoft SQL Server Programming forum or our Microsoft SQL Server Admin forum

About the Author

User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
Social SitingsTwitterFacebookLinkedInHomePageLTD RSS Feed
1244 views
submit to reddit Digg!FacebookDotnetkicks

Comments and Feedback

4 comments

Comment from: LNBruno [Member] Email
*****
Thank you, Denis. It's true what they say about "use it or lose it!"

I'm talking about system procedures, of course....
01/28/09 @ 10:27
Comment from: SQLDenis [Member] Email
*****
As long as the procs are documented.
Imagine someone took away xp_fixeddrives or sp_MSForEachtable procs, how many people would get screwed by that?
01/28/09 @ 11:03
Comment from: chrissie1 [Member] Email
How about unittesting ;-)
01/28/09 @ 12:28
Comment from: Michael Swart [Visitor] · http://dbwhisperer.blogspot.com
*****
Wow. I never realized this behavior. However, I'm happy to learn that synonyms still behave as expected.
02/02/09 @ 06:44

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)