Search code examples
sqlpetapoco

How can I use @ in PetaPoco queries


I am trying to use @@ROWCOUNT for an execute query in PetaPoco.

Problem is, it consider @@ROWCOUNT like "@ROWCOUNT" parameter because of the @.

Must declare the scalar variable "@ROWCOUNT".

Here is the execute query code

this.Execute(@"
            UPDATE  myTable
            SET     foo = @2
            WHERE   bar = @0
                AND bor = @1

            IF  @@ROWCOUNT=0
                INSERT INTO myTable
                (bar, bor, foo)
                VALUES
                (@0, @1, @2)", myItem.bar
                                , myItem.bor
                                , myItem.foo);

I've searched to find if there was any specific syntax to use for PetaPoco SQL query, but I can't find any information about using @ for something else then parameters.
If there is no way to do it, is there any ways I can do an insert if the update item doesn't exist? (I'd rather avoid having to use a SELECT to see if the entry exist)


Solution

  • You can double escape the @@ like this. @@@ROWCOUNT.

    You can see this technique used in the PetaPoco.cs file in the ExecuteInsert method.

    public override object ExecuteInsert(
        Database db, 
        System.Data.IDbCommand cmd, 
        string PrimaryKeyName)
    {
        db.ExecuteNonQueryHelper(cmd);
        return db.ExecuteScalar<object>("SELECT @@@IDENTITY AS NewID;");
    }