title: PostgreSQL stored procedure does not allow to use SELECT INTO and throws error "INTO used with a command that cannot return data"
body: <p>This stored procedure simply inserts some values in a table, but it is giving me a real pain when I try to return a type just to show the inserted values. I have declared a variable (report_tab) and the type (report_building_style_type) I want to return, but if I try to fill it via SELECT INTO (no matter in which select I try it) it would throw the &quot;Error: INTO used with a command that cannot return data&quot;.</p>
  <p>I have tried to figure out what this means and how to solve it, even checked some previus stackoverflow related questions, but cannot actually find the answer.</p>
  <p>This is my code so far:</p>
  <pre><code>CREATE OR REPLACE FUNCTION update_report_building_style(p_year integer)
  RETURNS report_building_style_type
  AS $$
  DECLARE
  found_bld building.id_building%TYPE;
  report_tab report_building_style_type;
  BEGIN
  FOR found_bld IN
  SELECT m.id_building into report_tab.t_id_building
  FROM building m
  WHERE EXTRACT(YEAR FROM m.birth) = p_year
  LOOP
  INSERT INTO report_building_style
  SELECT INTO 
  report_tab.t_id_building,
  report_tab.t_ style ,
  report_tab.t_ num_firms,
  report_tab.t_ stages,
  report_tab.t_average_building_height
  DISTINCT 
  T1.id_building, 
  T1.style, 
  T2.num_firms, 
  T3.num_stages, 
  T4.num_built_buildings, 
  T5.average_building_height
  FROM 
  (SELECT 
  x0.id_building, 
  firm.style
  FROM firm, 
  (SELECT building.name, building.id_building, id_firm 
  FROM building 
  INNER JOIN member
  ON building.id_building = member.id_building
  WHERE building.id_building = found_bld
  GROUP BY building.id_building, building.name, id_firm) as x0
  WHERE firm.id_firm = x0.id_firm
  GROUP BY x0.id_building, x0.name, x0.id_firm, firm.style) AS T1
  INNER JOIN 
  (SELECT x1.id_building, x1.name, COUNT(*) AS num_firms 
  FROM (SELECT building.name, building.id_building, id_firm
  FROM building
  INNER JOIN member
  ON building.id_building = member.id_building
  WHERE building.id_building = found_bld
  GROUP BY member.id_firm, building.id_building, building.name) as x1
  GROUP BY x1.id_building, x1.name) AS T2
  ON T1.id_building = T2.id_building
  INNER JOIN 
  (SELECT x2.id_building, x2.name, COUNT(*) AS num_stages
  FROM (SELECT building.name, building.id_building, member.id_firm
  FROM building
  INNER JOIN member
  ON building.id_building = member.id_building
  INNER JOIN stage
  ON stage.id_firm = member.id_firm
  WHERE building.id_building = found_bld   
  GROUP BY stage.id_stage, member.id_firm, building.id_building, building.name) as x2
  GROUP BY x2.id_building, x2.name
  ORDER BY x2.id_building) AS T3
  ON T3.id_building = T2.id_building
  INNER JOIN 
  (SELECT x3.id_building, x3.name, COUNT(*) AS num_built_buildings
  FROM (SELECT building.name, building.id_building
  FROM building
  INNER JOIN architect
  ON architect.id_building = building.id_building
  WHERE building.id_building = found_bld   
  GROUP BY architect.id_building, building.id_building, building.name) as x3
  GROUP BY x3.id_building, x3.name
  ORDER BY x3.id_building) AS T4
  ON T4.id_building = T3.id_building
  INNER JOIN 
  (select b.id_building, SUM (b.height)/COUNT (*) AS average_building_height 
  from (SELECT building.name, building.id_building, building.height, SUM(building.height) AS building_height
  FROM building
  INNER JOIN architect
  ON architect.id_building = building.id_building
  INNER JOIN building
  ON building.id_building = architect.id_building
  WHERE building.id_building = found_bld  
  GROUP BY building.id_building, building.name, building.duration, architect.id_building
  ORDER BY building.id_building) as b
  group by id_building) AS T5 
  ON T5.id_building = T3.id_building
  ORDER BY T1.id_building
  ON CONFLICT (id_building, style) 
  DO UPDATE SET 
  style = excluded.style,
  num_firms = excluded.num_firms,
  num_stages = excluded.num_stages,
  num_built_buildings = excluded.num_built_buildings,
  average_building_height = excluded.average_building_height;
  return report_tab;
  END LOOP;
  END;
  $$LANGUAGE plpgsql;
  </code></pre>
  <p>Many thanks in advance.</p>