'How to call a function a in plpgsql which has a return data type of void?

create or replace function f1() //procedure should display 9
returns void
as $$
declare age int default 9; //variable declaration
begin                     
select age;      //prints 9
end;
$$ language plpgsql;
CREATE FUNCTION

I keep receiving this error

perform f1();
ERROR:  syntax error at or near "perform"
LINE 1: perform f1();


Solution 1:[1]

perform is a PL/pgSQL statement. In plain SQL, you simply use SELECT:

select f1();

Note that the function as written, will not "print 9" - it will result in an error as the result of a SELECT needs to be stored somewhere. In PL/pgSQL you would need the RAISE statement to "print" something:

create or replace function f1()
returns void
as $$
declare
  age int default 9; 
begin                     
  raise 'Age: ', age; -- this prints 9
end;
$$ 
language plpgsql;

If you want a function to "display" something, it might make more sense to let the function return a result:

create or replace function f1()
returns int
as $$
declare
  age int default 9; 
begin                     
  return age;
end;
$$ 
language plpgsql;

Then select f1() will "print" 9

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 a_horse_with_no_name