我收到錯誤訊息:
子查詢回傳超過 1 個值。當子查詢跟隨 =,!=,<,<=,>,=> 或子查詢用作運算式時,這是不允許的。
下面是一個較大查詢的一小部分,但確定Test_Col值的查詢部分基本上是我遇到這個問題的地方。查詢本身有效,但是當我將其用作較大查詢中的子查詢時,我在 SQL Server 中收到此錯誤訊息。關于我哪里出錯的任何想法?
select
distinct(nml.scode) Loan_Num,
(select isnull(sum(isnull(t.smtd, 0) isnull(t.sbeginbudget, 0)), 0)
from nmloan nml
left join property p on nml.hprop = p.hmy
left join total t on p.hmy = t.hppty
where nml.hprop in (2380, 3348)
and t.umonth between '1/1/1900' and '9/30/2021'
and t.ibook = 1 and t.hacct in (1349, 1348, 1347, 1345, 1343, 1342, 1341, 1339, 1338, 1337, 1336, 1334, 1332, 1690, 1682, 1331)
group by nml.hprop) Test_Col
from
nmloan nml
left join
property p on nml.hprop = p.hmy
left join
total t on p.hmy = t.hppty
left join
acct ac on ac.hmy = t.hacct
left join
nmborrower nmb on nml.hmy = nmb.hnmloan
left join
person ps on nmb.hmyperson = ps.hmy
left join
nmloanterms nmt on nml.hmy = nmt.hloan
left join
nmcollateralxref nmx on nml.hmy = nmx.hnmloan
left join
nmcollateral nmc on nmx.hnmcollateral = nmc.hmy
left join
loanbut1 lb1 on nml.hmy = lb1.hcode
left join
NMLedger l ON nml.hmy = l.hNMLoan
left join
nmLedgerDetail d on l.hmy = d.hNMLedger
left join
loanbut7 lb on nml.hmy = lb.hcode
left join
loanbut8 lb8 on nml.hmy = lb8.hcode
left join
loanbut9 lb9 on nml.hmy = lb9.hcode
where
nml.hprop in (2380, 3348)
and lb.lrPeriod in ('9/30/2021')
and lb9.lrnDate in ('9/30/2021')
group by
nml.hprop, nml.scode
uj5u.com熱心網友回復:
在 SQL Server DB 中,如果您的子查詢是select在我們撰寫欄位名稱串列的命令之后撰寫的,那么您的子查詢必須只回傳一條記錄和一個欄位,否則會出錯。在您的腳本中,您在from命令之前撰寫了子查詢,在 this 之后Loan_Num,。我對您的子查詢做了一些研究。在大多數情況下,您的子查詢將回傳超過 1 條記錄。原因是您group by nml.hprop在where命令之后撰寫了此條件nml.hprop in (2380, 3348)。我會自己為您撰寫此查詢,但我不知道您的業務邏輯以及您需要什么。如果您的子查詢必須回傳多于 1 條記錄,那么您必須將此子查詢連接到主查詢,使用inner joinor left join,您不能在欄位串列中寫入此子查詢。
uj5u.com熱心網友回復:
事實證明,由于我的子查詢的別名 nml 與父查詢 nml(對于 nmLoan 表)具有相同的別名,因此它不起作用。
在將我的子查詢的別名更改為 nl 并將父查詢的別名保留為 nml 后,這確實有效,并且我能夠生成多個結果。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/407801.html
標籤:
上一篇:在T-SQL中即時轉義單引號
