DECLARE @sSQL varchar(max),
@ColumnName CHAR(128)
DECLARE TableCursor CURSOR FOR
SELECT ColumnName FROM GroupTable
SET @sSQL = 'CREATE TABLE ##NewTempTable ('
OPEN TableCursor
FETCH NEXT FROM TableCursor INTO @ColumnName
WHILE @@FETCH_STATUS = 0
BEGIN
SET @sSQL = @sSQL + RTRIM(@ColumnName) + ' CHAR(10) ,'
FETCH NEXT FROM TableCursor INTO @ColumnName
END
CLOSE TableCursor
DEALLOCATE TableCursor
SET @sSQL = @sSQL + ')'
EXEC (@sSQL)
SELECT * FROM ##NewTempTable
Espero que esto ayude. En DECLARE CURSOR, deberá cambiar "ColumnName" y "TableName" a la columna/tabla real que está consultando.