I have searched and searched and cannot work out how to resolve my problem. I am actually not sure it is possible but thought I'd post here and check.
The issue:
I have a stored procedure where I make the following call (there are 2 because I was trying different things but both fail):
SET @olddate = '1606'
SET @newdate = '1706'
SET @TableName = 'sometablename'
SET @sql = 'SP_RENAME ''[DBName' + @olddate + '.dbo.' + @TableName + @olddate +'].[ColumnName' + @olddate + 'restofname]'',''[ColumnName' + @newdate + 'restofname]'''
EXECUTE sp_executesql @sql
Other option:
SET @olddate = '1606'
SET @newdate = '1706'
SET @TableName = 'sometablename'
SET @old = '[DBName' + @olddate + '.dbo.' + @TableName + @olddate+']."[ColumnName' + @olddate + 'restofname]"'
SET @new = 'ColumnName' + @newdate + 'restofname'
EXECUTE sp_rename @objname = @old,@newname = @new, @objtype = 'COLUMN'
I saved this in a stored procedure, and then ran it in another query which has a different database context and got the following error for the first:
No item by the name of '[DBName.dbo.TableName.ColumnName]' could be found in the current database '[Other database]', given that @itemtype was input as '(null)'.
and this for the second:
Either the parameter @objname is ambiguous or the claimed @objtype (COLUMN) is wrong.
Now, what I'm wondering is: can I even do this? write a stored procedure that runs sp_rename in a table in one database and then call that stored procedure from another database?
I've also tried every permutation of putting open and closed brackets, [ and ] around parts of my old and new column names. Also tried putting N before the string. It's a bit of trial and error though and thus far nothing has worked.