5

I get an error when I try to call a postgre function using dapper. Where did i do wrong? I'll be glad if you can help me.

Error Message:

 availability_list(facilityId => integer, startDate => timestamp without time zone, endDate => timestamp without time zone) does not exist"

Call postgre function using Dapper:

var func = "public.availability_list";

var result = db.Query<ReportResponse>(
            sql: func,
            param: new { facilityId = request.FacilityId, startDate = 
            DateTime.Now, endDate = DateTime.Now },
            commandType: CommandType.StoredProcedure, 
            commandTimeout: 900) as List<ReportResponse>;

My Postgre Function:

CREATE FUNCTION Availability_List(facilityId int, startDate date, endDate date)
RETURNS report_type[] 
AS 
$$

DECLARE
result_record report_type[];

BEGIN

result_record := array(  
SELECT...
);

RETURN result_record;

 END $$ LANGUAGE plpgsql;
1
  • I have the same issue, using the lowercase for parameters name, worked for me. Commented Nov 12, 2020 at 18:17

3 Answers 3

2

Try passing function parameters in lowercase, i.e.

var result = db.Query<ReportResponse>(
            sql: func,
            param: new { facilityid = request.FacilityId, startdate = 
            DateTime.Now, enddate = DateTime.Now },
            commandType: CommandType.StoredProcedure, 
            commandTimeout: 900) as List<ReportResponse>;

That worked for me.

Sign up to request clarification or add additional context in comments.

1 Comment

User lowercase for parameters name, that worked for me too.
1

I would expect you need to specify the parameters, schema of the function and the result should match. Likely following would work and after that you can replace the return type to see if it is mapped correctly.

var result = _connection.Query<dynamic>(
    "SELECT dbo.Availability_List(@facilityId, @startDate, @endDate)", 
    new { 
        facilityId = request.FacilityId, 
        startDate = DateTime.Now, 
        endDate = DateTime.Now 
    },
    commandType: CommandType.Text,
    commandTimeout: 900);

2 Comments

Hi Margus, i tried this format but i'm getting same error message. By the way, schema name is public. For this reason, i added public prefix.
Are you connected to the correct database and if you execute the statement in Sql manager then does it work? Have you granted permissions to execute this query for the user ex: learn.microsoft.com/en-us/sql/t-sql/statements/…
0

I think this happens because you created the function Availability_List where A and L are capitalized. But then you are calling the function with all lower case. I have faced this issue already, use all in a small case. That will be better...

Comments

Your Answer

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.