declare @余额 int
set @余额=0
update 表 set @余额=@余额+isnull(借方,0)-isnull(贷方,0),余额=@余额

解决方案 »

  1.   

    --测试--测试数据
    create table 表(借方 int,贷方 int,余额 int)
    insert 表 select 200,100,null
    union all select 50,100,null
    union all select 50,100,null
    go--更新
    declare @余额 int
    set @余额=0
    update 表 set @余额=@余额+isnull(借方,0)-isnull(贷方,0),余额=@余额--显示更新结果
    select * from 表
    go--删除测试
    drop table 表/*--测试结果借方          贷方          余额          
    ----------- ----------- ----------- 
    200         100         100
    50          100         50
    50          100         0(所影响的行数为 3 行)
    --*/