Ver Mensaje Individual
  #3 (permalink)  
Antiguo 10/01/2012, 16:55
jepequi
 
Fecha de Ingreso: agosto-2011
Mensajes: 32
Antigüedad: 13 años, 2 meses
Puntos: 0
Respuesta: pivot dinamico

Bueno ps ya solucione el error... la verdad no supe era asi q por si alguien lo necesita dejo el codigo del q me funciono...

DECLARE @TableYears AS TABLE([Year] nvarchar(50) NOT NULL)
DECLARE @Year nvarchar(50), @YearsPVT NVARCHAR(max)
INSERT INTO @TableYears SELECT DISTINCT EXVARI AS [Year] FROM tempjai
SET @Year = (SELECT MIN([Year]) FROM @TableYears)
SET @YearsPVT=N''
WHILE @Year IS NOT NULL
BEGIN
SET @YearsPVT = @YearsPVT + N',['+ CONVERT(NVARCHAR(50),@Year) + N']'
SET @Year = (SELECT MIN([Year]) FROM @TableYears WHERE [Year]>@Year)
END
SET @YearsPVT = SUBSTRING(@YearsPVT,2,LEN(@YearsPVT))
--PRINT @YearsPVT
DECLARE @SQL NVARCHAR(MAX)
SET @SQL = N'SELECT *
FROM (
SELECT ESNRSO, EXVARI [Year],excont FROM tempjai
) pvt
PIVOT (max(excont) FOR [Year] IN (' + @YearsPVT + ')) AS PVJPQ
ORDER by esnrso'
EXECUTE sp_executesql @SQL