SQL SERVER 2008
我有一个数据,我正在尝试转轴。 运行以下代码遇到错误
The number of elements in the select list exceeds the maximum allowed number of 4096 elements.
代码:
DECLARE @Cols NVARCHAR(MAX);
DECLARE @SQL NVARCHAR(MAX);
DECLARE @Sel NVARCHAR(MAX);
DECLARE @GENERIC NVARCHAR(MAX)
SET @Cols = STUFF((SELECT distinct ',' + QUOTENAME([Description])
FROM STAGING_TEMP
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'');
SET @Sel = STUFF((SELECT distinct ',' + QUOTENAME([Description]) +
' AS DESC'+CAST(row_number () over (order by ([Description])) as varchar(250))
FROM STAGING_TEMP
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'');
SET @SQL = N'
SELECT DISTINCT [Generic Name], '+ @Sel+N'
FROM STAGING_TEMP
pivot
(
MAX(Description) For Description IN ('+ @Cols + N')
) P';
EXECUTE( @SQL);
当我运行单个代码段时,我开始知道下面的代码产生40000 desc值,它具有35k重复值AS Different DESC。
SET @Sel = STUFF((SELECT distinct ',' + QUOTENAME([Description]) +
' AS DESC'+CAST(row_number () over (order by ([Description])) as varchar(250))
FROM STAGING_TEMP
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'');
有没有办法处理这种情况。请建议