I have written many articles about renaming a tables, columns and procedures SQL SERVER - How to Rename a Column Name or Table Name, here I found something interesting about renaming the stored procedures and felt like sharing it with you all.
The interesting fact is that when we rename a stored procedure using SP_Rename command, the Stored Procedure is successfully renamed. But when we try to text the procedure using sp_helptext, the procedure will be having the old name instead of new name.
Example,
- Create a Stored Procedure in AdventureWorks Database named SP_Employee.
USE AdventureWorks
GO
CREATE PROCEDURE sp_Employee
AS
SELECT * FROM dbo.Employee
WHERE FName LIKE ‘%i%’
ORDER BY EMPID
GO

- Rename the Stored Procedure SP_Employee to SP_Getemployee
After Creating Stored Procedure, now we want to rename a stored procedure. So using sp_rename we can change the name of Stored Procedure as shown below:
sp_rename ‘SP_Employee’, ‘SP_GetEmployee’

- Use sp_helptext to see the stored procedure
USE AdventureWorks
GO
sp_helptext sp_getemployee

We can see the name of the stored procedure is the old name and not new name but when we try to access the old name there is an error that sp not found.

Conclusion
This happens because when the store procedure is renamed, the sys.procedures system table is not getting updated. The only solution to this is to drop the stored procedure and re-create it with the new name.
Reference : Pinal Dave (http://www.SQLAuthority.com)






Nice post, Pinal. I know many people just rename stored procedures, functions, views etc right from the Management Studio and land up with this problem.
Just wanted to add that this problem exists with Views, Triggers and Functions too. http://msdn.microsoft.com/en-us/library/ms188351(SQL.90).aspx
Thanks Pinal for initiating this discussion. I found this bit interesting and investigated it further. I was curious how SSMS manages this, because when you generate the ALTER/CREATE script from SSMS it generates the correct object name.
I have posted my findings here: http://www.sqlserverandxml.com/2008/09/be-careful-when-renaming.html
I request all the readers of my article to read the article posted by Jacob here. It explains in depth the why this happens and how to avoid it.
http://www.sqlserverandxml.com/2008/09/be-careful-when-renaming.html
Summary of the article must be memorized by any developer.
Regards,
Pinal
Hi Pinal,
I read the article and tried to follow the same.
Though after executing sp_helptext, I saw that the name is not changed, but when i see in the sys.procedures, i found that the name of the procedure is changed i.e the sys.procedures system table do get updated.
Pls check and clarify if I am going wrong.
Thanks in advance.
Abhishek