日期:2014-05-17  浏览次数:20351 次

求高手帮忙
有4张表,需求如下:
库存产品表,材质表,产品设计表,材质价格表

存货ID,材质重量,产品设计ID,材质ID,
材质ID,材质价格
产品设计ID,产品工艺系数
材质价格ID,材质ID,最小重量,最大重量,重量价格系数

导入的库存产品里,价格是NULL,需要批量更新。
库存产品价格公式为:材质重量*材质单价*产品工艺系数*重量价格系数

批量更新的update语句应该怎么写?
------解决方案--------------------
update a set 材质价格=a.材质重量*b.材质单价*c.产品工艺系数*d.重量价格系数
from 库存产品表 a inner join 材质表 b on a.材质ID=b.材质ID
inner join 产品设计表 c on a.产品设计ID=c.产品设计ID
inner join 材质价格表 d on a.材质ID=d.材质ID
------解决方案--------------------

--table1
if OBJECT_ID('table1','u') is not null
drop table table1

go
create table table1
(
存货ID nvarchar(10),
材质重量 int,
存货价格 decimal(18,2),
产品设计ID nvarchar(10),
材质ID nvarchar(10)
)

go

--table2
if OBJECT_ID('table2','u') is not null
drop table table2

go
create table table2
(
材质ID nvarchar(10),
材质价格 decimal(18,2)
)

go

--table3
if OBJECT_ID('table3','u') is not null
drop table table3

go
create table table3
(
产品设计ID nvarchar(10),
产品工艺系数 decimal(18,2)
)

go

--table4
if OBJECT_ID('table4','u') is not null
drop table table4

go
create table table4
(
材质价格ID nvarchar(10),
材质ID nvarchar(10),
最小重量 nvarchar(10),
最大重量 nvarchar(10),
重量价格系数 decimal(18,2)
)

go

--SQL
update table1 set 存货价格 =材质重量*B.材质价格*C.产品工艺系数*(select 重量价格系数 from table4 D where A.材质重量>最小重量 and A.材质重量<最大重量)
from table1 A 
inner join table2 B on A.材质ID=B.材质ID 
inner join table3 C on A.产品设计ID=C.产品设计ID