【问题标题】:Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery子查询返回超过 1 个值。当子查询遵循 =、!=、<、<=、>、>= 或子查询时,这是不允许的
【发布时间】:2014-04-07 01:25:24
【问题描述】:
declare @ProductDetails as table(ProductName nvarchar(200),ProductDescription nvarchar(200),
Brand nvarchar(200),
Categry  nvarchar(200),
Grop nvarchar(200),
MRP decimal,SalesRate decimal,CurrentQuantity decimal,AvailableQty decimal)

declare @AvailableQty table(prcode nvarchar(100),Aqty decimal)

declare @CloseStock table(pcode nvarchar(100),
Cqty decimal)

insert into @CloseStock
select PCODE ,
0.0
from producttable

insert into @AvailableQty
select PCODE ,
0.0
from producttable

--Current Qty
--OpenQty
update @CloseStock set Cqty=((OOQTY+QTY+SRRQTY+PYQTY)-(STQTY+PRRQTY))
from
(
select PC.PCODE as PRODUCTCODE,
--Opening
(select case when SUM(PU.Quantity)is null then 0 else SUM(PU.Quantity) end as Q from ProductOpeningYearEnd PU
where PC.PCODE=PU.ProductName) as OOQTY,
--Purchase
(select case when SUM(PU.quantity)is null then 0 else SUM(PU.quantity) end as Q from purchase PU
where PC.PCODE=PU.prdcode ) as QTY,
--Sales
(select case when SUM(ST.QUANTITY)is null then 0 else SUM(ST.QUANTITY)end as Q2 from salestable ST
where PC.PCODE=ST.PRODUCTCODE and ST.status!='cancel' )as STQTY,
--Physical Stock
(select case when SUM(PS.Adjustment)is null then 0 else SUM(PS.Adjustment)end as Q3 from physicalstock PS
where PC.PCODE=PS.PCODE )as PYQTY,
--Sales Return
(select case when SUM(SR.quantity)is null then 0 else SUM(SR.quantity)end as Q3 from salesreturn SR
where PC.PCODE=SR.prdcode )as SRRQTY,
--Purchase Return
(select case when SUM(PR.quantity)is null then 0 else SUM(PR.quantity)end as Q3 from purchasereturn PR
where PC.PCODE=PR.prdcode )as PRRQTY

from producttable PC
group by PC.PCODE
)t
where PCODE=t.PRODUCTCODE

--Available
update @AvailableQty set Aqty=((CCqty-GIQty)+(GOQty))
--((OOQTY+QTY+SRRQTY+PYQTY)-(STQTY+PRRQTY))
from
(
select PC.PCODE as PRODUCTCODE,
--GoodsIn
(select case when SUM(GI.quantity)is null then 0 else SUM(GI.quantity) end as Q from goodsin GI
where PC.PCODE=GI.productcode) as GIQty,
--GoodsOut
(select case when SUM(GUT.quantity)is null then 0 else SUM(GUT.quantity) end as Q from goodsout GUT
where PC.PCODE=GUT.productcode ) as GOQty,
--Current Stock
(select CS.Cqty as Q from @CloseStock CS 
where PC.PCODE=CS.pcode ) as CCqty
from producttable PC
group by PC.PCODE
)t
where prcode=t.PRODUCTCODE

insert into @ProductDetails
select PCODE,[DESCRIPTION],BRAND,CATEGORY,DEPARTMENT,MRP,SALERATE,0,0
from producttable

update @ProductDetails set CurrentQuantity=pcqty,AvailableQty=acqty
from
(
select pt.ProductName as pn,cs.Cqty as pcqty,ac.Aqty as acqty from @ProductDetails pt
inner join @CloseStock cs on pt.ProductName=cs.pcode
inner join @AvailableQty ac on pt.ProductName=ac.prcode     
)t
where ProductName=t.pn

select * from @ProductDetails

end

这在 pcode 字段中添加 ant (-.&) 时不起作用,我什至想在 pcode 字段中允许这种符号, 请帮助我如何在查询中允许任何符号

(此代码有问题) 更新@AvailableQty 设置Aqty=((CCqty-GIQty)+(GOQty)) 从 ( 选择 PC.PCODE 作为 PRODUCTCODE, --GoodsIn (选择 SUM(GI.quantity) 为 null 然后为 0 的情况,否则 SUM(GI.quantity) 以 G 中的货物 Q 结尾 其中 PC.PCODE=GI.productcode) 作为 GIQty, --GoodsOut (选择 SUM(GUT.quantity) 为 null 然后为 0 的情况,否则 SUM(GUT.quantity) 以来自货物 GUT 的 Q 结尾 其中 PC.PCODE=GUT.productcode ) 为 GOQty, --当前库存 (从@CloseStock CS 中选择 CS.Cqty 作为 Q 其中 PC.PCODE=CS.pcode ) 为 CCqty 从产品table PC 按 PC.PCODE 分组 )t 其中 prcode=t.PRODUCTCODE

【问题讨论】:

  • 我不知道您为什么会收到该消息,但此 Cqty=((OOQTY+QTY+SRRQTY+PYQTY)-(STQTY+PRRQTY)) 不起作用。您正在引用 INLINE VIEW 别名,就好像它们是字段一样。这些字段在任何情况下都无法寻址,因为您没有在内联视图 t 中选择它们。

标签: stored-procedures


【解决方案1】:

这里的问题不在于您使用的符号,而在于您将子查询的值分配给结果集中的单个列。例如:

(select case when SUM(PR.quantity)is null then 0 else SUM(PR.quantity)end as Q3 from purchasereturn PR
where PC.PCODE=PR.prdcode )as PRRQTY

请注意,仅当子查询仅返回单个值时才允许这样做;否则,我们不知道应该为列分配哪些值。

如果您希望您的子查询返回多个值,而您只想要一个任意值,请在子查询中使用 TOP 1 以仅返回 1 个值。否则,您必须调试每个子查询以找出返回多个结果并导致问题的原因。

【讨论】:

  • 值问题只是 (-.,) 这个符号有问题子查询返回显示错误的字符。在此查询中还希望允许这些符号。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-04
  • 2016-08-02
  • 2019-10-13
  • 1970-01-01
相关资源
最近更新 更多