¿Cómo obtengo varias columnas para usarlas en un bucle de cursor?
Cuando intento ejecutar el siguiente fragmento de código SQL dentro de un bucle de cursor,
set @cmd = N'exec sp_rename ' + @test + N',' +
RIGHT(@test,LEN(@test)-3) + '_Pct' + N',''COLUMN'''
Recibo el siguiente mensaje,
Msg 15248, Nivel 11, Estado 1, Procedimiento sp_rename, Línea 213
O bien el parámetro@objname
es ambiguo o la COLUMNA reclamada@objtype
es incorrecta.
¿Qué está mal y cómo lo arreglo ? Traté de envolver el nombre de la columna entre corchetes []
, y comillas dobles ""
como algunos de los resultados de búsqueda sugeridos.
Aquí está todo el script. ¿Cómo paso el nombre de la tabla al renombrar sp ? No estoy seguro de cómo hacerlo, ya que los nombres de las columnas están en una de las muchas tablas.
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
[6] Editar 2 -
El script está diseñado para renombrar columnas cuyos nombres coinciden con un patrón, en este caso con un prefijo "pct". Las columnas aparecen en una variedad de tablas dentro de la base de datos. Todos los nombres de tabla tienen el prefijo "TestData". 67
1 answers
Aquí está la versión ligeramente modificada. Los cambios se anotan como comentario del código.
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/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-06-14 19:35:16
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-06-14 19:35:16