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

Error Message:
Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

Severity level:
16.

Description:
This error message appears when you try to use subquery (correlated or not) that returns more than one value to the calling query.

Consequences:
The T-SQL statement can be parsed, but causes the error at runtime.

Resolution:
Error of the Severity Level 16 are generated by the user and can be fixed by the SQL Server user. The statement cannot be executed this way. You must rewrite the subquery, so that it returns exactly one value.

Versions:
All versions of SQL Server.

Example(s):
SELECT o.*
  FROM Northwind.dbo.Orders o
 WHERE o.OrderDate =
  (SELECT OrderDate
     FROM Northwind.dbo.Orders)

Remarks:
In the above example returns the subquery the column OrderDate for all rows of the table Orders. This raises the error. One possible rewrite of the query might look like:

SELECT o.*
  FROM Northwind.dbo.Orders o
 WHERE o.OrderDate =
  (SELECT MAX(OrderDate)
     FROM Northwind.dbo.Orders)

Because of the aggregate function MAX() is guaranteed that exactly only value is returned and the subquery is syntactically valid.




Related Articles :

  • No Related Articles Found
Uncategorized

No comments yet... Be the first to leave a reply!

Software Reviews | Book Reviews | FAQs | Tips | Articles | Performance Tuning | Audit | BI | Clustering | Developer | Reporting | DBA | ASP.NET Ado | Views tips | | Developer FAQs | Replication Tips | OS Tips | Misc Tips | Index Tuning Tips | Hints Tips | High Availability Tips | Hardware Tips | ETL Tips | Components Tips | Configuration Tips | App Dev Tips | OLAP Tips | Admin Tips | Software Reviews | Error | Clustering FAQs | Performance Tuning FAQs | DBA FAQs |