update a set 体积=体积-(select sum(体积) from TB2 where 单号=a.单号) from TB1 a
2update tb1 set 体积=tb1.体积-tb2.体积 from tb1 join tb2 on tb1.单号=tb2.单号
declare @TB1 table (单号 varchar(13),体积 int) insert into @TB1 select 'D110827004668',50declare @TB2 table (单号 varchar(13),体积 int) insert into @TB2 select 'D110827004668',10update @TB1 set 体积=a.体积-b.体积 from @TB1 a left join @TB2 b on a.单号=b.单号select * from @TB1 /* 单号 体积 ------------- ----------- D110827004668 40 */
生成一张出库记录表 这个按需求应该是先建好这个log表,然后insert ... select ... from ... 而不是生成一张表
declare @TB2 table (单号 varchar(13),体积 int)
insert into @TB2
select 'D110827004668',10select 单号=单号
+'C'+right(convert(varchar(10),getdate(),108),2),体积 from @TB2
/*
单号 体积
------------------ -----------
D110827004668C44 10
*/
from TB1 a
declare @TB1 table (单号 varchar(13),体积 int)
insert into @TB1
select 'D110827004668',50declare @TB2 table (单号 varchar(13),体积 int)
insert into @TB2
select 'D110827004668',10update @TB1 set 体积=a.体积-b.体积
from @TB1 a left join @TB2 b on a.单号=b.单号select * from @TB1
/*
单号 体积
------------- -----------
D110827004668 40
*/
而不是生成一张表