SQL Update期间“子查询返回的值超过1”错误

时间:2015-09-14 18:08:07

标签: sql sql-server

我有一个这样的历史表:

PersonId    FromDate    EntityId
1           2011-08-01  
1           2012-03-02 
1           2014-02-15
2           2015-03-02
3           2012-01-01
3           2012-10-05

我也有以下表格。

家庭

PersonId   AccountId  FamId   FromDate    ToDate      
1          500        A01
1          200        C01
2          400        B01           
3          600        A01                  2012-10-04
3          700        A01     2012-10-05

Missacct

AccountId   SendingEntityFieldId
500         24
200         64
400         24
600         70
700         24

以下查询返回记录:

select f.personid
   from family as f
   where f.famid not like 'C%'
     and (f.todate is null or f.todate >= getdate())
   group by f.personid
   having count(personid) > 1

换句话说,没有人一次与多个帐户挂钩。此查询也不返回任何结果:

with paccounts as (select f.personid, m.SendingEntityFieldId
   from Family as f
   join Missacct as m on m.AccountId = f.AccountId
   where (f.Todate is null or f.Todate >= GETDATE())
     and f.FamId not like 'C%')
 select personid from paccounts group by PersonId having COUNT(personid) > 1

同样,这里的想法是每人不要超过1行。

但是,当我尝试使用以下SQL更新更新历史记录表中的EntityId时,出现错误Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

update PersonHistory set SendingEntityId = 
  (select ma.SendingEntityFieldId 
   from Family as f 
   join Missacct as ma on f.AccountId = ma.AccountId 
   where f.FamId not like 'C%' 
   and (f.Todate is null or f.Todate >= GETDATE()) and f.PersonId = PersonId)

似乎我应该在历史记录表中的每条记录与子查询中的相关记录之间获得1比1的匹配。我不明白,当我已经通过先前的查询证明我在每个人的子查询中没有超过1行时,我可以获得超过1的值。

(顺便说一句,我也尝试了以下更新并返回了相同的错误)

with paccounts as (select f.personid, m.SendingEntityFieldId
 from Family as f
 join Missacct as m on m.AccountId = f.AccountId
 where (f.Todate is null or f.Todate >= GETDATE())
 and f.FamId not like 'C%')
update PersonHistory set SendingEntityId =  
  (select pa.SendingEntityFieldId 
   from paccounts as pa where pa.PersonId = PersonId)

我错过了什么吗?

更新

我使用连接而不是子查询尝试了以下更新:

update PersonHistory
  set SendingEntityId = ma.SendingEntityFieldId
  from PersonHistory as ph
  join Family as f on ph.PersonId = f.PersonId and f.FamId not like 'C%' and  (f.Todate is null or f.Todate >= GETDATE())
  join Missacct as ma on f.AccountId = ma.AccountId

更新2

我尝试了以下查询并获取了每条PersonH​​istory记录的记录,并且每个记录都在tot_finds列中有1。我不知道比较苹果和苹果吗?

select ph.personid, (select COUNT(*) from Family where PersonId = ph.personid 
and FamId not like 'C%' and (Todate is null or Todate >= GETDATE())) as tot_finds
from PersonHistory as ph

2 个答案:

答案 0 :(得分:1)

我会看这个:

update P 
set SendingEntityId =  ma.SendingEntityFieldId 
--select  p.SendingEntityId,  ma.SendingEntityFieldId, *
   from PersonHistory  p
  JOIN  Family as f  on f.PersonId = P.PersonId
   join Missacct as ma on f.AccountId = ma.AccountId 
   where f.FamId not like 'C%' 
   and (f.Todate is null or f.Todate >= GETDATE()) 

当您运行注释中嵌入的选择时,您可能会看到记录问题是什么,然后您可以根据需要正确修复更新。连接完全有可能为您提供具有相同personid和SendingEntityFieldID的多个记录,在这种情况下,子查询将不起作用,但是连接将会或者您可能需要添加where子句以获得完全正确的记录。

答案 1 :(得分:0)

事实证明我得到的错误是PersonH​​istory表上的一个活动触发器的结果。