THE SQL Server Blog Spot on the Web

Welcome to SQLblog.com - The SQL Server blog spot on the web Sign in | |
in Search

Davide Mauri

A place for my thoughts and experiences the Microsoft Data Platform

bug with sp_rename + sp_refreshview

Today I’ve found with my Italian collegues a “nice” bug that made us and the customer mad for several hours.

The problem is related to sp_rename usage and sp_refreshview.

If you have a view like this one

create view dbo.a

as

select c = 1

go

and for any reason someone renames it (for example because he wants to change the view but don’t want to drop the old one, so it rename to something like myview_old):

exec sp_rename 'a', 'b'

go

create view dbo.a

as

select c = 2

go

If you do, everything still works perfectly:

select * from dbo.a

select * from dbo.b

go

And now the “magic”: try to use the refreshview procedure on the “b” view

sp_refreshview 'dbo.b'

go

and voilà, the view “a” will be changed back to the orginal script! If you execute

select * from dbo.a

go

you will get the same result of the view “b”. That’s magic, isn’t it?

And and it’s not only a problem of results. If you open the view “a” you will see that its body has been changed into the body of view “b”!!!

I’ve tested the bug on SQL Server 2005, SQL Server 2008 and SQL Server 2008 R2.

To be honest is not something dangerous, and it should not harm anyone, but just know that if sometimes you have your views reset back to a previous situation, you may well be in the same situation we’ve experienced. Unfortunately there is no way to “detect” this problem even using SQL Profiler or even DDL Events since no “ALTER VIEW” (nor DROP VIEW followed by CREATE VIEW) gets fired in the sp_refreshview process.

There is a workaround to fix it? Of course the answer is yes :)

Just alter the “b” view manually and everything will be fixed:

alter view dbo.b

as

select c = 1

go

from now on the sp_refreshview will work correctly [:)] Just keep it in mind.

Published Tuesday, October 12, 2010 9:02 AM by Davide Mauri
Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments

Leave a Comment

(required) 
(required) 
Submit

About Davide Mauri

Director of Software Development & Cloud Infrastructure @ Sensoria, an innovative smart garments and wearable company. After more than 15 year playing with the Microsoft Data Platform, with a specific focus on High Performance databases, Business Intelligence, Data Science and Data Architectures, he's now applying all his skills to IoT, defining architectures to crunch numbers, create nice user experiences and provide meaningful insights, all leveraging Microsoft Azure cloud. MVP on Data Platform since 2006 he has a very strong background development and love both the ER model and OO principles. He is also a fan of Agile Methodology and Automation, which he tries to apply everywhere he can, to make sure that "people think, machines do".

This Blog

Syndication

Privacy Statement