Postgresql – Create function with a parameter, which will become the part of IN (1, 2, 3) clause

parameterplpgsqlpostgresql

Following is the part of function declaration. Unfortunately it doesn't work the way I would like it to.

CREATE FUNCTION my_cool_func(some_parameter INTEGER, some_other_parameter INTEGER[])
RETURNS BOOLEAN
LANGUAGE 'plpgsql'
STABLE 

AS $BODY$

DECLARE count_entries INTEGER;

BEGIN

    SELECT COUNT(some_table.id) INTO count_entries FROM some_table
    WHERE ...
    AND some_table.some_integer_field IN (some_other_parameter);

        IF (count_entries > 0) THEN...

some_other_parameter is a list of integer values (1, 2, 3).

My questions:

  1. Is this function declaration correct?
  2. How do I define the default value for some_other_parameter?

Best Answer

Use = ANY instead of IN

AND some_table.some_integer_field = ANY (some_other_parameter);

The equivalent of NOT IN would be <> ALL (...)

To declare a parameter with a default value use:

CREATE FUNCTION my_cool_func(..., p_id_list INTEGER[] default array[42])