Dave S Dave S - 1 month ago 8x
SQL Question

Increment an integer in a Simple.Data Update query

Is it possible to increment an integer in an Update query using Simple.Data?

My database is SQL Server, but I know Simple.Data is designed to be database agnostic so perhaps this query is not available in other providers.

The SQl query I am trying to generate would look like this:

UPDATE MyTable SET MyInt = MyInt + 1 WHERE Id = 123

Currently I have to do a SELECT and an UPDATE in Simple.Data to achieve the same thing:

var result = db.MyTable.Find(db.MyTable.Id == 123);
db.MyTable.UpdateById(Id: result.Id, MyInt: result.MyInt );

I am wondering if there is a way to replicate the single SQL statement above.


You can do this:

db.MyTable.UpdateById(Id: 123, MyInt: db.MyTable.MyInt + 1);

Works on SQL Server, should work in all the SQL-based providers.