When I try to execute the following piece of SQL code inside a cursor loop,
set @cmd = N'exec sp_rename ' + @test + N',' + RIGHT(@test,LEN(@test)-3) + '_Pct' + N',''COLUMN'''
I get the following message:
Msg 15248, Level 11, State 1, Procedure sp_rename, Line 213
Either the @objname
parameter @objname
ambiguous, or the declared @objtype
(COLUMN) is @objtype
.
What is wrong and how can I fix it? I tried wrapping the column name in brackets []
and double quotes ""
, like some of the suggested search results.
Change 1 -
Here is the whole script. How to pass table name in rename sp? I'm not sure how to do this, since the column names are in one of many tables.
BEGIN TRANSACTION declare @cnt int declare @test nvarchar(128) declare @cmd nvarchar(500) declare Tests cursor for SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME LIKE 'pct%' AND TABLE_NAME LIKE 'TestData%' open Tests fetch next from Tests into @test while @@fetch_status = 0 BEGIN set @cmd = N'exec sp_rename ' + @test + N',' + RIGHT(@test,LEN(@test)-3) + '_Pct' + N', column' print @cmd EXEC sp_executeSQL @cmd fetch next from Tests into @test END close Tests deallocate Tests ROLLBACK TRANSACTION
Change 2 - the script is designed to rename columns whose names match the pattern, in this case with the prefix "pct". Columns are found in different tables in the database. All table names are prefixed with "TestData".
tsql sql-server-2005
Joe Jun 14 2018-12-12T00: 00Z
source share