Just spent far too much time trying to figure out why this was happening on a production database I can only access via mylittlesql. Couldn't reproduce the problem but made this script from bits of sp_rename so when it does happen next time I can find out exactly why. Yes is overkill, but might help somebody else.
There is an issue if you ever somehow manage to get '[' or ']' into the actual column name as stored in sys.columns, (? 'nvarchar' as your column name ???? ). PARSENAME doesn't cope with []'s and returns null, so sp_rename won't work.
This will only help diagnose the issue for the 'column' case with the error code 15248 which is where I keep having this issue:
declare @objname nvarchar(1035) = N'dbo.EducationTypes.nvarchar' -- input to sp_rename
declare @newname sysname = N'EducationTypeTitle' -- input to sp_rename
declare @UnqualOldName sysname,
@QualName1 sysname,
@QualName2 sysname,
@QualName3 sysname,
@OwnAndObjName nvarchar(517),
@SchemaAndTypeName nvarchar(517),
@objid int,
@xtype nchar(2),
@colid int,
@retcode int
select @UnqualOldName = parsename(@objname, 1),
@QualName1 = parsename(@objname, 2),
@QualName2 = parsename(@objname, 3),
@QualName3 = parsename(@objname, 4)
print 'Old Object Name = ''' + convert(varchar,isnull(@UnqualOldName ,'')) + ''''
-- checks that parsename is getting the right name out of your @objname parameter
print 'Table name:'
if @QualName2 is not null
begin
print QuoteName(@QualName2) +'.'+ QuoteName(@QualName1)
select @objid = object_id(QuoteName(@QualName2) +'.'+ QuoteName(@QualName1))
end
else
begin
print QuoteName(@QualName1)
select @objid = object_id(QuoteName(@QualName1))
end
-- check if table is found ok
print 'Table Object ID = ''' + convert(varchar,isnull(@objid ,-1)) + ''''
select @xtype = type from sys.objects where object_id = @objid
print '@xtype = ''' + convert(varchar,isnull(@xtype,'')) + ''' (U or V?)'
if (@xtype in ('U','V'))
begin
print 'select @colid = column_id from sys.columns where object_id = ' +
convert(varchar,isnull(@objid,0)) + ' and name = ''' +
@UnqualOldName + ''''
select * from sys.columns where object_id = @objid -- and name = @UnqualOldName
select @colid = column_id from sys.columns
where object_id = @objid and name = @UnqualOldName
print 'Column ID = ''' + convert(varchar,isnull(@colid,-1)) + ''''
end
This will output some helpful messages in the Messages tab (of SSMS or whatever you are using) and the table fields in the Results tab.
Good luck.