我有一张非常大的桌子(100多个字段),我需要比较两个记录。为此,我决定使用PIVOT。记录将由用户选择(因此它们将是动态的),但是现在我将它们作为静态放入。
我的SQL看起来像这样:
-- Where clause params
DECLARE @whereClauseParam VARCHAR(MAX) = '[42] <> [600]' --<-- User will need to determine what the condition will be
--Get the Fields required for the initial pivot
DECLARE @Fields VARCHAR(MAX)= '';
SELECT @Fields+=QUOTENAME(t.name)+', '
FROM sys.columns AS t
WHERE t.object_id = OBJECT_ID('tblSQLAdminInventory')
AND t.name <> 'TransID'
AND t.system_type_id = '60';
--36
--48
--52
--56
--60
--61
--62
--104
--106
--127
--165
--167
--231
--239
-- Get the KeyId's with alias added
DECLARE @keyIDs VARCHAR(MAX)= '';
SELECT @keyIDs+=QUOTENAME(t.TransID)+' AS [KeyID_'+CAST(t.TransID AS VARCHAR(10))+'], '
FROM tblSQLAdminInventory AS t
WHERE TransID IN ('42', '600');
-- Get the KeyId's without alias
DECLARE @keyIDs1 VARCHAR(MAX)= '';
SELECT @keyIDs1+=QUOTENAME(t.TransID)+', '
FROM tblSQLAdminInventory AS t
WHERE TransID IN ('42', '600');
--Generate Dynamic SQL
DECLARE @SQL2 VARCHAR(MAX)= 'SELECT Value AS FieldName, ';
PRINT @SQL2+SUBSTRING(@keyIDs, 1, LEN(@keyIDs)-1)
PRINT @keyIDs
PRINT LEN(@keyIDs)
SELECT @SQL2+=SUBSTRING(@keyIDs, 1, LEN(@keyIDs)-1)+'
FROM
(SELECT TransID , Value , FieldName
FROM
(SELECT TransID,'+SUBSTRING(@Fields, 1, LEN(@Fields)-1)+'
FROM tblSQLAdminInventory) p
UNPIVOT
(FieldName FOR Value IN
('+SUBSTRING(@Fields, 1, LEN(@Fields)-1)+')
)AS unpvt) AS SourceTable
PIVOT
(
MAX(FieldName)
FOR TransID IN ('+SUBSTRING(@keyIDs1, 1, LEN(@keyIDs1)-1)+')
) AS PivotTable
--WHERE '+@whereClauseParam
PRINT(@SQL2);
EXECUTE(@SQL2);
PIVOT上方的3条PRINT线打印出来如下:
SELECT Value AS FieldName, [42] AS [KeyID_42], [600] AS [KeyID_600]
[42] AS [KeyID_42], [600] AS [KeyID_600],
41
一切看起来都很好。但是,我的PIVOT上有一个错误:
Msg 537, Level 16, State 5, Line 49
Invalid length parameter passed to the LEFT or SUBSTRING function.
SELECT Value AS FieldName,
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ','.
有人能告诉我为什么我的PRINT语句会识别所有3个字段名称,但我的PIVOT语句没有?
奖金问题:当我过滤t.system_type_id ='56'时,它会返回数据。对于任何其他system_type_id,我收到错误并且没有返回任何记录。
根据要求,SQL2运行时没有错误:
SELECT Value AS FieldName, [42] AS [KeyID_42], [600] AS [KeyID_600]
FROM
(SELECT TransID , Value , FieldName
FROM
(SELECT TransID,[ErisaPlanEndsMM], [ErisaPlanEndsDD], [MLRAvgLivesNumber], [MLRAvgLivesRptYear]
FROM tblSQLAdminInventory) p
UNPIVOT
(FieldName FOR Value IN
([ErisaPlanEndsMM], [ErisaPlanEndsDD], [MLRAvgLivesNumber], [MLRAvgLivesRptYear])
)AS unpvt) AS SourceTable
PIVOT
(
MAX(FieldName)
FOR TransID IN ([42], [600])
) AS PivotTable
--WHERE [42] <> [600]
答案 0 :(得分:1)
不确定这是否会解决您的所有问题,但您可以使用它来构建您的@keyIds并消除对Substring()的需求
-- Get the KeyId's with alias added
DECLARE @keyIDs VARCHAR(MAX),
@keyIDs1 VARCHAR(MAX);
SELECT @keyIDs = COALESCE(@keyIDs + ',','') + QUOTENAME(t.TransID) + ' AS [KeyID_' + CAST(t.TransID AS VARCHAR(10)) + ']',
@keyIDs1 = COALESCE(@keyIDs1 + ',','') + QUOTENAME(t.TransID)
FROM tblSQLAdminInventory AS t
WHERE TransID IN ('42', '600');