1

当我执行子查询时,一切正常,现在我使用相同的查询来更新基于子查询结果的列,但它说子查询返回了超过 1 行,这是有道理的。我应该如何解决这个问题..

begin transaction
update trn_RatingAuto                                                        
set Rate  = 0 
where rate = (
SELECT ar.Rate
FROM trn_account ta
INNER JOIN trn_risk tr  ON ta.AccountId=tr.AccountId
inner join trn_option ot on tr.riskid = ot.riskid
INNER JOIN trn_Rating ra ON ot.RatingId = ra.RatingId
INNER JOIN trn_RatingAuto ar on ra.RatingId = ar.RatingId
where  ar.Rate is null 
)
4

4 回答 4

2

您需要使用IN而不是=.

update trn_RatingAuto
set Rate  = 0 
where rate IN (
  SELECT ar.Rate
  FROM trn_account ta
  INNER JOIN trn_risk tr  ON ta.AccountId=tr.AccountId
  inner join trn_option ot on tr.riskid = ot.riskid
  INNER JOIN trn_Rating ra ON ot.RatingId = ra.RatingId
  INNER JOIN trn_RatingAuto ar on ra.RatingId = ar.RatingId
  where  ar.Rate is null 
)

编辑:

您也可以通过以下方式进行更新:

update ar
set Rate  = 0 
FROM trn_account ta
INNER JOIN trn_risk tr  ON ta.AccountId=tr.AccountId
inner join trn_option ot on tr.riskid = ot.riskid
INNER JOIN trn_Rating ra ON ot.RatingId = ra.RatingId
INNER JOIN trn_RatingAuto ar on ra.RatingId = ar.RatingId
where  ar.Rate is null
于 2012-10-24T17:53:04.843 回答
0

我在SQLServer2008R2上写了这个脚本,也许对你有帮助

UPDATE x
SET x.Rate  = 0 
FROM (
      SELECT ar.Rate
      FROM trn_account ta INNER JOIN trn_risk tr  ON ta.AccountId=tr.AccountId
                          INNER JOIN trn_option ot ON tr.riskid = ot.riskid
                          INNER JOIN trn_Rating ra ON ot.RatingId = ra.RatingId
                          INNER JOIN trn_RatingAuto ar ON ra.RatingId = ar.RatingId
      WHERE  ar.Rate is null
      ) x
于 2012-10-25T07:38:39.193 回答
0

将您的从哪里更改=in

begin transaction
update trn_RatingAuto                                                        
set Rate  = 0 
where rate in (
SELECT ar.Rate
FROM trn_account ta
INNER JOIN trn_risk tr  ON ta.AccountId=tr.AccountId
inner join trn_option ot on tr.riskid = ot.riskid
INNER JOIN trn_Rating ra ON ot.RatingId = ra.RatingId
INNER JOIN trn_RatingAuto ar on ra.RatingId = ar.RatingId
where  ar.Rate is null 
)
于 2012-10-24T17:52:29.663 回答
0

如果要更新多行,则需要将查询更改为“更新自”查询...

sql server 2005“更新自”查询

喜欢:

 update trn_RatingAuto
   set Rate  = 0  
 from trn_RatingAuto
 join (...
于 2012-10-24T17:55:44.837 回答