title: Nested for loops in postgresql
body: <p>How can I nest loops in postgresql?
  I have to insert into a table some data based on another table</p>
  
  <pre><code>DO $$
  DECLARE productAdminsCursor CURSOR IS 
      SELECT * 
      FROM public.actor_groups 
      WHERE actor_groups.type = 'PRODUCT_ADMIN';
  
  DECLARE actorsId INTEGER;
  
  BEGIN FOR productadmin IN productAdminsCursor LOOP
  
     actorsId := (SELECT actor_id 
                  FROM public.users 
                  WHERE users.super_admin = TRUE 
                  AND users.product_id = productadmin.product_id);
  
    IF actorsId NOTNULL
    THEN
      INSERT INTO public.data_permission 
      (object_type, object_id, permission_level, id_based_on_permission_level, permission_flag)
      VALUES ('ACTOR_GROUP', productadmin.key,
              'ACTOR', (SELECT key 
                        FROM public.actors 
                        WHERE actors.id = actorsId),
              63); -- super admin can everything on product admin
    END IF;
  
  END LOOP;
  END;
  $$;
  </code></pre>
  
  <p>But if select returning value into <code>actorsId</code> returns more than one row, it crashes with "more than one row returned by a subquery used as an expression".</p>