0

I wrote a Postgresql function to insert a row and return the value generated from the identity column. I receive the following exception when trying to call it from C#.

Npgsql.PostgresException: '42601: query has no destination for result data'

I have looked around for an answer, and it seems ExecuteScalar has worked for others, but in all the examples I've seen, it's usually when using RETURN QUERY, not a local variable. What am I missing?

Here is the function:

CREATE OR REPLACE FUNCTION public.func_insert_item(_name character varying)
 RETURNS BIGINT
 LANGUAGE plpgsql
AS $function$
DECLARE
 _item_id BIGINT;
BEGIN
 INSERT INTO public.items
 (
 name
 )
 VALUES
 (
 _name
 )
 RETURNING _item_id;
 
 RETURN _item_id;
END;
$function$

Here is the C#:


 static NpgsqlParameter CreateParameter(string name, ParameterDirection direction, string value)
 {
 var parameter = new NpgsqlParameter(name, NpgsqlTypes.NpgsqlDbType.Varchar, value.Length);
 parameter.Direction = direction;
 parameter.Value = value;
 return parameter;
 }
 static void Main(string[] args)
 {
 using var connection = new NpgsqlConnection(connectionString.ToString());
 connection.Open();
 
 using var command = new NpgsqlCommand("func_insert_item", connection);
 command.CommandType = CommandType.StoredProcedure;
 command.Parameters.Add(CreateParameter("_name", ParameterDirection.Input, name));
 object itemId = command.ExecuteScalar();
 }
asked Feb 13, 2021 at 4:43

1 Answer 1

2

It seems like you misunderstood the RETURNING clause. It should list the columns the statement should return, not the names of the variables the returned values should go into. You need an additional INTO clause for the latter.

CREATE
 OR REPLACE FUNCTION public.func_insert_item(_name character varying)
 RETURNS bigint
 LANGUAGE plpgsql
AS
$$
DECLARE
 _item_id bigint;
BEGIN
 INSERT INTO public.items
 (name)
 VALUES (_name)
 RETURNING <name of the id column>
 INTO _item_id;
 
 RETURN _item_id;
END;
$$

Unfortunately you didn't provide the table's DDL, so I don't know what column could be the ID you want to return. Replace <name of the id column> with its name.

answered Feb 13, 2021 at 6:04
1
  • That was it! Thank you! Commented Feb 13, 2021 at 16:27

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.