C# pass parameters to Postgres Npgsqlcommand DO block

Viewed 1015

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

0 Answers
Related