2016-01-06 50 views
-6

要在ADO.Net中運行的SQL命令字符串。它可能在子查詢中返回了多於一行的問題。子查詢在sqlc中返回了超過1個值#

string str = " 
select 
(select (quantity) from Orderinfo where iron=1 and rno=o.rno) as iron 
,(select (quantity) from Orderinfo where wash=1 and rno=o.rno) as wash 
,(select (quantity) from Orderinfo where dryclean=1 and rno=o.rno) as dryclean 
,o.rno 
,o.id 
,o.name 
,(select sum(convert(int,d.quantity)) from orderinfo as d where d.rno=o.rno) as quantity 
,o.status 
from orderinfo as o 
where o.status='Order in Process' 
group by o.rno,o.id ,o.status ,o.name"; 
+0

改爲使用select top 1 .......。 –

+2

請小心處理您的SQL查詢,它們應該像應用程序代碼一樣可讀,可維護且清晰。 – Arran

回答

0

如果我正確理解你想要的結果,你需要得到量的總和:

string str = " 
select 
(select sum(quantity) from Orderinfo where iron=1 and rno=o.rno) as iron 
,(select sum(quantity) from Orderinfo where wash=1 and rno=o.rno) as wash 
,(select sum(quantity) from Orderinfo where dryclean=1 and rno=o.rno) as dryclean 
,o.rno 
,o.id 
,o.name 
,(select sum(convert(int,d.quantity)) from orderinfo as d where d.rno=o.rno) as quantity 
,o.status 
from orderinfo as o 
where o.status='Order in Process' 
group by o.rno,o.id ,o.status ,o.name"; 

順便說一句,我假設它是正確的,你想整個的量「o.rno 「獨立於具體的」o.id「。否則,子查詢不需要。

相關問題