5

I get the error "42883: operator does not exist: integer =@ integer" Npgsql.PostgresException when trying to pass parameters to a DO block:

var cmd = new NpgsqlCommand();
cmd.CommandText = 
@"DO $$ 
BEGIN
IF EXISTS (SELECT id FROM pub.table1 WHERE id = @id) THEN
    UPDATE pub.table1 
    SET Field1 = @Field1
       ,Field2 = @Field2
    WHERE id = @id;
ELSE
    INSERT INTO pub.table1 (id, Field1, Field2) 
    VALUES (@id, @Field1, @Field2);
END IF;

END $$;";

cmd.Parameters.AddWithValue("@id", 1);
cmd.Parameters.AddWithValue("@Field1", "text");
cmd.Parameters.AddWithValue("@Field2", "text2");

Otherwise connection to postgres works and also (classic) queries work when passing parameters; e.g.:

cmd.CommandText = @"SELECT * FROM pub.table1 WHERE id = @id;";

Is it not possible to pass parameters to a "DO" block or am I missing something?

Thank you. M

TheMixy
  • 1,049
  • 15
  • 36
  • Possible duplicate of [Dapper parameters not working with PostgreSQL through npgsql connection, is postgres anonymous function parameterization supported?](https://stackoverflow.com/questions/31439352/dapper-parameters-not-working-with-postgresql-through-npgsql-connection-is-post) – Ben Collins May 07 '19 at 19:18

0 Answers0