I've two tables, theta and convergence_table, each having same number of columns(7 columns here) (w0,w1,...w6; j0,j1,...j6). I need to update the 'w' values as wn=wn-jn. Here I'm updating the values of theta table with the help of a user-defined datatypes wt & jt(%rowtype).
select * into jt from convergence_table;
select * into wt from theta
update theta
set w0 = wt.w0-jt.j0, w1 = wt.w1-jt.j1, w2 = wt.w2-jt.j2,
w3 = wt.w3-jt.j3, w4 = wt.w4-jt.j4, w5 = wt.w5-jt.j5,
w6 = wt.w6-jt.j6;
But, now I've 'n+1' number of columns for both theta & convergence_table, so instead of writing the update statement and setting all the values from w0,w1,w2 .... wn. Is there a way to write a dynamic query to perform the updation for all the column values from w0 to wn.
I'm trying the following the code, but it isn't working ..
create or replace function sample(c int)
returns void as $$
declare jt convergence_table%rowtype; wt theta%rowtype;
query1 text:=''; query2 text:='';
begin
select * into jt from convergence_table;
select * into wt from theta;
for i in 0..n
loop
query1 := query1 ||'w'||i||' = '||
'wt.w'||i||' - jt.j'||i||',';
end loop;
query2 := trim(trailing ',' from query1);
query2 := 'update theta set '||query2||';';
execute query2;
return;
end;
$$ language plpgsql;
This gives me an error ...
ERROR: missing FROM-clause entry for table "wt" LINE 1: update theta set w0 = wt.w0 - jt.j0,w1 = wt.w1 - jt.j1,w2 = ... ^ QUERY: update theta set w0 = wt.w0 - jt.j0,w1 = wt.w1 - jt.j1,w2 = wt.w2 - jt.j2,w3 = wt.w3 - jt.j3,w4 = wt.w4 - jt.j4,w5 = wt.w5 - jt.j5,w6 = wt.w6 - jt.j6; CONTEXT: PL/pgSQL function sample(integer) line 20 at EXECUTE statement ********** Error ********** ERROR: missing FROM-clause entry for table "wt" SQL state: 42P01 Context: PL/pgSQL function sample(integer) line 20 at EXECUTE statement
Can anyone help out with this?