SELECT的UPDATE抱怨更多的返回值

时间:2010-05-13 09:37:30

标签: sql-server sql-server-2005 tsql

我有这个数据结构:

request
=======
building_id
lot_code

building
========
building_id
lot_id

lot
===
lot_id
lot_code

request表缺少building_id列的值,我想从其他表填充它。所以我试过这个:

UPDATE request
SET building_id = (
    SELECT bu.building_id
    FROM building bu
    INNER JOIN lot lo ON bu.lot_id=lo.lot_id
    WHERE lo.lot_code = request.lot_code
);

但是我收到了这个错误:

  

子查询返回的值超过1。   这是不允许的   子查询跟随=,!=,<,< =,>,> =   或者子查询用作   表达

是否由于语法错误?数据模型允许每批不止一个建筑物,但实际数据不包含此类情况,因此每building_id最多只能有一个lot_code

2 个答案:

答案 0 :(得分:2)

可能你的意思是

UPDATE request
SET building_id = bu.building_id
FROM building bu
    INNER JOIN lot lo ON bu.lot_id=lo.lot_id
WHERE lo.lot_code = request.lot_code

答案 1 :(得分:0)

在查询中使用top 1,如下例所示。

UPDATE request
SET building_id = (
    SELECT top 1 bu.building_id
    FROM building bu
    INNER JOIN lot lo ON bu.lot_id=lo.lot_id
    WHERE lo.lot_code = request.lot_code
);