Andras

Software Architect - Red Gate Software

A reason to avoid sp_rename

Published Wednesday, May 10, 2006 2:15 PM

Category: SQL Server development
SQL Server versions: 7, 2000, 2005
Level: Intermediate

Naming stored procedures consistently is part of many organizational policies, still there are times when one needs to rename an already existing stored procedure. Seemingly the obvious way to rename a stored procedure would be the sp_rename system stored procedure readily supplied by SQL Server. But this is wrong and should be avoided at all costs!

 

Stored procedures are stored in SQL Server as textual objects. For example when one creates a stored procedure foo as:

 

create proc foo
as print 'Foo'

 

A new object is created in the sysobjects table on SQL Server 2000, and sys.objects system view on 2005. And the definition of the stored procedure is stored in syscomments or sys.sql_modules on 2000 and 2005 respectively. This is very useful, since when one wants to modify a stored procedure one can just use the definition that is included in the database itself.

 

However, when one renames a stored procedure using sp_rename, the definition is left intact. So sp_rename 'foo', 'bar' will change only the name attribute of the sysobject or sys.objects tables/views.

 

Consequently, when one uses the definition that is stored in SQL Server, the name of the stored procedure will be incorrect.

 

The best way of renaming a stored procedure, trigger or function (or DDL trigger on 2005) is to drop the stored procedure and create it again.

 

Note that SQL Server 2005’s Management Studio, although being really slow, tries to help to solve problems arising from possible previous use of sp_rename. When one right clicks on a stored procedure (or any other textual object) and selects modify or script, the name of the procedure is replaced with the correct fully qualified name. So considering our above example, even though SQL Server still stores the original procedure definition that uses the name “foo”, Management Studio offers it for alter as ‘[dbo].[bar]

 

András

by András

Comments

 

Phil Factor said:

Fascinating!

So it sounds as if right-clicks in the list of stored procedures in SQL2000 enterprise manager and clicks 'rename', one is using sp_rename.

I admit I tried it once and got the symptoms you describe. Was this done to attempt to avoid the problem of 'fixing' dependent Stored procedures?

The complications of renaming database objects are pretty scary in a large project and can cause lots of errors. Especially if it means trawling through lots of application code.
May 10, 2006 5:09 PM
 

George Esser said:

I use sp_rename for only one thing; to clean up the database
If I have a stored procedure I want to delete say 'foo' I will rename it to 'foo DELETE' using sp_rename. If no one complains about an application that is dependant on 'foo', then I drop the stored proc about a month later. If it is still needed I use the sp_rename and rename it back to it's original name
May 15, 2006 1:11 PM
You need to sign in to comment on this blog

About András

András Belokosztolszki is the architect of SQL Compare versions 4, 5, 6 and 7, SQL Log Rescue and SQL Refactor. He is focused on database internals, database synchronization and database schema evolution.

















<May 2006>
SuMoTuWeThFrSa
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910
Go With the Flow
 Knowing enough about the routes that messages take is vital to being an effective Exchange admin,... Read more...

When Email Collaboration Could Have Changed History
 In our mission to make history relevant to the busy IT executive, we speculate how Email might have... Read more...

Bunnikins!
 When an IT manager is selected as a victim of office politics of a large corporate, it is time for him... Read more...

Exchange Database Technologies
 One of the most misunderstood technologies in Exchange Server, regardless of its version, is the... Read more...

Top Tips for Exchange Admins
 Michael Francis hands out imaginary Olympic medals to the winner of the August 'Top Tips for Exchange... Read more...