PL/PgSQL

  • Inline SQL
create or replace function test(int) returns int as '
declare
alias for $1;
begin
insert into a values(i);
return i;
end;' language plpgsql;
  • Control structures
    • IF ... THEN ... [ELSE IF | ELSE [... THEN ... ] END IF;
DECLARE
i ALIAS FOR $1;
BEGIN
IF  i > 10 THEN
	INSERT INTO a values(i);
ELSE
	INSERT INTO b values(i);
END IF;
END;' ...
    • LOOP ... END LOOP;
    • Unconditional loop
LOOP
IF i > 10 THEN
	EXIT;
END IF;
...
END LOOP;
    • WHILE expression LOOP ... END LOOP;
WHILE i < 10 LOOP
...
END LOOP;
    • FOR var IN expression..expression LOOP ... END LOOP;
    • FOR record | row IN SELECT ... LOOP ... END LOOP;
FOR r IN SELECT * FROM a LOOP
	IF r.i > 10 THEN
		RAISE NOTICE ''Found i greater than 10'';
	END IF;
END LOOP;
Prev

Next

Page 33