子查询返回的值超过1。子查询遵循=,!=,<,< =,>,> =时不允许这样做

时间:2015-06-23 01:40:46

标签: sql

我需要知道下面存储过程的问题在哪里。它将返回错误" Subquery返回的值超过1。当子查询遵循=,!=,<,< =,>,> =或子查询用作表达式时,不允许这样做"

CREATE PROCEDURE [dbo].[SP_APP_FP_Complete_Details]
    @FinishProductId int
AS
BEGIN

    SELECT 
    userID,
    productTariffCode,
    tariffDesc,
    finishProductID,
    productDesc,
    tariffOfMeasurement,
    importCountry,
    productionCapacity,
    brandProduct,
    Manufacturerbrn,
    Manufacturername,
    Manfactoryaddress,
    Manfactorypostcode,
    Manfactorycity, 
    Manfactorystate

    (select [FileName] from [dbo].[APP_FinishProductCompleteForm_Attachment] where [finishProductID]=@FinishProductId and FileType='3' ) FileName3,
    (select [FileName] from [dbo].[APP_FinishProductCompleteForm_Attachment] where [finishProductID]=@FinishProductId and FileType='4' ) FileName4,
    (select [FileName] from [dbo].[APP_FinishProductCompleteForm_Attachment] where [finishProductID]=@FinishProductId and FileType='5' ) FileName5,
    (select [FileName] from [dbo].[APP_FinishProductCompleteForm_Attachment] where [finishProductID]=@FinishProductId and FileType='6' ) FileName6,
    (select [FileName] from [dbo].[APP_FinishProductCompleteForm_Attachment] where [finishProductID]=@FinishProductId and FileType='7' ) FileName7,
    (select finishProductID from [dbo].APP_Raw_Material where [finishProductID]=@FinishProductId ) FinishProductIDRM


    from [dbo].[APP_FinishProductCompleteForm]
    where  finishProductID=@FinishProductId and IsDeleted = 0    

END

1 个答案:

答案 0 :(得分:0)

您显然使用的是SQL Server语法,因此请使用TOP 1

   (select  TOP 1 [FileName] from [dbo].[APP_FinishProductCompleteForm_Attachment] where [finishProductID]=@FinishProductId and FileType='3' ) FileName3,
    (select TOP 1 [FileName] from [dbo].[APP_FinishProductCompleteForm_Attachment] where [finishProductID]=@FinishProductId and FileType='4' ) FileName4,
    (select TOP 1 [FileName] from [dbo].[APP_FinishProductCompleteForm_Attachment] where [finishProductID]=@FinishProductId and FileType='5' ) FileName5,
    (select TOP 1 [FileName] from [dbo].[APP_FinishProductCompleteForm_Attachment] where [finishProductID]=@FinishProductId and FileType='6' ) FileName6,
    (select TOP 1 [FileName] from [dbo].[APP_FinishProductCompleteForm_Attachment] where [finishProductID]=@FinishProductId and FileType='7' ) FileName7,
    (select TOP 1 finishProductID from [dbo].APP_Raw_Material where [finishProductID]=@FinishProductId ) FinishProductIDRM

顺便说一下,您可以在from子句中执行前7个。这可能更有效:

from [dbo].[APP_FinishProductCompleteForm] cf left join
     (select finishProductID,
             max(case when FileType = 1 then FileName end) as FileName1,
             max(case when FileType = 2 then FileName end) as FileName2,
             max(case when FileType = 3 then FileName end) as FileName3,
             max(case when FileType = 4 then FileName end) as FileName4,
             max(case when FileType = 5 then FileName end) as FileName5,
             max(case when FileType = 6 then FileName end) as FileName6,
             max(case when FileType = 7 then FileName end) as FileName7
      from dbo.APP_FinishProductCompleteForm_Attachment
      where finishProductID] = @FinishProductId
      group by finishProductID
     ) fp
     on fp.finishProductID = cf.finishProductID

此外,您的上一个子查询:

(select finishProductID
 from [dbo].APP_Raw_Material
 where [finishProductID]=@FinishProductId
) as FinishProductIDRM

可能是导致问题的那个,并且有点误导。如何使用exists呢?

(case when exists (select 1
                   from APP_Raw_Material
                   where finishProductID = @FinishProductId
                  )
      then @FinishProductId
 end) as FinishProductIDRM