sp_rename upates the name in sys.objects but it does not update the definition or objects referencing it.
Renaming a stored procedure, function, view, or trigger will not change the name of the corresponding object name in the definition column of the sys.sql_modules catalog view. Therefore, we recommend that sp_rename not be used to rename these object types. Instead, drop and re-create the object with its new name. (MSDN)
When you run this query:
Create Procedure Test
set nocount on
Exec sp_rename 'Test', 'Tests'
Select * From sys.sql_modules
definition column in
sql_modules still contains
object_id definition ...
599673184 Create Procedure Test as begin set nocount on Select id From T1 end
- Rename a Table (or an object)
When an object is rename, any object using it and referencing the old name will fail.
Here are a View and a Table beining renamed:
Create Table T1(id int)
Create View V1 AS Select id From dbo.T1
Exec sp_rename 'T1', 'T2'
Again if you look at the definition, both the procedure and the view reference T1 (which does not exist anymore):
759673754 Create Procedure Test as begin set nocount on Select id From T1 end
791673868 Create View V1 AS Select id From dbo.T1
And both will fail with this error when you Select from it or Execute it:
Invalid object name 'dbo.T1'.
Could not use view or function 'V1' because of binding errors.
You get the same error if you refresh the view:
All objects with references to the old name will break.
Before renaming an object (table, procedure, function, ...), you can look at its dependencies using
sys.sql_expression_dependencies (MSDN) (deprecated: