- Error number
- 512
- Severity
- 16 (user-correctable error)
- State
- 1
- Product
- Microsoft SQL Server Database Engine
- Versions
- All supported SQL Server versions (2016 to 2025), Azure SQL Database, Azure SQL Managed Instance
- Category
- Data Type & Conversion
- Last reviewed
- 23 October 2026
On this page
What it means
Error 512 means a subquery in a place that needs exactly one value, such as after = or in the SELECT list, returned two or more rows when the query ran.
A subquery written after a comparison operator, in the SELECT list, or on the right of SET col = is a scalar subquery: it must produce at most one row and one column. SQL Server compiles it happily, then raises 512 at run time the first time it returns a second row. That is why the same query can work for months and fail the day a second matching row appears.
The message lists the operators but not which subquery failed. In a statement with several subqueries, run each one on its own with the values from the outer row and see which returns more than one row. The usual reason is a lookup on a column that is not unique, or a correlated subquery missing part of its join condition.
Common causes
- A lookup on a non-unique column, for example
WHERE CustomerId = (SELECT CustomerId FROM Customers WHERE City = ...)when several customers share the city. - A subquery in the SELECT list that returns one row per child, such as one order total per customer when customers have several orders.
UPDATE ... SET col = (SELECT ...)where the subquery is not correlated to the row being updated, or matches several source rows.- New data that breaks an assumption: a second row arrives for a key that used to be unique, often because no UNIQUE constraint enforces it.
Diagnose it
Read-only These queries only read metadata and dynamic management views. They do not change data, permissions or configuration, so they are safe to run first.
Run the subquery on its own and count the rows
SELECT COUNT(*) AS rows_returned
FROM dbo.Customers AS c
WHERE c.City = N'London'; More than 1 means the subquery cannot be used after = as written.
Find the outer rows that match more than one inner row
For a correlated subquery, group the inner table by the correlation column to see which keys are duplicated.
SELECT o.CustomerId, COUNT(*) AS matches
FROM dbo.Orders AS o
GROUP BY o.CustomerId
HAVING COUNT(*) > 1; Each CustomerId listed makes (SELECT o.Total FROM dbo.Orders AS o WHERE o.CustomerId = c.CustomerId) return several rows.
How to fix it
Each fix is labelled with what it changes. Try read-only checks and the least invasive fix first, and test changes outside production.
Use IN or EXISTS when several matches are valid
Read-onlyIf the outer row should match any of the returned values, replace = with IN (or EXISTS with a correlated condition).
SELECT o.OrderId, o.Total
FROM dbo.Orders AS o
WHERE o.CustomerId IN (
SELECT c.CustomerId
FROM dbo.Customers AS c
WHERE c.City = N'London'
);
Return one value on purpose: aggregate or TOP (1) with ORDER BY
Read-onlyWhen you want a single value, say which one: SUM or MAX collapse the rows, and TOP (1) with a deterministic ORDER BY picks a specific row. TOP without ORDER BY returns an arbitrary row.
SELECT c.CustomerId,
c.Name,
(SELECT SUM(o.Total) FROM dbo.Orders AS o WHERE o.CustomerId = c.CustomerId) AS OrderTotal,
(SELECT TOP (1) o.Total FROM dbo.Orders AS o
WHERE o.CustomerId = c.CustomerId
ORDER BY o.OrderId DESC) AS LatestTotal
FROM dbo.Customers AS c;
Rewrite as a JOIN
Read-onlyA JOIN returns one row per match instead of failing, which is usually what a report wants. Check the row count afterwards, since a one-to-many join multiplies rows.
SELECT o.OrderId, o.Total
FROM dbo.Orders AS o
JOIN dbo.Customers AS c ON c.CustomerId = o.CustomerId
WHERE c.City = N'London';
Use UPDATE ... FROM with a join for updates
Changes dataCorrelate the update to its source with a join on the key. If the source can still hold several rows per key, de-duplicate it first, because UPDATE ... FROM would then pick one of them unpredictably.
UPDATE c
SET c.LastTotal = o.Total
FROM dbo.Customers AS c
JOIN (SELECT CustomerId, MAX(Total) AS Total
FROM dbo.Orders
GROUP BY CustomerId) AS o ON o.CustomerId = c.CustomerId;
Examples
Scalar subquery after = in a WHERE clause
CREATE TABLE dbo.Customers (CustomerId INT PRIMARY KEY, Name NVARCHAR(100), City NVARCHAR(50));
CREATE TABLE dbo.Orders (OrderId INT PRIMARY KEY, CustomerId INT, Total DECIMAL(10,2));
INSERT INTO dbo.Customers VALUES (1, N'Sara', N'London'), (2, N'Ali', N'London');
INSERT INTO dbo.Orders VALUES (10, 1, 50.00), (11, 2, 75.00);
-- Two customers live in London, so the subquery returns two rows
SELECT o.OrderId, o.Total
FROM dbo.Orders AS o
WHERE o.CustomerId = (
SELECT c.CustomerId
FROM dbo.Customers AS c
WHERE c.City = N'London'
);
Msg 512, Level 16, State 1, Line 7 Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Reproduced on SQL Server 2025 (17.0.1145.4), 23 October 2026.
Scalar subquery in the SELECT list
CREATE TABLE dbo.Customers (CustomerId INT PRIMARY KEY, Name NVARCHAR(100));
CREATE TABLE dbo.Orders (OrderId INT PRIMARY KEY, CustomerId INT, Total DECIMAL(10,2));
INSERT INTO dbo.Customers VALUES (1, N'Sara');
INSERT INTO dbo.Orders VALUES (10, 1, 50.00), (11, 1, 75.00);
-- Sara has two orders, so this expression is not scalar
SELECT
c.CustomerId,
c.Name,
(SELECT o.Total FROM dbo.Orders AS o WHERE o.CustomerId = c.CustomerId) AS OrderTotal
FROM dbo.Customers AS c;
Msg 512, Level 16, State 1, Line 7 Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Reproduced on SQL Server 2025 (17.0.1145.4), 23 October 2026.
UPDATE with SET col = (SELECT ...)
CREATE TABLE dbo.Customers (CustomerId INT PRIMARY KEY, LastTotal DECIMAL(10,2));
CREATE TABLE dbo.Orders (OrderId INT PRIMARY KEY, CustomerId INT, Total DECIMAL(10,2));
INSERT INTO dbo.Customers VALUES (1, NULL);
INSERT INTO dbo.Orders VALUES (10, 1, 50.00), (11, 1, 75.00);
-- Customer 1 has two orders, so the subquery returns two values
UPDATE dbo.Customers
SET LastTotal = (SELECT o.Total FROM dbo.Orders AS o WHERE o.CustomerId = dbo.Customers.CustomerId);
Msg 512, Level 16, State 1, Line 7 Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. The statement has been terminated.
Reproduced on SQL Server 2025 (17.0.1145.4), 23 October 2026.
How to prevent it
If a column is supposed to identify one row, enforce it with a PRIMARY KEY or UNIQUE constraint; then a lookup on it can never return two rows. Where a subquery may legitimately match several rows, write it with IN, EXISTS, an aggregate or a join from the start.
Test each scalar subquery alone before embedding it, and prefer joins for combining tables. The subqueries vs joins guide covers when each form fits.
Common questions
Why did the query work yesterday and fail today with error 512?
The subquery is checked only when it runs. Until now it found at most one row; a new row (a second customer in the city, a second order) made it return two. Add a constraint if the value should be unique, or rewrite the subquery.
Should I just add TOP 1 to make the error go away?
Only with an ORDER BY that chooses the row you want. TOP (1) without ORDER BY returns whichever row the engine reads first, which can change between runs and give silently wrong results.
Is IN always a safe replacement for =?
IN changes the meaning from match this value to match any of these values. It is correct when several matches are valid. Watch NOT IN with a subquery that can return NULL: it then returns no rows, so NOT EXISTS is safer.
Learn more on CodeWithSQL
Official documentation
Last technically reviewed 23 October 2026; examples and diagnostics run on SQL Server 2025 (17.0.1145.4). Message text checked against the engine's sys.messages catalogue.