0

I'm trying to run this command according the documentation but it always returns 1, i.e. not the number of rows that I want

CREATE FUNCTION getQuantity() RETURNS integer $$
   select count(*) from table;
$$ LANGUAGE SQL;

select getQuantity();

Does anyone know if I'm doing something wrong?

2
  • You are not returning anything. Where's your RETURN statement? Commented May 24, 2017 at 18:13
  • @WEI_DBA you mean put return select count(*)... or save the select statement in a variable and return it? Commented May 24, 2017 at 18:24

1 Answer 1

1

You missed a keyword as. Also, count(*) returns bigint, so:

create function get_quantity() 
returns bigint as $$
   select count(*) from my_table;
$$ language sql;

or

create function get_quantity() 
returns integer as $$
   select count(*)::int from my_table;
$$ language sql;

Test it in dbfiddle.

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

3 Comments

Still return one yet, I try both
Both functions work as expected, see the link in the modified answer. Maybe you should drop an old version of the function: drop function get_quatnity().
Yeah, after I drop it worked. It's something like cache in browser

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.