就像我說的,我oppinion會更好地解決在樓內設有商務邏輯但如果你真的想在服務器端出於某種原因(夜間,...),然後像這樣的腳本。但請記住我在評論中指出的性能問題和其他事情。對於一個基於C#的解決方案,看看Blam的答案在這裏是sql的方法
create table tabA (orderNum Numeric(1), partNum varchar(3), quntity numeric(2), shipdate datetime)
create table tabB (lotID Numeric(1), partNum varchar(3), quntity numeric(2), dateEnt datetime)
create table tabC (orderNum Numeric(1), partNum varchar(3), quntity numeric(2), lotID Numeric(1), lotQuntity numeric(2))
insert into tabA values(1,'XYZ',15,'20140107'),
(2,'XYZ',15,'20140107')
insert into tabB values(1,'XYZ',10,'20140101'),
(2,'XYZ',10,'20140102'),
(3,'XYZ',10,'20140103')
select * into #tabB1 from tabB
declare @lnOrderNum numeric(1), @lcPartNum varchar(3), @ldShipdate datetime, @lnQuntity numeric(2)
declare @lnLotID numeric(1), @lcPartNum1 varchar(3), @ldEntdate datetime, @lnQuntity1 numeric(2),
@lnQuntity2 numeric(2), @lnQuntity3 numeric(2)
declare cur_a cursor for
select *
from tabA
order by shipdate
open cur_a
fetch next from cur_a into @lnOrderNum, @lcPartNum,@lnQuntity, @ldShipdate
while @@Fetch_status=0
BEGIN
SET @lnQuntity3 = @lnQuntity
while @lnQuntity > 0
BEGIN
select top 1 lotID, partNum, quntity, dateEnt into #tmpB1
from #tabB1
where quntity > 0
order by dateEnt
set @lnLotID = (select top 1 lotId from #tmpB1)
set @lcPartNum1 = (select top 1 partNum from #tmpB1)
set @ldEntdate = (select top 1 dateEnt from #tmpB1)
set @lnQuntity1 = (select top 1 quntity from #tmpB1)
IF @lnQuntity1 >= @lnQuntity
BEGIN
SET @lnQuntity2 = 0
insert into tabC values(@lnOrderNum,@lcPartNum,@lnQuntity3,@lnLotID,@lnQuntity)
update #tabB1
set quntity = @lnQuntity1 - @lnQuntity
where lotId = @lnLotID and partNum = @lcPartNum1 and dateEnt = @ldEntdate
drop table #tmpB1
SET @lnQuntity = @lnQuntity2
END
ELSE
BEGIN
SET @lnQuntity2 = @lnQuntity - @lnQuntity1
insert into tabC values(@lnOrderNum,@lcPartNum,@lnQuntity3,@lnLotID,@lnQuntity1)
update #tabB1
set quntity = 0
where lotId = @lnLotID and partNum = @lcPartNum1 and dateEnt = @ldEntdate
drop table #tmpB1
SET @lnQuntity = @lnQuntity2
END
END
fetch next from cur_a into @lnOrderNum, @lcPartNum,@lnQuntity, @ldShipdate
END
close cur_a
deallocate cur_a
drop table #tabB1
drop table tabA
drop table tabB
SELECT * FROM TABC
drop table tabC
請告訴我們你試過的東西。 – JiggsJedi
恰好滿足什麼標準? – Hogan
表b中的數量會發生什麼?每行更新爲零? – Horaciux