vendredi 4 janvier 2019

How can I do a CASE WHEN or IF ELSIF in a FOR LOOP in Postgresql?

I would like to know if I can do a if-elsif or case-when inside a loop. No between the for-loop, but as a statament of the foor-loop to choose between a select or another. something like this:

I have tried with both, if-elsif and case-when....but none of them worked, and I have been lurking around the net to find something but nope.

CREATE OR REPLACE FUNCTION myfunct(op integer, -vars-)
RETURNS table(-vars-)
LANGUAGE plpgsql
AS $function$
DECLARE 
  selectop record;
  -vars-
BEGIN
  FOR selectop in (IF (op=1) THEN
                             SELECT * FROM mytab WHERE somevar=true;
                   ELSIF (op=2) THEN
                             SELECT * from mytab WHERE somevar=false;
                   END IF;)
      -things-
  RETURN NEXT;
  LOOP
    ---THINGS---
 END;
$function$

Aucun commentaire:

Enregistrer un commentaire