Ask the Expert

Result of query more than one row

Is there a solution for the error, result of select more than one row?


    Requires Free Membership to View

The description of the problem is a little vague, but I'm going to guess the query has a scalar subquery that is actually returning more than one row --

select foo, bar
  from fubar
 where bar = ( select baz from qux )

In this example, the subquery must return exactly one value, or else the outer query fails. This cannot be checked by the database until execution time. Depending on what is required, there are a couple of ways to rewrite the query --

select foo, bar
  from fubar
 where bar = ( select min(baz) from qux )

Using an aggregate function like min() will always return only one value.

select foo, bar
  from fubar
 where bar IN ( select baz from qux )

Here the subquery is no longer a scalar or single value subquery, but the outer query will not fail if more than one value is returned.

For More Information

  • What do you think about this answer? E-mail the edtiors at editor@searchDatabase.com with your feedback.
  • The Best SQL Web Links: tips, tutorials, scripts, and more.
  • Have an SQL tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize. Submit your tip today!
  • Ask your technical SQL questions -- or help out your peers by answering them -- in our live discussion forums.
  • Ask the Experts yourself: Our SQL, database design, Oracle, SQL Server, DB2, metadata, object-oriented and data warehousing gurus are waiting to answer your toughest questions.

This was first published in February 2002

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: