Official PostgreSQL 8.3 PL/pgSQL Documentation URL: http://www.postgresql.org/docs/8.3/static/plpgsql.html We cover only a subset of what we feel are the most useful constructs that we could squash in a single cheatsheet page commonly used 1 New in this release. |
|||||
FUNCTION CACHINGIMMUTABLE STABLE VOLATILE CONTROL FLOWFOR i IN 1 ... numtimes LOOP statements END LOOP; FOR i IN REVERSE numtimes ... 1 LOOP statements END LOOP; FOR var_e IN EXECUTE('somedynamicsql') LOOP statements RETURN NEXT var_e; END LOOP; FOR var_e IN somesql LOOP statements RETURN NEXT var_e; END LOOP; IF condition THEN : END IF; IF condition THEN : ELSE : END IF; IF condition. THEN : ELSIF condition THEN : ELSE : END IF; WHILE condition LOOP : END LOOP; LOOP -- some computations EXIT WHEN count > 100; CONTINUE WHEN count < 50; -- some computations for count IN [50 .. 100] END LOOP; |
RETURN constructsRETURN somevariable RETURN NEXT rowvariable RETURN QUERY 1 RAISE FAMILYRAISE DEBUG[1-5] RAISE EXCEPTION RAISE INFO RAISE LOG RAISE NOTICE EXCEPTION HandlingRAISE EXCEPTION 'Exception notice: %', var EXCEPTION WHEN condition THEN do something or leave blank to ignore END; COMMON States and ERROR constantsFOUND ROW_COUNT division_by_zero no_data_found too_many_rows unique_violation Variable SettingDECLARE somevar sometype := somevalue; somevar sometype curs1 refcursor; curs2 CURSOR FOR SELECT * FROM sometable; somevar := somevalue SELECT field1, field2 INTO somevar1, somevar2 FROM sometable WHERE .. LIMIT 1; Return typesRETURNS somedatatype RETURNS SETOF somedatatype RETURNS refcursor RETURNS trigger RETURNS void QUALIFIERSSECURITY DEFINER STRICT COST cost_metric 1 ROWS est_num_rows 1 |
||||
PLPGSQL FUNCTION SAMPLES
|