Updating Data¶
UPDATE statements are built with Sql.Update(table), followed by one or more .Set() calls and an optional .Where().
Basic update¶
var sql = Sql
.Update("Users")
.Set("UserName", "jdoe_updated")
.Where(SqlExpression.Equal("Id", 4))
.ToSql();
Updating multiple columns¶
Chain .Set() for each column you want to change:
var sql = Sql
.Update("Users")
.Set("UserName", "jdoe_new")
.Set("Email", "new@example.com")
.Set("UpdatedAt", SqlConstant.Null)
.Where(SqlExpression.Equal("Id", 42))
.ToSql();
UPDATE [Users] SET [UserName] = 'jdoe_new', [Email] = 'new@example.com', [UpdatedAt] = NULL WHERE [Id] = 42
Update with NULL¶
Pass null (or SqlConstant.Null) to set a column to NULL:
var sql = Sql
.Update("Users")
.Set("DeletedAt", null)
.Where(SqlExpression.Equal("Id", 7))
.ToSql();
Update with parameters¶
var sql = Sql
.Update("Users")
.Set("Email", "Email" + (SqlConstant)"new@example.com")
.Where(SqlExpression.Equal("Id", "UserId" + (SqlConstant)42))
.ToSql();
Update with arithmetic expression¶
Use arithmetic expressions to compute the new value from existing columns — for example, incrementing a counter:
var sql = Sql
.Update("Products")
.Set("Stock", SqlExpression.Subtract((SqlColumn)"Stock", (SqlConstant)1))
.Where(SqlExpression.Equal("Id", "ProductId" + (SqlConstant)5))
.ToSql();
Applying a percentage increase:
var sql = Sql
.Update("Products")
.Set("Price", SqlExpression.Multiply((SqlColumn)"Price", (SqlConstant)1.10m))
.Where(SqlExpression.Equal("Category", "Electronics"))
.ToSql();
Update using a table alias¶
Pass a SqlTable with an alias to Sql.Update():
SqlTable u = new("Users", "u");
var sql = Sql
.Update(u)
.Set("Email", "Email" + (SqlConstant)"updated@example.com")
.Where(SqlExpression.Equal(u + "Id", "UserId" + (SqlConstant)5))
.ToSql();
Update with compound WHERE¶
var sql = Sql
.Update("Users")
.Set("IsActive", false)
.Where(
SqlExpression.And(
SqlExpression.LessThan("LastLoginAt", "CutOff" + (SqlConstant)DateTime.UtcNow),
SqlExpression.Equal("IsActive", true)
)
)
.ToSql();
Update without WHERE¶
Omitting .Where() updates all rows in the table. Make sure that is the intent:
Warning
An UPDATE without a WHERE clause modifies every row in the table. Ensure this is intentional before executing.