In SQL Server you can update with a join directly. Most other RDBMSs are similar:
update b
set price = m.price + s.price + t.price
from project.boxes as b
left join project.types as t on t.type=b.type
left join project.materials as m on m.material=b.material
left join project.sizes as s on s.size=b.size
In answer to your sort-of question on functions, you need to add a parameter @type
and pass through b.type
:
UPDATE project.boxes
SET Price = dbo.calculateBoxPrice(boxes.type)
Note:
The code you have given will update all rows, even when there is no matching type, material, size. You may want some combination of left join
and isnull/ifnull
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…