title: postgresql insert inside loop is not working
body: <p>I am trying to insert some data to table based on id of product but insert with loop is not working or I did something wrong.</p>
  <p>In short what I am trying to do is to run a loop for each id from select and insert output to pre-created table.</p>
  <pre><code>    DO $$
  
  DECLARE
  idtow int;
  BEGIN
  FOR idtow IN (SELECT te.tel_ilosc as ilosc,te.ttw_idtowaru as idtow FROM tg_transelem AS te LEFT OUTER JOIN tg_transakcje tr ON ((tr.tr_idtrans = te.tr_idtrans)) WHERE (te.tel_datauklienta&gt;='2022-04-02' AND te.tel_datauklienta&lt;='2022-04-20') AND ((te.tel_flaga&amp;1024=0) AND (((te.tel_new2flaga&gt;&gt;15)&amp;63)!=14) AND (tr.tr_rodzaj in (30,251))))
  
  LOOP
  ``
  INSERT INTO  zapotrzebowanie_hc (WITH base as (SELECT 
  skr.idr AS fng_764972659,
  skr.lpall AS Drzewo,
  sk.sk_kod AS Kod,
  sk.sk_idstruktury AS idtowaru,
  sk.sk_nazwa AS nazwa,
  skr.iloscl::float AS ilość,
  sk.sk_flaga&amp;(12) AS sk_1541782576,
  twsk.tjn_idjedn AS jednostka,
  sk.sk_flaga AS rodzaj,
  skr.addr as addr,
  skr.addrp as addrp
  FROM tr_strukturakonstrukcyjna AS sk  
  JOIN (WITH RECURSIVE t (id,addr,addrp,idr,idp,ilosc,iloscl,ilosc_l,ilosc_m,ilosc_s,lpall,lp) AS (SELECT idtow,'idtow'::text,''::text,'238:'||(idtow)::text,NULL::text,1::numeric,NULL::numeric,NULL::numeric,NULL::numeric,NULL::numeric,NULL::TEXT,NULL::INT  UNION SELECT r.sk_idstrukturyc,t.addr||'|'||r.sk_idstrukturyc,t.addr,'257:'||r.skr_idrelacji,t.idr,getIloscWgStruktury(r,t.ilosc,4), (r.skr_ilosc_licznik/r.skr_ilosc_mianownik), r.skr_ilosc_licznik, r.skr_ilosc_mianownik, r.skr_ilosc_skladnik, (CASE WHEN t.lpall IS NOT NULL THEN  t.lpall||'-' ELSE '' END)||r.skr_lp,  r.skr_lp FROM tr_strukturakonstrukcyjnarel AS r JOIN t ON (t.id=r.sk_idstrukturyp))SELECT * FROM t) AS skr ON (sk.sk_idstruktury=skr.id)  
  LEFT OUTER JOIN tg_towary AS twsk ON ((twsk.ttw_idtowaru=sk.ttw_idtowaru)) WHERE skr.iloscl IS NOT NULL ORDER BY COALESCE(skr.addr,NULL) DESC)
  SELECT (SELECT ttw_klucz from tg_towary tg JOIN tr_strukturakonstrukcyjna tr USING (ttw_idtowaru) where tr.sk_idstruktury = idtowaru) as code_of_product, SUM(mult) as zapotrzebowanie_hc
  FROM (
  SELECT  t.idtowaru,addr,mul((SELECT ilość FROM base b where b.idtowaru = parts::int and t.addr ilike b.addr||'%')) as mult --mul(ilość::numeric) 
  from(
  SELECT unnest(
    string_to_array(addr, '|')
  ) AS parts,idtowaru,addr FROM base ) t
  GROUP BY 1,2) f
  GROUP BY 1);
  END LOOP;
  END$$;
  SELECT * from zapotrzebowanie_hc
  </code></pre>
  <p>It dosen't insert a thing. Tried to make it manually for specific id and works just fine.
  PS. Sorry for messy code was quite in hurry.</p>