UPDATE from SELECT complains about more that one value returned

Posted by Álvaro G. Vicario on Stack Overflow See other posts from Stack Overflow or by Álvaro G. Vicario
Published on 2010-05-13T09:37:30Z Indexed on 2010/05/13 9:44 UTC
Read the original article Hit count: 229

Filed under:
|
|

I have this data structure:

request
=======
building_id
lot_code

building
========
building_id
lot_id

lot
===
lot_id
lot_code

The request table is missing the value for the building_id column and I want to fill it in from the other tables. So I've tried this:

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
);

But I'm getting this error:

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

Is it due to wrong syntax? The data model allows more than one building per lot but actual data doesn't contain such cases so there should be at most one building_id per lot_code.

© Stack Overflow or respective owner

Related posts about sql-server

Related posts about sql-server-2005