我正在执行一个返回错误的程序:
子查询返回的值超过1。这是不允许的 子查询跟随=,!=,<,< =,>,> =或当子查询用作 表达。
我知道因为某些字段预计会返回超过1个值,但我需要处理它,我正在处理Debit,将减法结果归功于CR-DR等
(
Conductor_ID
)
Set @AccID= (Select Account_ID FROM Transport.Conductors WHERE ConductorID in (@ConductorID))
Set @AccCredit= (Select CR from [dbo].[Transactions] where Account_ID in (@AccID))
Set @AccDebit= (Select DR from [dbo].[Transactions] where Account_ID in (@AccID))
Set @Total= @AccCredit-@AccDebit
答案 0 :(得分:0)
试 使用max():它将选择最大值,因此不会有任何多行选择冲突。
Set @AccID= (Select MAX(Account_ID) FROM Transport.Conductors WHERE ConductorID in (@ConductorID))
Set @AccCredit= (Select MAX(CR) from [dbo].[Transactions] where Account_ID in (@AccID))
Set @AccDebit= (Select MAX(DR) from [dbo].[Transactions] where Account_ID in (@AccID))
Set @Total= @AccCredit-@AccDebit
答案 1 :(得分:0)
如果没有错,这就是你需要的
SELECT Account_ID,
CR,
DR,
Total = CR - DR
FROM [dbo].[Transactions] T
JOIN Transport.Conductors C
ON T.Account_ID = C.Account_ID
WHERE C.ConductorID IN ( @ConductorID )