T1表
编码 名称 数量
1001 酒 50
1002 烟 10
1003 茶 20
1002 烟 15
1005 花 25T2表 编码 名称 库存
1001 酒 100
1002 烟 100
1003 茶 100
1004 水 100 想得到结果是
T2表 编码 名称 库存
1001 酒 150
1002 烟 175
1003 茶 180
1004 水 100
1005 花 25
注意,T1表中数据有重复,也有T2 表中原来没有的商品
其实也就是通过一张表的数量对另一张表进行批量增减,应该如何写?谢谢
编码 名称 数量
1001 酒 50
1002 烟 10
1003 茶 20
1002 烟 15
1005 花 25T2表 编码 名称 库存
1001 酒 100
1002 烟 100
1003 茶 100
1004 水 100 想得到结果是
T2表 编码 名称 库存
1001 酒 150
1002 烟 175
1003 茶 180
1004 水 100
1005 花 25
注意,T1表中数据有重复,也有T2 表中原来没有的商品
其实也就是通过一张表的数量对另一张表进行批量增减,应该如何写?谢谢
insert @T1 select 1001,N'酒',50
union all select 1002,N'烟',10
union all select 1003,N'茶',20
union all select 1002,N'烟',15
union all select 1005,N'花',25declare @T2 table([No] int,[Class] nvarchar(4),Store int)
insert @T2 select 1001,N'酒',100
union all select 1002,N'烟',100
union all select 1003,N'茶',100
union all select 1004,N'水',100
--更新T2表
update @T2
set Store=B.Store + A.Store
from
@T2 B,(select [No] ,sum(Store) Store from @T1 group by [No]) A
where
B.[No]=A.[No]
--不缺T2表
insert @T2
select
[No],[Class],Store
from
@T1 B
where
B.[No] not in (select distinct [No] from @T2)select * from @T2
(
[No] int,
[Class] nvarchar(4),
Store int
)
insert @T1 select 1001,N'酒',50
union all select 1002,N'烟',10
union all select 1003,N'茶',20
union all select 1002,N'烟',15
union all select 1005,N'花',25 declare @T2 table
(
[No] int,
[Class] nvarchar(4),
Store int
)
insert @T2 select 1001,N'酒',100
union all select 1002,N'烟',100
union all select 1003,N'茶',100
union all select 1004,N'水',100 --更新存在的
update @T2
set Store=t2.Store+t1.Store
from @T2 t2
inner join
(
select [No],[Class],sum(Store) Store
from @T1
group by [No],[Class]
)t1 on t1.[No]=t2.[No] and t1.[Class]=t2.[Class]--插入没有的
insert into @t2
select [No],[Class],sum(Store) Store
from @T1 t1
where not exists(select 1 from @t2 t2 where t1.[No]=t2.[No] and t1.[Class]=t2.[Class] )
group by [No],[Class]select * from @t2结果:
1001 酒 150
1002 烟 125
1003 茶 120
1004 水 100
1005 花 25
(
[No] int,
[Class] nvarchar(4),
Store int
)
insert @T1 select 1001,N'酒',50
union all select 1002,N'烟',10
union all select 1003,N'茶',20
union all select 1002,N'烟',15
union all select 1005,N'花',25 declare @T2 table
(
[No] int,
[Class] nvarchar(4),
Store int
) insert @T2 select 1001,N'酒',100
union all select 1002,N'烟',100
union all select 1003,N'茶',100
union all select 1004,N'水',100 --更新存在的
update @T2
set Store=t2.Store+t1.Store
from @T2 t2
inner join
(
select [No],[Class],sum(Store) Store
from @T1
group by [No],[Class]
)t1 on t1.[No]=t2.[No] and t1.[Class]=t2.[Class] --插入没有的
insert into @t2
select [No],[Class],sum(Store) Store
from @T1 t1
where not exists(select 1 from @t2 t2 where t1.[No]=t2.[No] and t1.[Class]=t2.[Class] )
group by [No],[Class] select * from @t2 结果:
1001 酒 150
1002 烟 125
1003 茶 120
1004 水 100
1005 花 25
select A.编码,A.名称,B.数量,isnull(C.库存,0) as 库存 from
(select distinct 编码,名称 from t1) A
left outer join (
select 编码,名称,sum(数量) as 数量 from t1) On A.编码=B.编码
left outer join T2 C on C.编码=A.编码
这样可以搜索你想要的结果,然后根据需要进行更新和插入就行了
update a set 库存=a.库存+b.sl from t2 a,(select 编码,sl=sum(数量) from t1 group by t1) b where a.编码=b.编码
if object_id('t1') is not null
drop table t1
go
create table t1(编码 varchar(10),名称 varchar(10),数量 int)
go
insert into t1
select '1001','酒',50 union all
select '1002','烟',10 union all
select '1003','茶',20 union all
select '1002','烟',15 union all
select '1005','花',25
go
if object_id('t2') is not null
drop table t2
go
create table t2(编码 varchar(10),名称 varchar(10),库存 int)
go
insert into t2
select '1001','酒',100 union all
select '1002','烟',100 union all
select '1003','茶',100 union all
select '1004','水',100
go
insert into t2 select * from t1 where 编码 not in(select 编码 from t2)update t2 set 库存=库存+(select isnull(sum(数量),0) from t1 where t1.编码=t2.编码)