很经典的“先进先出”的库存处理的SQL语句。 CREATE TABLE #tmp ( ID int IDENTITY (1, 1), 单价 decimal(18, 2) NOT NULL , 数量 decimal(18, 2) NOT NULL , 已出数量 decimal(18, 2) NOT NULL ) insert into #tmp(单价,数量,已出数量) values(1.1,50,0) insert into #tmp(单价,数量,已出数量) values(1.3,30,0) insert into #tmp(单价,数量,已出数量) values(1.4,60,0) insert into #tmp(单价,数量,已出数量) values(1.5,20,0) declare @t decimal(18, 2),@temp decimal(18, 2) set @t=90update #tmp set @temp=case when @t>数量 then 数量 else @t end,数量=数量-@temp,@t=@t-@temp from #tmp where 单价<=1.30 select * from #tmp go drop table #tmp CREATE TABLE #tmp ( ID int IDENTITY (1, 1), 单价 decimal(18, 2) NOT NULL , 数量 decimal(18, 2) NOT NULL , 已出数量 decimal(18, 2) NOT NULL ) insert into #tmp(单价,数量,已出数量) values(1.1,50,0) insert into #tmp(单价,数量,已出数量) values(1.3,30,0) insert into #tmp(单价,数量,已出数量) values(1.4,60,0) insert into #tmp(单价,数量,已出数量) values(1.5,20,0) declare @t decimal(18, 2),@temp decimal(18, 2),@a varchar(8000) select @t=60,@a=''update #tmp set @temp=case when @t>数量-已出数量 then 数量-已出数量 else @t end, @t=@t-@temp,@a=@a+','+cast(@temp as varchar(10)),已出数量=@temp+已出数量 where 已出数量<>数量 select * from #tmpset @t=30update #tmp set @temp=case when @t>数量-已出数量 then 数量-已出数量 else @t end,@t=@t-@temp,@a=@a+','+cast(@temp as varchar(10)),已出数量=@temp+已出数量 where 已出数量<>数量 select * from #tmpselect right(@a,len(@a)-1) go drop table #tmp
ID号1的记录,一个表是数量为10,结果出来是0,而日期都是2009-2-5,这个不是有点问题吗?
结果中的ID为1的日期应该是2009-3-1吧,也就是3-1销售后的库库存数量还有就是出库表与库存表间通过名称联系,这样不太好在琢磨一下
很经典的“先进先出”的库存处理的SQL语句。 CREATE TABLE #tmp (
ID int IDENTITY (1, 1),
单价 decimal(18, 2) NOT NULL ,
数量 decimal(18, 2) NOT NULL ,
已出数量 decimal(18, 2) NOT NULL
)
insert into #tmp(单价,数量,已出数量) values(1.1,50,0)
insert into #tmp(单价,数量,已出数量) values(1.3,30,0)
insert into #tmp(单价,数量,已出数量) values(1.4,60,0)
insert into #tmp(单价,数量,已出数量) values(1.5,20,0)
declare @t decimal(18, 2),@temp decimal(18, 2)
set @t=90update #tmp set @temp=case when @t>数量 then 数量 else @t end,数量=数量-@temp,@t=@t-@temp
from #tmp
where 单价<=1.30
select * from #tmp
go
drop table #tmp
CREATE TABLE #tmp (
ID int IDENTITY (1, 1),
单价 decimal(18, 2) NOT NULL ,
数量 decimal(18, 2) NOT NULL ,
已出数量 decimal(18, 2) NOT NULL
)
insert into #tmp(单价,数量,已出数量) values(1.1,50,0)
insert into #tmp(单价,数量,已出数量) values(1.3,30,0)
insert into #tmp(单价,数量,已出数量) values(1.4,60,0)
insert into #tmp(单价,数量,已出数量) values(1.5,20,0)
declare @t decimal(18, 2),@temp decimal(18, 2),@a varchar(8000)
select @t=60,@a=''update #tmp set @temp=case when @t>数量-已出数量 then 数量-已出数量 else @t end, @t=@t-@temp,@a=@a+','+cast(@temp as varchar(10)),已出数量=@temp+已出数量 where 已出数量<>数量
select * from #tmpset @t=30update #tmp set @temp=case when @t>数量-已出数量 then 数量-已出数量 else @t end,@t=@t-@temp,@a=@a+','+cast(@temp as varchar(10)),已出数量=@temp+已出数量 where 已出数量<>数量
select * from #tmpselect right(@a,len(@a)-1)
go
drop table #tmp
不知道你是从哪里抄来的,完全不正确,单单考虑SQL语句,这里面就有问题,无法执行,而且那个东西没有一点注视,就连我这个SQL高手都不知道它说明了什么。我有现成的先进先出处理的存储过程,实现相当复杂,而且必须得用到游标,不用游标的都不是先进先出的过程。考虑到楼主的表设计,不满足先进先出要求,所以整套业务过程需要先设计表,然后设计存储过程,说白了和C#一点关系都没有。楼主有意思的话,我单独打个包传你看下。SQLSERVER和Oracle两种版本都有,不过实现方式不一样罢了。我个人还是比较喜欢SqlServer下的那个方式。