获取错误:
子查询返回的值超过1。当子查询遵循=,!=,<,< =,>,> =或子查询用作表达式时,不允许这样做。
请帮我解决这个疑问:
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
答案 0 :(得分:1)
我很确定@TimBiegeleisen是正确的,但只是没有解释那么好。我认为这里的问题是使用case
。我只使用case
:
or
(注意:由于父项重点错位,您可能会收到语法错误,其中有很多我丢失了)
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;
答案 1 :(得分:0)
您的查询工作正常@Zohar Peled但它降低了性能,因此我自己找到了解决方案,通过下面的条件查找将整个查询分开,
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