I have a table in PostgreSQL, table1, with 5 columns (col_a, col_b, col_c, col_d, col_e). col_a is timestamp type and col_b is integer_type. I want to make a function with two parameters that returns count of col_b.
CREATE or REPLACE FUNCTION first_count (year schema.table1.col_a%type, col_b schema.table1.col_b%type)
RETURNS void AS
$$
DECLARE
contador integer;
BEGIN
SELECT COUNT(col_b) INTO contador
FROM schema.table1
WHERE schema.table1.col_b = col_b AND EXTRACT(year FROM
schema.table1.col_a) = year;
END;
$$
LANGUAGE plpgsql;
This works but when I try to pass parameters it gives me an error.
My aim with function is get the result like this query:
SELECT COUNT(col_b)
FROM schema.table1
WHERE col_b = 2216 AND EXTRACT(year FROM schema.table1.col_a) = 2009;
SELECT ... INTO variablein PL/pgSQL.INTOis spelled out here INTO Look at the example function at the bottom of the section(ignore theSTRICTportion). If that does not make sense, show us in your question what you attempted to do.