Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何使用子查询更新多行_Sql_Sql Server - Fatal编程技术网

Sql 如何使用子查询更新多行

Sql 如何使用子查询更新多行,sql,sql-server,Sql,Sql Server,当我执行子查询时,一切正常,现在我使用相同的查询根据子查询结果更新列,但它说子查询返回的行数超过1行,这是有意义的。我应该如何解决此问题 begin transaction update trn_RatingAuto set Rate = 0 where rate = ( SELECT ar.Rate FROM trn_account ta INNER JOIN trn_risk

当我执行子查询时,一切正常,现在我使用相同的查询根据子查询结果更新列,但它说子查询返回的行数超过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 
)

将您的位置从
=
更改为
中的

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 
)

您需要在
中使用
,而不是
=

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

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

比如:


我在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
查询返回0行,我之前尝试过,但失败了。谢谢!
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