Does sp_rename on a column preserve the ms_description?
sql-server
Did some checking as couldn’t find help in the MSDN documentation. My test on SQL 2016 shows that since the column_id isn’t changing, the existing mapping of the description for the column is preserved.
Research to Validate if SP_RENAME left the existing ms_description on the column when just changing the existing name
set nocount on;
go
if object_id('dbo.TestMe', 'U') is not null
begin
print 'Dropped table dbo.TestMe per existed';
drop table dbo.TestMe;
end;
go
print 'CREATE TABLE dbo.TestMe';
go
create table dbo.TestMe
(
foobar int identity(1, 1) not null primary key
, ColumnToDescribe varchar(100) null
);
go
print 'Add first extended property';
exec sys.sp_addextendedproperty
@name = 'ms_description'
, @value = 'Original Description Before I Rename It'
, @level0type = 'SCHEMA'
, @level0name = 'dbo'
, @level1type = 'TABLE'
, @level1name = 'TestMe'
, @level2type = 'COLUMN'
, @level2name = 'ColumnToDescribe';
go
print 'Get the extended property';
select
query_description = 'Original MS_Description'
, ep.class
, ep.class_desc
, ep.major_id
, ep.minor_id
, ep.name
, ep.value
from
sys.extended_properties as ep
where
ep.class_desc = 'OBJECT_OR_COLUMN'
and ep.major_id = object_id('dbo.TestMe');
go
print 'Use sp_rename to change column name';
go
exec sys.sp_rename
@objname = 'dbo.TestMe.ColumnToDescribe'
, @newname = 'ChangedTheName'
, @objtype = 'COLUMN';
go
print 'Get extended property after rename and see if still exists mapped to same object';
go
select
query_description = 'After sp_rename'
, ep.class
, ep.class_desc
, ep.major_id
, ep.minor_id
, ep.name
, ep.value
from
sys.extended_properties as ep
where
ep.class_desc = 'OBJECT_OR_COLUMN'
and ep.major_id = object_id('dbo.TestMe');
go
if object_id('dbo.TestMe', 'U') is not null
begin
print 'Cleaning up after myself to remove the dbo.TestTable';
drop table dbo.TestMe;
end;
go
declare @descriptioncount int;
select
@descriptioncount = count(*)
from
sys.extended_properties as ep
where
ep.class_desc = 'OBJECT_OR_COLUMN'
and ep.major_id = object_id('dbo.TestMe');
print 'Matching ms_descriptions now that removed table: (should be zero) '
+ cast(@descriptioncount as varchar(100)); I know it’s probably pretty obvious, but I had someone ask me, so figured proving the mapping for ms_description is maintained would be a good thing to walk through. Score another point for Microsoft, for design practices