Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute raw SQL using ServiceStack.OrmLite

I am working ServiceStack.OrmLite using MS SQL Server. I would like to execute raw SQL against database but original documentation contains description of how to do it with SELECT statement only. That is not enough for me.

I cant find the way to run anything as simple as that:

UPDATE table1
SET column1 = 'value1' 
WHERE column2 = value2

Using, for example:

var two = db.Update(@"UPDATE table1
    SET column1 = 'value1' 
    WHERE column2 = value2");

Running this expressions with db.Update() or db.Update<> produces uncomprehensive errors like

Incorrect syntax near the keyword 'UPDATE'.

I would like to use raw sql because my real UPDATE expression uses JOIN.

like image 411
y.selivonchyk Avatar asked Mar 24 '23 06:03

y.selivonchyk


1 Answers

db.Update is for updating a model or partial model as shown in OrmLite's Documentation on Update. You can choose to use the loose-typed API to build your update statement, e.g:

db.Update(table: "table1", 
  set: "column1 = {0}".Params("value1"), 
  where: "column2 = {0}".Params("value2"));

The Params extension method escapes your values for you.

Otherwise the way to execute any arbitrary raw sql is to use db.ExecuteSql().

like image 152
mythz Avatar answered Mar 25 '23 21:03

mythz