Table1
发票号
店铺号
商品号
商品数量
标志位(如果数量为零标志位置位2把这条记录废除)
--------------
Table2
店铺号
商品号
商品数量
---------------------------------------------------------------
Table1
发票号 店铺号 商品号 商品数量
记录 1 7 30 10
2 7 30 20
----------------------------------------------------------------
Table2
店铺号 商品号 商品数量
记录 7 30 15
7 30 10
-----------------------------------------------------------------
现在用Table2中的数据更新Table1中的数据,两个表的连接条件是店铺号和商品号,
现在统计Table2的商品数量是25,怎样把Table1中的记录修改为
Table1
发票号 店铺号 商品号 商品数量
记录 1 7 30 0
2 7 30 5
或
Table1
发票号 店铺号 商品号 商品数量
记录 1 7 30 5
2 7 30 0
都可以。
发票号
店铺号
商品号
商品数量
标志位(如果数量为零标志位置位2把这条记录废除)
--------------
Table2
店铺号
商品号
商品数量
---------------------------------------------------------------
Table1
发票号 店铺号 商品号 商品数量
记录 1 7 30 10
2 7 30 20
----------------------------------------------------------------
Table2
店铺号 商品号 商品数量
记录 7 30 15
7 30 10
-----------------------------------------------------------------
现在用Table2中的数据更新Table1中的数据,两个表的连接条件是店铺号和商品号,
现在统计Table2的商品数量是25,怎样把Table1中的记录修改为
Table1
发票号 店铺号 商品号 商品数量
记录 1 7 30 0
2 7 30 5
或
Table1
发票号 店铺号 商品号 商品数量
记录 1 7 30 5
2 7 30 0
都可以。
发票号
店铺号
商品号
商品数量
标志位(如果数量为零标志位置位2把这条记录废除)
--------------
Table2
店铺号
商品号
商品数量
---------------------------------------------------------------
Table1
发票号 店铺号 商品号 商品数量 标志位
记录 1 7 30 10 1
2 7 30 20 1
----------------------------------------------------------------
Table2
店铺号 商品号 商品数量
记录 7 30 15
7 30 10
-----------------------------------------------------------------
现在用Table2中的数据更新Table1中的数据,两个表的连接条件是店铺号和商品号,
现在统计Table2的商品数量是25,怎样把Table1中的记录修改为
Table1
发票号 店铺号 商品号 商品数量 标志位
记录 1 7 30 0 2
2 7 30 5 1
或
Table1
发票号 店铺号 商品号 商品数量 标志位
记录 1 7 30 5 1
2 7 30 0 2
都可以。
insert @Table1 select
1 , 7 , 30 , 10 union select
2 , 7 , 30 , 20 union select
3 , 7 , 30 , 20
----------------------------------------------------------------
declare @Table2 table(店铺号 int,商品号 int,商品数量 int)
insert @Table2 select
7, 30 , 15 union select
7 , 30 , 10
declare @i int,@j int
set @j = 0
select @i = sum(商品数量) from @table2
---query
select
*,结存=(@i-isnull((select sum(商品数量) from @Table1 where 发票号<=a.发票号 ),0))
from
@Table1 a
--update
update a
set 商品数量 = case when @i>isnull((select sum(商品数量) from @Table1 where 发票号<=a.发票号 ),0) then 0
else -(@i-isnull((select sum(商品数量) from @Table1 where 发票号<=a.发票号 ),0))
end
from @table1 a
select * from @Table1
insert @Table1 select
1 , 7 , 30 , 10 union select
2 , 7 , 30 , 20 union select
3 , 7 , 30 , 20 union select
4 , 7 , 31 , 20
----------------------------------------------------------------
declare @Table2 table(店铺号 int,商品号 int,商品数量 int)
insert @Table2 select
7, 30 , 15 union select
7 , 30 , 10 union select
7 , 31 , 11
declare @i int,@j int,@m intdeclare cur cursor for select 店铺号 ,商品号,sum(商品数量) 商品数量 from @table2 group by 店铺号 ,商品号OPEN curFETCH NEXT FROM cur INTO @j, @m,@iWHILE @@FETCH_STATUS = 0
BEGIN --update
update a
set 商品数量 = case when @i>isnull((select sum(商品数量) from @Table1 where 发票号<=a.发票号 and 店铺号= a.店铺号 and 商品号 = a.商品号 ),0) then 0
else (isnull((select sum(商品数量) from @Table1 where 发票号<=a.发票号 and 店铺号= a.店铺号 and 商品号 = a.商品号),0))-@i
end
from @table1 a
where 店铺号 = @j and 商品号 = @m
FETCH NEXT FROM cur INTO @j, @m,@i
end
CLOSE cur
DEALLOCATE cur
select * from @Table1
/*
发票号 店铺号 商品号 商品数量
----------- ----------- ----------- -----------
1 7 30 0
2 7 30 5
3 7 30 25
4 7 31 9(所影响的行数为 4 行)
*/
发票号
店铺号
商品号
商品数量
标志位(如果数量为零标志位置位2把这条记录废除)
--------------
Table2
店铺号
商品号
商品数量
---------------------------------------------------------------
Table1
发票号 店铺号 商品号 商品数量 标志位
记录 1 7 30 10 1
2 7 30 20 1
----------------------------------------------------------------
Table2
店铺号 商品号 商品数量
记录 7 30 15
7 30 10
-----------------------------------------------------------------
现在用Table2中的数据更新Table1中的数据,两个表的连接条件是店铺号和商品号,
现在统计Table2的商品数量是25,怎样把Table1中的记录修改为
Table1
发票号 店铺号 商品号 商品数量 标志位
记录 1 7 30 0 2
2 7 30 5 1
或
Table1
发票号 店铺号 商品号 商品数量 标志位
记录 1 7 30 5 1
2 7 30 0 2
都可以。
insert into @table1
select 1,7,30,10,1 union all
select 2,7,30,20,1
declare @table2 table(shop int,item int,qty int)
insert into @table2
select 7,30,15 union all
select 7,30,10
--建立环境update t1
set t1.qty=case t1.invoice when (select min(invoice) from @table1 where shop=t1.shop and item=t1.item) then t.qty else 0 end,
t1.symbol=case t1.invoice when (select min(invoice) from @table1 where shop=t1.shop and item=t1.item) then t1.symbol+1 else t1.symbol end
from @table1 t1
inner join (
select shop,item,sum(qty) qty
from (
select shop,item,qty from @table1 union all
select shop,item,-qty from @table2
) t
group by shop,item
) t on t1.shop=t.shop and t1.item=t.item
--更新数据select * from @table1
--查询数据/*
invoice shop item qty symbol
----------- ----------- ----------- ----------- -----------
1 7 30 5 2
2 7 30 0 1(2 row(s) affected)*/code]
insert into @table1
select 1,7,30,10,1 union all
select 2,7,30,20,1
declare @table2 table(shop int,item int,qty int)
insert into @table2
select 7,30,15 union all
select 7,30,10
--建立环境update t1
set t1.qty=case t1.invoice when (select min(invoice) from @table1 where shop=t1.shop and item=t1.item) then t.qty else 0 end,
t1.symbol=case t1.invoice when (select min(invoice) from @table1 where shop=t1.shop and item=t1.item) then t1.symbol+1 else t1.symbol end
from @table1 t1
inner join (
select shop,item,sum(qty) qty
from (
select shop,item,qty from @table1 union all
select shop,item,-qty from @table2
) t
group by shop,item
) t on t1.shop=t.shop and t1.item=t.item
--更新数据select * from @table1
--查询数据/*
invoice shop item qty symbol
----------- ----------- ----------- ----------- -----------
1 7 30 5 2
2 7 30 0 1(2 row(s) affected)*/
insert into @table1
select 1,7,30,10,1 union all
select 2,7,30,20,1
declare @table2 table(shop int,item int,qty int)
insert into @table2
select 7,30,15 union all
select 7,30,10
--建立环境update t1
set t1.qty=case t1.invoice when (select min(invoice) from @table1 where shop=t1.shop and item=t1.item) then t.qty else 0 end,
t1.symbol=case t1.invoice when (select min(invoice) from @table1 where shop=t1.shop and item=t1.item) then t1.symbol else t1.symbol+1 end
from @table1 t1
inner join (
select shop,item,sum(qty) qty
from (
select shop,item,qty from @table1 union all
select shop,item,-qty from @table2
) t
group by shop,item
) t on t1.shop=t.shop and t1.item=t.item
--更新数据select * from @table1
--查询数据/*
invoice shop item qty symbol
----------- ----------- ----------- ----------- -----------
1 7 30 5 1
2 7 30 0 2(2 row(s) affected)*/
谢谢
insert into @table1
select 1,7,30,10,1 union all
select 2,7,30,20,1
declare @table2 table(shop int,item int,qty int)
insert into @table2
select 7,30,15 union all
select 7,30,10
--建立环境update t1
set t1.qty=case t1.invoice when minvalue then t.qty else 0 end,
t1.symbol=case t1.invoice when minvalue then t1.symbol else t1.symbol+1 end
from @table1 t1
inner join (
select shop,item,sum(qty) qty,min(invoice) minvalue
from (
select invoice,shop,item,qty from @table1 union all
select null,shop,item,-qty from @table2
) t
group by shop,item
) t on t1.shop=t.shop and t1.item=t.item
--更新数据select * from @table1/*
invoice shop item qty symbol
----------- ----------- ----------- ----------- -----------
1 7 30 5 1
2 7 30 0 2(2 row(s) affected)*/--学, 无止境 觉得这样如何?