子查询在更新查询中返回了多个值

时间:2018-09-18 15:05:36

标签: sql sql-server

  

子查询返回了多个值。当子查询遵循=,!=,<,<=,>,> =或将子查询用作表达式时,不允许这样做。

当我执行此查询时,出现此错误,请帮助我

       USE [DSOG2016]
GO
/****** Object:  StoredProcedure [dbo].[wsp_GRN_Delete]    Script Date: 2018-09-19 09:12:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[wsp_GRN_Delete]

@grnid varchar(max),
@br_code varchar(max)
as
begin
declare @Doc_No nvarchar(50),@GrnQty numeric(18,3),@OrderNo nvarchar(50)
SET @Doc_No = (select Doc_No from GRN where GRNID=@grnid and Br_Code=@br_code)
SET @GrnQty = (select GRNQty from GRN_T where GRNID=@grnid and Br_Code=@br_code)
SET @OrderNo = (select OrderNo from GRN_T where GRNID=@grnid and Br_Code=@br_code)
delete from GRN where GRNID=@grnid and Br_Code=@br_code
delete from GRN_T where GRNID=@grnid and Br_Code=@br_code
delete from Inv_Trans where Doc_No=@Doc_No and Br_Code=@br_code  and Trans_Type='GRN'
update SupplierOrder set GRNNo=
                                    CASE WHEN GRNNo like '%'+@Doc_No+'%' THEN  REPLACE(GRNNo, ','+cast(@Doc_No as varchar),'')
                                    else 
                                    null
                                    END      
                     where Doc_No=@OrderNo and Br_Code=@br_code 
update SupplierOrder_T set GRNID=
                    CASE WHEN GRNID like '%'+cast(@grnid as varchar)+'%' THEN 
                                     REPLACE(GRNID, ','+cast(@grnid as varchar),'')
                                     else
                                     null
                                    END,

                           GRNQty=  CASE WHEN (GRNQty-@GrnQty) >0 THEN (GRNQty-@GrnQty)

                                         ELSE 0

                                   END

                     where Doc_No=@OrderNo and Br_Code=@br_code




end

1 个答案:

答案 0 :(得分:0)

请使用SUB QUERY,仅返回一个类似的值。

使用“选择查询前1个值”。

Declare @Doc_No nvarchar(50)

SET @Doc_No = (select Top 1 Doc_No from GRN where GRNID=@grnid and Br_Code=@br_code)


 USE [DSOG2016]
GO
/****** Object:  StoredProcedure [dbo].[wsp_GRN_Delete]    Script Date: 2018-09-19 09:12:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[wsp_GRN_Delete]

@grnid varchar(max),
@br_code varchar(max)
as
begin
declare @Doc_No nvarchar(50),@GrnQty numeric(18,3),@OrderNo nvarchar(50)
SET @Doc_No = (select TOP 1 Doc_No from GRN where GRNID=@grnid and Br_Code=@br_code)
SET @GrnQty = (select TOP 1 GRNQty from GRN_T where GRNID=@grnid and Br_Code=@br_code)
SET @OrderNo = (select TOP 1 OrderNo from GRN_T where GRNID=@grnid and Br_Code=@br_code)
delete from GRN where GRNID=@grnid and Br_Code=@br_code
delete from GRN_T where GRNID=@grnid and Br_Code=@br_code
delete from Inv_Trans where Doc_No=@Doc_No and Br_Code=@br_code  and Trans_Type='GRN'
update SupplierOrder set GRNNo=
                                    CASE WHEN GRNNo like '%'+@Doc_No+'%' THEN  REPLACE(GRNNo, ','+cast(@Doc_No as varchar),'')
                                    else 
                                    null
                                    END      
                     where Doc_No=@OrderNo and Br_Code=@br_code 
update SupplierOrder_T set GRNID=
                    CASE WHEN GRNID like '%'+cast(@grnid as varchar)+'%' THEN 
                                     REPLACE(GRNID, ','+cast(@grnid as varchar),'')
                                     else
                                     null
                                    END,

                           GRNQty=  CASE WHEN (GRNQty-@GrnQty) >0 THEN (GRNQty-@GrnQty)

                                         ELSE 0

                                   END

                     where Doc_No=@OrderNo and Br_Code=@br_code




end