1

I tried this code:

CREATE OR REPLACE FUNCTION public.t4(in iid int, OUT n1 text, OUT n2 text)  
LANGUAGE plpgsql
AS $function$
    BEGIN 
            SELECT DISTINCT n1=l.name, n2=l.comment from public.languages l where l.id=iid;
    END;        
$function$;
 NpgsqlConnection connection = new NpgsqlConnection(ConString);
 connection.Open();
 NpgsqlCommand cmd = new NpgsqlCommand("SELECT * from public.t4()", connection);
 cmd.Parameters.Add(new NpgsqlParameter("iid", DbType.Int32) { Direction = ParameterDirection.Input });
 cmd.Parameters[0].Value = 18;
 cmd.Parameters.Add(new NpgsqlParameter("n1", DbType.String) { Direction = ParameterDirection.Output });
 cmd.Parameters.Add(new NpgsqlParameter("n2", DbType.String) { Direction = ParameterDirection.Output });
 cmd.ExecuteNonQuery();
 t1.Text = cmd.Parameters[1].Value.ToString();
 t2.Text = cmd.Parameters[2].Value.ToString();

And I got an error:

Npgsql.PostgresException: '42883: function public.t4() does not exist.

What am I doing wrong?

1
  • 4
    The message is correct function public.t4() does not exist, but public.t4(in iid int, OUT n1 text, OUT n2 text) does. I don't any placeholders in the query for the parameters you are trying to add. See here stackoverflow.com/questions/54691579/… fro examples. Commented yesterday

2 Answers 2

3

You need to pass parameters to your function invocation - see the Npgsql docs:

NpgsqlCommand cmd = new NpgsqlCommand("SELECT * from public.t4(@iid)", connection);

Otherwise your code tries to call a t4 function without input parameters and there is no t4 function accepting no input parameters.

Also you need to do is to fix the function itself:

CREATE OR REPLACE FUNCTION public.t4(in iid int, OUT n1 text, OUT n2 text)
    LANGUAGE plpgsql
AS $function$
BEGIN
    SELECT DISTINCT l.name, l.comment
    from public.languages l
    where l.id=iid
    into n1, n2; -- <--- HERE!
END;
$function$;
Sign up to request clarification or add additional context in comments.

Comments

0

You need to add all three parameters to the function call in SQL

using NpgsqlConnection connection = new NpgsqlConnection(ConString);
const string query = """
    SELECT * from public.t4(iid, n1, n2);
    """;
using NpgsqlCommand cmd = new NpgsqlCommand(query, connection);
cmd.Parameters.Add("iid", DbType.Int32).Value = 18;
cmd.Parameters.Add("n1", DbType.String).Direction = ParameterDirection.Output;
cmd.Parameters.Add("n2", DbType.String).Direction = ParameterDirection.Output;
conection.Open();
cmd.ExecuteNonQuery();

Note also the use of a raw query string which makes it easier to format, and using to dispose. Also consider using await and async functions.

1 Comment

"You need to add all three parameters to the function call in SQL" - only the in parameter is needed based on my tests (latest PG and Npgsql though)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.