Skip to main content
October 18, 2007
Question

Subquery returning mulitple rows error

  • October 18, 2007
  • 2 replies
  • 295 views
Hi All,

While trying to execute this statement in Postgres,

update customer_master set locationid = (select p.locationid from location_info l,customer_location cl where c.locationid=cl.locationid)

I get this error,

ERROR: more than one row returned by a subquery used as an expression

Any Ideas?...



    This topic has been closed for replies.

    2 replies

    Inspiring
    October 19, 2007
    S C O R P I O N wrote:
    > Hi All,
    >
    > While trying to execute this statement in Postgres,
    >
    > update customer_master set locationid = (select p.locationid from
    > location_info l,customer_location cl where c.locationid=cl.locationid)
    >
    > I get this error,
    >
    > ERROR: more than one row returned by a subquery used as an expression
    >
    > Any Ideas?...
    >
    >
    >
    >
    >

    well, the error says it all: your subquery returns several records
    (rows), not one.
    but, i am perplexed by your subquery table aliases: p, c, cl, l??? which
    table is aliased as p? and which one is c?

    --

    Azadi Saryev
    Sabai-dee.com
    http://www.sabai-dee.com
    Inspiring
    October 18, 2007
    First, in your subquery, you have a table alias p, that does not refer to any tables.

    Secondly, nothing in the code you posted shows a relationship between customer_master and the tables in your subquery.

    Thirdly, if postgres supports it, the following syntax generally runs quicker and has fewer problems:

    update a
    set somefield = b.somefield
    from TableA a join TableB b on something
    where etc