2016-12-31 59 views
0

四處錯誤:子查詢返回不止一個值,不允許變量是=,=,<, <= , >,> =

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

請幫我這個查詢:

DECLARE @TYPENAME varchar(20) = 'Hardness'; 
DECLARE @MODEL varchar(20) = 'LEEB'; 
DECLARE @SPECIFICATION varchar(20) = 'Other'; 
DECLARE @EQUIPMENT varchar(20) = 'Phase II Model: PHT-18 00 S/N: PHO109112815'; 

BEGIN 

SELECT 
    Document.TypeName 
    ,Document.CreatedDateTime 
    ,QuestionType.SequenceNumber 
    ,QuestionType.ValueType 
    ,QuestionType.NAME 
    ,Answer.ValueAsString 
    ,Answer.ValueAsNumber 
    ,Answer.ValueAsDateTime 
    ,Person.FamilyName 
    ,Person.GivenName 
    ,QuestionType.AnswerType 
    ,Media.Media 
FROM 
    Document (NOLOCK) 
INNER JOIN 
    DocumentType (NOLOCK) ON Document.DocumentTypeGuid = DocumentType.Guid 
INNER JOIN 
    Answer (NOLOCK) ON Document.Guid = Answer.ParentDocumentGuid 
INNER JOIN 
    QuestionType (NOLOCK) ON Answer.QuestionTypeGuid = QuestionType.Guid 
LEFT JOIN 
    Media (NOLOCK) on Media.ParentGuid = Answer.Guid 
LEFT JOIN 
    Person (NOLOCK) ON Document.CreatedBySystemUserGuid = Person.Guid 
WHERE 
    DocumentType.BonIdentifier = @TYPENAME 
    AND Document.Guid IN (CASE 
          WHEN @TYPENAME = 'Hardness' 
           THEN (SELECT hrd.[DocumentGuid] 
             FROM [dbo].[Hardness] hrd 
             WHERE (@MODEL IS NULL OR REPLACE(LOWER(hrd.[HardnessType]),' ','') LIKE '%'+REPLACE(LOWER(@MODEL),' ','')+'%') 
             AND (@SPECIFICATION IS NULL OR (REPLACE(LOWER(hrd.[RockwellTestMethod]),' ','') LIKE '%'+REPLACE(LOWER(@SPECIFICATION),' ','')+'%' 
    OR REPLACE(LOWER(hrd.[LEEBTestMethod]),' ','') LIKE '%'+REPLACE(LOWER(@SPECIFICATION),' ','')+'%' 
    OR REPLACE(LOWER(hrd.[TelebrinellerTestMethod]),' ','') LIKE '%'+REPLACE(LOWER(@SPECIFICATION),' ','')+'%' 
    )) 
    AND (@EQUIPMENT IS NULL OR (REPLACE(LOWER(hrd.[RockwellEquipment]),' ','') LIKE '%'+REPLACE(LOWER(@EQUIPMENT),' ','')+'%' 
    OR REPLACE(LOWER(hrd.[LEEBEquipment]),' ','') LIKE '%'+REPLACE(LOWER(@EQUIPMENT),' ','')+'%' 
    OR REPLACE(LOWER(hrd.[TelebrinellerEquipment]),' ','') LIKE '%'+REPLACE(LOWER(@EQUIPMENT),' ','')+'%' 
    )) 
) 
ELSE 
    (
    SELECT mh.[DocumentGuid] from [dbo].[MicroHardness] mh 
    WHERE 
    (@MODEL IS NULL OR REPLACE(LOWER(mh.[HardnessType]),' ','') LIKE '%'+REPLACE(LOWER(@MODEL),' ','')+'%') 
    AND (@SPECIFICATION IS NULL OR (REPLACE(LOWER(mh.[WT Test Method]),' ','') LIKE '%'+REPLACE(LOWER(@SPECIFICATION),' ','')+'%' 
    OR REPLACE(LOWER(mh.[BM Test Method]),' ','') LIKE '%'+REPLACE(LOWER(@SPECIFICATION),' ','')+'%'  
    )) 
    AND (@EQUIPMENT IS NULL OR (REPLACE(LOWER(mh.[WT Equipment]),' ','') LIKE '%'+REPLACE(LOWER(@EQUIPMENT),' ','')+'%' 
    OR REPLACE(LOWER(mh.[BM Equipment]),' ','') LIKE '%'+REPLACE(LOWER(@EQUIPMENT),' ','')+'%'  
    )) 
) 
END 
) 
    AND QuestionType.AnswerType NOT IN ('Single Pick List', 'Multi-Pick List') 
    AND QuestionType.AnswerType <> 'Summary' 
    AND QuestionType.AnswerType <> 'Informational' 
    AND Document.Active = 1 
