四處錯誤:子查詢返回不止一個值,不允許變量是=,=,<, <= , >,> =
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
該問題可能是在'CASE'表達式,其中子查詢返回不止一個值。你需要修正那裏的邏輯。 –
我不認爲@TimBiegeleisen是對的。內部選擇預計會返回一個或多個值(請參閱條件'... AND Document.Guid in ...')。我建議你重新安排你的內部查詢到一個更可讀的佈局和檢查括號。 – FDavidov
設置[壞習慣踢 - 把NOLOCK無處不在](http://blogs.sqlsentry.com/aaronbertrand/bad-habits-nolock-everywhere/) - 它是*不推薦*到處使用 - 完全相反! –