如何正确指定条件:总计数=列值?

时间:2018-07-08 04:17:14

标签: sql-server tsql sql-server-2012 triggers

我正在更新系统,尝试创建原始表的“影子”表,通过触发器接收更改,但不使用原始表,因为我真的不喜欢原始程序的设计方式。

其中之一是团队配置表,其中包含所有用户及其团队ID。具有相同团队ID的用户属于同一团队。我正在尝试在此处创建触发器,以将数据拦截到新的团队历史记录日志中。 (1行/ 1个团队ID) 但是,由于在进行团队设置后,表的多行会更新,并且更新未批量进行,因此很难确定新的团队设置何时结束。幸运的是,有一个member_count列,可用于对成员进行计数并确定何时更新与该新team_id相关的所有nencessry记录。

| team id     | uid | member_count |
|201701010800A| 1   | 3            |
|201701010800A| 2   | 3            |
|201701010800A| 3   | 3            |

我的问题是我应如何在更新后触发器中指定此条件。 这是我现在要做的。但这似乎很hacky。我想还有更多明智的表达方式。

if exists(
    select 1 from ( values (1) ) t(c) 
    outer apply (
        select count(team_id) tc, max(member_count) mx, min(member_count) mm
        from old_teamtable 
        where team_id = ( select top 1 team_id from inserted )
    ) e
    where tc = mx and mx = mm
)

2 个答案:

答案 0 :(得分:1)

这对您有用吗?

if exists (
    select  1 
    from    old_teamtable 
    where   team_id = (select top 1 team_id from inserted) 
    having  count(*) = max(member_count) 
    and     max(member_count) = min(member_count)
)

答案 1 :(得分:0)

以下代码演示了一种基于对团队和用户表的插入来管理团队历史表的方法。在每个insertTeamsAndUsers之后触发触发器。对于尚未加入TeamHistory的新团队,将添加适当的行并标记为IsComplete = 0。然后,通过检查受TeamHistory影响的所有团队来查看insert的用户数量是否正确。如果是这样,则IsComplete表中相应行的1位设置为TeamHistory

-- Create sample tables.
create table TeamsAndUsers ( Id Int Identity, TeamId Int, UserId Int, TeamMembers Int );

create table TeamHistory ( Id Int Identity, TeamId Int, IsComplete Bit );
go

-- Create the trigger.
create trigger UpdateTeamHistory
  on TeamsAndUsers
  after Insert
as
  set nocount on

  -- Handle any teams we've not heard of before.
  insert into TeamHistory ( TeamId, IsComplete )
    select distinct TeamId, 0
      from inserted
      where TeamId not in ( select TeamId from TeamHistory );

  -- See if any teams have been completed, i.e. all of their members are present.
  update TeamHistory
    set IsComplete = 1
    where TeamId in (
      select TAU.TeamId from (
        -- TeamId   values touched by the INSERT ...
        select distinct TeamId from inserted ) as i inner join
          -- ... matched with corresponding incomplete teams in the team history ...
          TeamHistory as TH on TH.TeamId = i.TeamId and TH.IsComplete = 0 inner join
          -- ... matched with users currently assigned to the teams.
          TeamsAndUsers as TAU on TAU.TeamId = i.TeamId
    group by TAU.TeamId
    having Count( TAU.UserId ) = Max( TAU.TeamMembers ) );
go

-- Test the trigger.
insert into TeamsAndUsers ( TeamId, UserId, TeamMembers ) values
  ( 101, 1, 3 ), ( 101, 2, 3 );
select * from TeamsAndUsers;
select * from TeamHistory;

insert into TeamsAndUsers ( TeamId, UserId, TeamMembers ) values
  ( 101, 3, 3 );
select * from TeamsAndUsers;
select * from TeamHistory;

insert into TeamsAndUsers ( TeamId, UserId, TeamMembers ) values
  ( 102, 4, 1 );
select * from TeamsAndUsers;
select * from TeamHistory;

insert into TeamsAndUsers ( TeamId, UserId, TeamMembers ) values
  ( 103, 10, 2 );
select * from TeamsAndUsers;
select * from TeamHistory;

insert into TeamsAndUsers ( TeamId, UserId, TeamMembers ) values
  ( 103, 11, 2 );
select * from TeamsAndUsers;
select * from TeamHistory;

在旁边:select中的update看起来很笨拙,但是来晚了。可能可以整理一下。