create table tb(产品 varchar(2), 数量 int, 日期 varchar(4), 单据号 varchar(4))
insert into tb
select 'a',10,'9.1','001' union all
select 'a',3,'9.2','002' union all
select 'a',4,'9.3','003' union all
select 'a',2,'9.4','004'
select * from tb
declare @num int,@t int
set @num=14
set @t=14
/*
update tb set 数量=case when 数量>=@num then 数量-@num else 0 end,@num=@num-@t,
@t=case when 数量>=@num then @num else 数量 end
*/
update tb set 数量=case when 数量>=@num then 数量-@num else 0 end,@num=@t,
@t=case when 数量>=@num then 0 else @num-数量 end
--update tb set 数量=数量+2,日期=cast((数量+3) as varchar(4)) 为什么给变量赋值时,先计算变量值再计算字段值.
select * from tb where 数量>0
drop table tb
update set 语句中当有给变量赋值时,它的赋值顺序将不按照顺序进行,而是先给变量赋值.再按顺序赋值.
insert into tb
select 'a',10,'9.1','001' union all
select 'a',3,'9.2','002' union all
select 'a',4,'9.3','003' union all
select 'a',2,'9.4','004'
select * from tb
declare @num int,@t int
set @num=14
set @t=14
/*
update tb set 数量=case when 数量>=@num then 数量-@num else 0 end,@num=@num-@t,
@t=case when 数量>=@num then @num else 数量 end
*/
update tb set 数量=case when 数量>=@num then 数量-@num else 0 end,@num=@t,
@t=case when 数量>=@num then 0 else @num-数量 end
--update tb set 数量=数量+2,日期=cast((数量+3) as varchar(4)) 为什么给变量赋值时,先计算变量值再计算字段值.
select * from tb where 数量>0
drop table tb
update set 语句中当有给变量赋值时,它的赋值顺序将不按照顺序进行,而是先给变量赋值.再按顺序赋值.