使用UPDATE FROM来批量设置字段

时间:2011-03-29 10:38:25

标签: sql tsql sql-update

我有以下查询,我曾经用它来提取车辆ID,它的注册,驱动程序ID和驱动程序的名称。您在此查询中看到的_core_people表格也是我希望设置到车辆牌照的字段。

以下是查询:

SELECT fv._plate, cp._people_name
FROM
_fleet_vehicle fv,
_fleet_vehicle_status fvs,
_core_people cp,
_fleet_allocation fa
WHERE 
cp._id_hierarchy = fv._id_hierarchy
AND fv._id_status = fvs._id
AND fvs._status_live = 1
AND fa._id_person = cp._id
AND fa._id_vehicle = fv._id
AND fa._alloc_end_date IS NULL
ORDER BY cp._people_name ASC

现在,我想编写一个UPDATE...FROM子句,利用它将_core_people._plate(此处未显示)设置为其车辆的平板区域。

但是,我不确定如何构建UPDATE...FROM子句。

此外,一些司机有2辆车。它还能用吗?

提前致谢!

1 个答案:

答案 0 :(得分:3)

UPDATE  _core_people
SET     _plate = fv._plate
FROM    _fleet_vehicle fv,
        _fleet_vehicle_status fvs,
        _core_people cp,
        _fleet_allocation fa
WHERE   cp._id_hierarchy = fv._id_hierarchy
        AND fv._id_status = fvs._id
        AND fvs._status_live = 1
        AND fa._id_person = cp._id
        AND fa._id_vehicle = fv._id
        AND fa._alloc_end_date IS NULL

如果_core_people匹配条件的记录超过1条,_core_people只会更新为其中一条记录(无法准确判断哪一条记录)。