发票主表
aid cuscode--客户编码
1 001
发票子表
bid aid cinvcode--存货编码 price
1 1 0001 50
2 1 0002 90
客户调价单
id cuscode cinvcode price
1 001 0001 100
2 001 0002 110
3 002 0001 88
现在需要批量更改发票子表的价格
(把发票子表的价格更新成客户调价单的价格)
怎么写这个句子啊 请高手支招
aid cuscode--客户编码
1 001
发票子表
bid aid cinvcode--存货编码 price
1 1 0001 50
2 1 0002 90
客户调价单
id cuscode cinvcode price
1 001 0001 100
2 001 0002 110
3 002 0001 88
现在需要批量更改发票子表的价格
(把发票子表的价格更新成客户调价单的价格)
怎么写这个句子啊 请高手支招
如果你这两个表,再没有其他键关联就这么写
UPDATE a SET price = b.price
FROM 发票子表 a JOIN 客户调价单 b ON a.cinvcode = b.cinvcode
WHERE b.price IS NOT NULL
http://blog.csdn.net/downmoon/archive/2006/09/28/1301932.aspx
--创建临时表
--发票主表
create table #t1
( aid int,
cuscode nvarchar(10)
)
--发票子表
create table #t2
(
bid int,
aid int,
cinvcode nvarchar(10),
price int
)
--客户调价单
create table #t3
(
id int,
cuscode nvarchar(10),
cinvcode nvarchar(10),
price int
)
--插入测试数据
insert into #t1
select 1,'001'insert into #t2
select 1,1,'0001',50 union all
select 2,1,'0002',90insert into #t3
select 1,'001','0001',100 union all
select 2,'001','0002',110 union all
select 3,'002','0001',88--更新数据
UPDATE A SET A.price = B.price
FROM #t2 A LEFT JOIN #t1 C
ON A.aid=C.aid LEFT JOIN #t3 B
ON C.cuscode=B.cuscode AND A.cinvcode=B.cinvcode
set price=(select price from 客户调价单 where 发票子表.bid=客户调价单.id)子查询
把发票子表的价格改成客户调价单的价格