简体   繁体   English

PIVOT语句中的SQL子字符串问题

[英]SQL Substring issue in PIVOT statement

I've got a very large table (100+ fields) where I need to compare two records. 我有一个非常大的表(超过100个字段),在这里我需要比较两个记录。 For this I've decided to use a PIVOT. 为此,我决定使用PIVOT。 The records will be selected by the user (so they'll be dynamic), but for now I'm putting them in as static. 记录将由用户选择(因此它们将是动态的),但是现在我将它们作为静态放入。

My SQL looks like this: 我的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);

The 3 PRINT lines above my PIVOT print out like this: PIVOT上方的3条PRINT行打印如下:

SELECT Value AS FieldName, [42] AS [KeyID_42], [600] AS [KeyID_600]
[42] AS [KeyID_42], [600] AS [KeyID_600], 
41

All looks well. 一切看起来不错。 But then, there's an error on my PIVOT: 但是,我的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 ','.

Can anyone tell me why my PRINT statement recognizes all 3 field names, but my PIVOT statement doesn't? 谁能告诉我为什么我的PRINT语句可以识别所有3个字段名称,但是我的PIVOT语句却不能识别?

BONUS QUESTION: When I filter on t.system_type_id = '56' it returns data. 奖金问题:当我在t.system_type_id ='56'上进行过滤时,它将返回数据。 For any other system_type_id I get the error and no records returned. 对于其他任何system_type_id,我都会收到错误,并且没有记录返回。

EDIT 编辑

As requested, here is SQL2 when it runs without errors: 根据要求,下面是运行无错误的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]

Not sure if this will solve all of your problems but you can use this to build your @keyIds and eliminate the need for Substring() 不知道这是否可以解决您所有的问题,但是您可以使用它来构建@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');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM