create table wl(bookname varchar(8),company varchar(8),account varchar(8),bp varchar(8),amount_in int,amount_out int,status varchar(8)) 
insert into wl values('宁波','A','张三','01', 100,null,'right')
insert into wl values('宁波','A','张三','01',null, 100,'right')
insert into wl values('宁波','A','张三','02', 200,null,null)
insert into wl values('宁波','A','张三','02',null, 100,null)
insert into wl values('宁波','A','张三','03',null, 100,null)
goCREATE TRIGGER wl_gl ON wl  
FOR UPDATE 
AS 
if update(bp) 
begin 
   update aa 
   set 
       status= 'right ' 
   from 
       wl aa,inserted bb
   where 
       aa.bp=bb.bp
       and
       (select sum(isnull(amount_in,0)-isnull(amount_out,0)) from wl where bp=aa.bp)=0
       and
       aa.status is null
end
goupdate wl set bp='02' where bp='03'
goselect * from wl
/*
bookname company  account  bp       amount_in   amount_out  status   
-------- -------- -------- -------- ----------- ----------- -------- 
宁波       A        张三       01       100         NULL        right
宁波       A        张三       01       NULL        100         right
宁波       A        张三       02       200         NULL        right 
宁波       A        张三       02       NULL        100         right 
宁波       A        张三       02       NULL        100         right 
*/
godrop trigger wl_gl
drop table wl