Jak pobrać wiele kolumn do użycia w pętli kursora?

Kiedy próbuję uruchomić następujący fragment SQL wewnątrz pętli kursora,

set @cmd = N'exec sp_rename ' + @test + N',' +
           RIGHT(@test,LEN(@test)-3) + '_Pct' + N',''COLUMN'''

Otrzymuję następujący komunikat,

Msg 15248, Poziom 11, Stan 1, Procedura sp_rename, linia 213
Albo parametr @objname jest niejednoznaczny, albo deklarowana @objtype (kolumna) jest błędna.

Co jest nie tak i jak to naprawić ? Próbowałem zawijać nazwę kolumny w nawiasy [] i podwójne cudzysłowy "", jak sugerowały niektóre wyniki wyszukiwania.

Edycja 1 -

Oto cały scenariusz. Jak przekazać nazwę tabeli do zmiany nazwy sp ? Nie jestem pewien, jak to zrobić, ponieważ nazwy kolumn są w jednej z wielu tabel.

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
--COMMIT TRANSACTION

Edycja 2 - Skrypt jest przeznaczony do zmiany nazw kolumn, których nazwy pasują do wzorca, w tym przypadku z prefiksem "pct". Kolumny występują w różnych tabelach w bazie danych. Wszystkie nazwy tabel są poprzedzone "TestData".

Author: Joe, 2012-06-14

1 answers

Oto nieco zmodyfikowana wersja. Zmiany są odnotowywane jako komentarz do kodu.

BEGIN TRANSACTION

declare @cnt int
declare @test nvarchar(128)
-- variable to hold table name
declare @tableName nvarchar(255)
declare @cmd nvarchar(500) 
-- local means the cursor name is private to this code
-- fast_forward enables some speed optimizations
declare Tests cursor local fast_forward for
 SELECT COLUMN_NAME, TABLE_NAME
   FROM INFORMATION_SCHEMA.COLUMNS 
  WHERE COLUMN_NAME LIKE 'pct%' 
    AND TABLE_NAME LIKE 'TestData%'

open Tests
-- Instead of fetching twice, I rather set up no-exit loop
while 1 = 1
BEGIN
  -- And then fetch
  fetch next from Tests into @test, @tableName
  -- And then, if no row is fetched, exit the loop
  if @@fetch_status <> 0
  begin
     break
  end
  -- Quotename is needed if you ever use special characters
  -- in table/column names. Spaces, reserved words etc.
  -- Other changes add apostrophes at right places.
  set @cmd = N'exec sp_rename ''' 
           + quotename(@tableName) 
           + '.' 
           + quotename(@test) 
           + N''',''' 
           + RIGHT(@test,LEN(@test)-3) 
           + '_Pct''' 
           + N', ''column''' 

  print @cmd

  EXEC sp_executeSQL @cmd
END

close Tests 
deallocate Tests

ROLLBACK TRANSACTION
--COMMIT TRANSACTION
 118
Author: Nikola Markovinović,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-06-14 19:35:16