Search code examples
sqlpostgresqlselectpostgresql-9.2select-into

ERROR: "coalesce" is not a known variable in INTO clause


Following is my sample function

create or replace function samp(in a int) returns int as
$$
declare 
val int;
val1 int;
begin
select coalesce(a-1,1) into val,coalesce(a-2,1) into val1;
return val + val1;
end;
$$
language plpgsql 

when executing it I get following error

ERROR: "coalesce" is not a known variable LINE 7: select coalesce(a-1,1) into val,coalesce(a-2,1) into val1;


Solution

  • Your syntax is wrong - into is a single clause that applies to all the variables, not a keyword that should be applied to each variable:

    SELECT coalesce(a-1,1), coalesce(a-2,1)
    INTO   val, val1;