Recursive AI

Read SQLite UPDATE RETURNING rows in C#

OneLite is a free, open-source SQLite-compatible database for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Pass UPDATE … RETURNING qty to QueryAsync to read the new quantity without a second query.

Terminal
dotnet add package RecursiveAI.OneLite

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneLite;
await using var db =
    await Database.OpenAsync(":memory:");
await db.ExecuteScriptAsync("""
    CREATE TABLE stock(id, qty);
    INSERT INTO stock VALUES (1, 4);
    """);
int increase = 2;
var values = await db.QueryAsync<long>($"""
    UPDATE stock SET qty = qty + {increase}
    WHERE id = 1 RETURNING qty
    """);
foreach (long qty in values)
    Console.WriteLine(qty);

Unzip the example, then dotnet run.

{increase} becomes a SQL parameter. The snippet prints the new quantity for item 1:

Text
6

The full program

The download restocks every item below a threshold and returns both the ID and the new quantity. The numbers in the interpolated SQL become bound parameters. [OneLiteRow] tells OneLite to generate the mapper for StockRow at build time.

C#
using RecursiveAI.OneLite;
 
await using var db = await Database.OpenAsync(":memory:");
await db.ExecuteScriptAsync("""
    CREATE TABLE stock(id INTEGER PRIMARY KEY, qty INTEGER);
    INSERT INTO stock VALUES (1, 4), (2, 3), (3, 10);
    """);
 
int increase = 2;
int threshold = 5;
var updated = await db.QueryAsync<StockRow>($"""
    UPDATE stock SET qty = qty + {increase}
    WHERE qty < {threshold}
    RETURNING id, qty
    """);
 
Console.WriteLine($"Rows returned: {updated.Count}");
foreach (var row in updated.OrderBy(row => row.Id))
    Console.WriteLine($"Item {row.Id}: {row.Qty}");
 
[OneLiteRow]
record StockRow(long Id, long Qty);
Text
Rows returned: 2
Item 1: 6
Item 2: 5

Item 3 is already above the threshold, so the statement doesn’t update or return it. If no rows match the WHERE condition, QueryAsync gives you an empty list, not an exception.

Variations

Return just the IDs. Replace the QueryAsync call and the lines that print the results in the full program with:

C#
var ids = await db.QueryAsync<long>($"""
    UPDATE stock SET qty = qty + {increase}
    WHERE qty < {threshold}
    RETURNING id
    """);
foreach (long id in ids.Order())
    Console.WriteLine(id);
Text
1
2

The short answer

Put RETURNING after the WHERE clause and list the columns you want back, such as id, qty. In OneLite, call QueryAsync<T> to get one result for each row the update touches, with the new values. Use a scalar type like long to read the first result column, or a record marked with [OneLiteRow] to read several columns. You don’t need a separate SELECT to find the updated values.

Does SQLite support RETURNING on UPDATE?

Yes. SQLite added RETURNING in 3.35.0; OneLite 1.0.0 supports it on the ordinary table in this recipe. WHERE decides which rows to change. RETURNING decides which columns to report for every row directly updated. In the full program, item 2 was at 3; the result contains 5, the quantity after adding 2, not the old value. Item 3 doesn’t appear at all because the update never touches it.

SQLite’s RETURNING reports values from the top-level statement. It doesn’t report rows changed by foreign key cascades, and SQLite’s reference notes that the values don’t include later changes from AFTER triggers. If the application needs final values after a trigger, it needs to read them separately. Likewise, a result row doesn’t mean an outer transaction has committed.

How do you read multiple rows from UPDATE RETURNING in C#?

Treat the result as a list, even if you expect just one row. The snippet at the top uses QueryAsync<long> because it asks for one column. The full program asks for two and uses a record whose Id and Qty parameters match the SQL column names. By contrast, OneLite’s ExecuteAsync returns a count of rows changed, not their values. Use that when the count is all you need.

Pick the columns you use rather than requesting RETURNING * by default. OneLite collects the rows while the statement runs, so returning wide text or blob columns for a large update adds to the data held in memory. SQLite doesn’t guarantee an output order for RETURNING. The full program sorts the returned rows by ID in C# before printing them.

All onelite recipes