I'm writing a function which does the following:
Create a temporary table with a single field. This field is the result of the sum of up to 5 variables from a specific table.
Let's say I have the following table:
create table src (x1 numeric, x2 numeric);
insert into src values (2,1),(5,2),(10,4);
My code is:
create or replace function qwert(cod numeric, v1 numeric default 0
, v2 numeric default 0, v3 numeric default 0, v4 numeric default 0,
v5 numeric default 0)
returns numeric as
$func$
declare vv numeric;
begin
vv = v1+v2+v3+v4+v5;
execute '
drop table if exists t' || cod || ';
create temporary table t' || cod || ' as
select ' || vv || ' ;'
;
return vv;
end
$func$ language plpgsql;
If I run: select qwert(1, x1,x2) from src;
The expected result is a table t1:
column1
---------
3
7
14
(3 rows)
Instead the result is:
db1=# select * from t1;
?column?
----------
14
(1 row)
In my code, line: return vv; is only there to check whether vv was being created correctly.
Could someone help you this?