Acredito que isso resolva o seu problema.
SET DATEFORMAT DMY
CREATE TABLE #A ( A DATE, B VARCHAR(3), C DECIMAL(18,2) )
declare @x int = 0
declare @d datetime
declare @r int
DECLARE @s char(3)
while @x < 20
begin
SET @s = ( SELECT C1 AS [text()]
FROM ( SELECT TOP (1) c1
FROM ( VALUES /*('A'), RESERVADO*/ ('B'), ('C'), ('D'), ('E'), ('F'), ('G'), ('H'), ('I'), ('J'),
('K'), ('L'), ('M'), ('N'), ('O'), ('P'), ('Q'), ('R'), ('S'), ('T'),
('U'), ('V'), ('W'), ('X'), ('Y'), ('Z')
) AS T1(c1)
ORDER BY ABS(CHECKSUM(NEWID()))
) AS T2 FOR XML PATH('') )
insert into #a values ( DATEADD( DAY, FLOOR(RAND() * (10-1+1)+1), GETDATE() ), @s, CAST( RAND() * 100.0 AS DECIMAL(18,2) ) )
set @x = @x + 1
end
SELECT DISTINCT A
INTO #B
FROM #A
DECLARE @A DATE, @B VARCHAR(3), @C DECIMAL(18,2)
SET @A = NULL
SET @B = NULL
SET @C = NULL
DECLARE @CMD VARCHAR(8000)
DECLARE CR_B SCROLL CURSOR FOR
SELECT DISTINCT B
FROM #A
OPEN CR_B
FETCH FIRST FROM CR_B INTO @B
WHILE @@FETCH_STATUS = 0
BEGIN
SET @CMD =
'ALTER TABLE #B ADD ' + @B + ' VARCHAR(100)'
EXEC ( @CMD )
FETCH NEXT FROM CR_B INTO @B
END
CLOSE CR_B
DEALLOCATE CR_B
SET @A = NULL
SET @B = NULL
SET @C = NULL
DECLARE CR_A SCROLL CURSOR FOR
SELECT A,B,C
FROM #A
OPEN CR_A
FETCH FIRST FROM CR_A INTO @A, @B, @C
WHILE @@FETCH_STATUS = 0
BEGIN
SET @CMD =
'UPDATE #B SET '+
CAST( @B AS VARCHAR )+' = ISNULL('+CAST( @B AS VARCHAR )+', 0 ) + '+
CAST( @C AS VARCHAR )+' WHERE A = '+CHAR(39)+
CONVERT( VARCHAR(10),@A, 103 )+CHAR(39)
EXEC ( @CMD )
FETCH NEXT FROM CR_A INTO @A, @B, @C
END
CLOSE CR_A
DEALLOCATE CR_A
--TABELA ORIGINAL
SELECT A, B, C
FROM #A
--RESULTADO FINAL
SELECT *
FROM #B
ORDER BY 1 ASC
drop table #A
DROP TABLE #B