ORDER BY 
    Document.CreatedDateTime, QuestionType.SequenceNumber; 

END 
+2

該問題可能是在'CASE'表達式,其中子查詢返回不止一個值。你需要修正那裏的邏輯。 –

+0

我不認爲@TimBiegeleisen是對的。內部選擇預計會返回一個或多個值(請參閱條件'... AND Document.Guid in ...')。我建議你重新安排你的內部查詢到一個更可讀的佈局和檢查括號。 – FDavidov

+0

設置[壞習慣踢 - 把NOLOCK無處不在](http://blogs.sqlsentry.com/aaronbertrand/bad-habits-nolock-everywhere/) - 它是*不推薦*到處使用 - 完全相反! –

回答

0

您的查詢做工精細@Zohar佩萊德但性能變差,所以我自己找到了解決方案,通過分離條件整個查詢發現下面,

DECLARE @TYPENAME varchar(20) = 'MicroHardness'; 
DECLARE @MODEL varchar(20) = 'Wilson Tukon'; 
DECLARE @SPECIFICATION varchar(20) = 'ASTME 384'; 
DECLARE @EQUIPMENT varchar(20) = '300 BM/DF, S/N: 83180-483'; 

BEGIN 

IF(@TYPENAME = 'Hardness') 

SELECT 
    Document.Guid, 
    Document.TypeName 
    ,Document.CreatedDateTime 
    ,QuestionType.SequenceNumber 
    ,QuestionType.ValueType 
    ,QuestionType.NAME 
    ,Answer.ValueAsString 
    ,Answer.ValueAsNumber 
    ,Answer.ValueAsDateTime 
    ,Person.FamilyName 
    ,Person.GivenName 
    ,QuestionType.AnswerType 
    ,Media.Media 
FROM Document (NOLOCK) 
INNER JOIN DocumentType (NOLOCK) ON Document.DocumentTypeGuid = DocumentType.Guid 
INNER JOIN Answer (NOLOCK) ON Document.Guid = Answer.ParentDocumentGuid 
INNER JOIN QuestionType (NOLOCK) ON Answer.QuestionTypeGuid = QuestionType.Guid 
LEFT JOIN Media (NOLOCK) on Media.ParentGuid = Answer.Guid 
LEFT JOIN Person (NOLOCK) ON Document.CreatedBySystemUserGuid = Person.Guid 
WHERE 
DocumentType.BonIdentifier = @TYPENAME AND Document.Guid in 
(
    SELECT hrd.[DocumentGuid] from [dbo].[Hardness] hrd 
    WHERE 
    (@MODEL IS NULL OR REPLACE(LOWER(hrd.[HardnessType]),' ','') LIKE '%'+REPLACE(LOWER(@MODEL),' ','')+'%') 
    AND (@SPECIFICATION IS NULL OR (REPLACE(LOWER(hrd.[RockwellTestMethod]),' ','') LIKE '%'+REPLACE(LOWER(@SPECIFICATION),' ','')+'%' 
    OR REPLACE(LOWER(hrd.[LEEBTestMethod]),' ','') LIKE '%'+REPLACE(LOWER(@SPECIFICATION),' ','')+'%' 
    OR REPLACE(LOWER(hrd.[TelebrinellerTestMethod]),' ','') LIKE '%'+REPLACE(LOWER(@SPECIFICATION),' ','')+'%' 
    )) 
    AND (@EQUIPMENT IS NULL OR (REPLACE(LOWER(hrd.[RockwellEquipment]),' ','') LIKE '%'+REPLACE(LOWER(@EQUIPMENT),' ','')+'%' 
    OR REPLACE(LOWER(hrd.[LEEBEquipment]),' ','') LIKE '%'+REPLACE(LOWER(@EQUIPMENT),' ','')+'%' 
    OR REPLACE(LOWER(hrd.[TelebrinellerEquipment]),' ','') LIKE '%'+REPLACE(LOWER(@EQUIPMENT),' ','')+'%' 
    )) 
) 
AND QuestionType.AnswerType not in ('Single Pick List', 'Multi-Pick List') 
AND QuestionType.AnswerType <> 'Summary' 
AND QuestionType.AnswerType <> 'Informational' 
AND Document.Active = 1 
ORDER BY Document.CreatedDateTime, QuestionType.SequenceNumber; 

ELSE IF(@TYPENAME = 'MicroHardness') 

SELECT 
    Document.Guid, 
    Document.TypeName 
    ,Document.CreatedDateTime 
    ,QuestionType.SequenceNumber 
    ,QuestionType.ValueType 
    ,QuestionType.NAME 
    ,Answer.ValueAsString 
    ,Answer.ValueAsNumber 
    ,Answer.ValueAsDateTime 
    ,Person.FamilyName 
    ,Person.GivenName 
    ,QuestionType.AnswerType 
    ,Media.Media 
FROM Document (NOLOCK) 
INNER JOIN DocumentType (NOLOCK) ON Document.DocumentTypeGuid = DocumentType.Guid 
INNER JOIN Answer (NOLOCK) ON Document.Guid = Answer.ParentDocumentGuid 
INNER JOIN QuestionType (NOLOCK) ON Answer.QuestionTypeGuid = QuestionType.Guid 
LEFT JOIN Media (NOLOCK) on Media.ParentGuid = Answer.Guid 
LEFT JOIN Person (NOLOCK) ON Document.CreatedBySystemUserGuid = Person.Guid 
WHERE 
DocumentType.BonIdentifier = @TYPENAME AND Document.Guid in 
(
    SELECT mh.[DocumentGuid] from [dbo].[MicroHardness] mh 
    WHERE 
    (@MODEL IS NULL OR REPLACE(LOWER(mh.[HardnessType]),' ','') LIKE '%'+REPLACE(LOWER(@MODEL),' ','')+'%') 
    AND (@SPECIFICATION IS NULL OR (REPLACE(LOWER(mh.[WT Test Method]),' ','') LIKE '%'+REPLACE(LOWER(@SPECIFICATION),' ','')+'%' 
    OR REPLACE(LOWER(mh.[BM Test Method]),' ','') LIKE '%'+REPLACE(LOWER(@SPECIFICATION),' ','')+'%'  
    )) 
    AND (@EQUIPMENT IS NULL OR (REPLACE(LOWER(mh.[WT Equipment]),' ','') LIKE '%'+REPLACE(LOWER(@EQUIPMENT),' ','')+'%' 
    OR REPLACE(LOWER(mh.[BM Equipment]),' ','') LIKE '%'+REPLACE(LOWER(@EQUIPMENT),' ','')+'%'  
    )) 
) 
AND QuestionType.AnswerType not in ('Single Pick List', 'Multi-Pick List') 
AND QuestionType.AnswerType <> 'Summary' 
AND QuestionType.AnswerType <> 'Informational' 
AND Document.Active = 1 
ORDER BY Document.CreatedDateTime, QuestionType.SequenceNumber; 

END 
1

我很確定@TimBiegeleisen是正確的,但沒有解釋好。我認爲這裏的問題是使用case。取而代之的case,我會簡單地使用or

(注:你可能會得到一個語法錯誤由於錯位的parenthasis,那裏有那麼多的人我有點迷路了)

SELECT 
    Document.TypeName 
    ,Document.CreatedDateTime 
    ,QuestionType.SequenceNumber 
    ,QuestionType.ValueType 
    ,QuestionType.NAME 
    ,Answer.ValueAsString 
    ,Answer.ValueAsNumber 
    ,Answer.ValueAsDateTime 
    ,Person.FamilyName 
    ,Person.GivenName 
    ,QuestionType.AnswerType 
    ,Media.Media 
FROM 
    Document (NOLOCK) 
INNER JOIN 
    DocumentType (NOLOCK) ON Document.DocumentTypeGuid = DocumentType.Guid 
INNER JOIN 
    Answer (NOLOCK) ON Document.Guid = Answer.ParentDocumentGuid 
INNER JOIN 
    QuestionType (NOLOCK) ON Answer.QuestionTypeGuid = QuestionType.Guid 
LEFT JOIN 
    Media (NOLOCK) on Media.ParentGuid = Answer.Guid 
LEFT JOIN 
    Person (NOLOCK) ON Document.CreatedBySystemUserGuid = Person.Guid 
WHERE 
    DocumentType.BonIdentifier = @TYPENAME 
    AND 
    (
    ( 
     @TYPENAME = 'Hardness' 
     AND Document.Guid IN ((SELECT hrd.[DocumentGuid] 
             FROM [dbo].[Hardness] hrd 
             WHERE (@MODEL IS NULL OR REPLACE(LOWER(hrd.[HardnessType]),' ','') LIKE '%'+REPLACE(LOWER(@MODEL),' ','')+'%') 
             AND (@SPECIFICATION IS NULL OR (REPLACE(LOWER(hrd.[RockwellTestMethod]),' ','') LIKE '%'+REPLACE(LOWER(@SPECIFICATION),' ','')+'%' 
    OR REPLACE(LOWER(hrd.[LEEBTestMethod]),' ','') LIKE '%'+REPLACE(LOWER(@SPECIFICATION),' ','')+'%' 
    OR REPLACE(LOWER(hrd.[TelebrinellerTestMethod]),' ','') LIKE '%'+REPLACE(LOWER(@SPECIFICATION),' ','')+'%' 
    )) 
    AND (@EQUIPMENT IS NULL OR (REPLACE(LOWER(hrd.[RockwellEquipment]),' ','') LIKE '%'+REPLACE(LOWER(@EQUIPMENT),' ','')+'%' 
    OR REPLACE(LOWER(hrd.[LEEBEquipment]),' ','') LIKE '%'+REPLACE(LOWER(@EQUIPMENT),' ','')+'%' 
    OR REPLACE(LOWER(hrd.[TelebrinellerEquipment]),' ','') LIKE '%'+REPLACE(LOWER(@EQUIPMENT),' ','')+'%' 
    )) 
) 
    OR 
    (
     @TYPENAME <> 'Hardness' 
     AND 

    (
    SELECT mh.[DocumentGuid] from [dbo].[MicroHardness] mh 
    WHERE 
    (@MODEL IS NULL OR REPLACE(LOWER(mh.[HardnessType]),' ','') LIKE '%'+REPLACE(LOWER(@MODEL),' ','')+'%') 
    AND (@SPECIFICATION IS NULL OR (REPLACE(LOWER(mh.[WT Test Method]),' ','') LIKE '%'+REPLACE(LOWER(@SPECIFICATION),' ','')+'%' 
    OR REPLACE(LOWER(mh.[BM Test Method]),' ','') LIKE '%'+REPLACE(LOWER(@SPECIFICATION),' ','')+'%'  
    )) 
    AND (@EQUIPMENT IS NULL OR (REPLACE(LOWER(mh.[WT Equipment]),' ','') LIKE '%'+REPLACE(LOWER(@EQUIPMENT),' ','')+'%' 
    OR REPLACE(LOWER(mh.[BM Equipment]),' ','') LIKE '%'+REPLACE(LOWER(@EQUIPMENT),' ','')+'%'  
    )) 
) 

) 
    AND QuestionType.AnswerType NOT IN ('Single Pick List', 'Multi-Pick List') 
    AND QuestionType.AnswerType <> 'Summary' 
    AND QuestionType.AnswerType <> 'Informational' 
    AND Document.Active = 1 
ORDER BY 
    Document.CreatedDateTime, QuestionType.SequenceNumber; 
相關問題