Skip to content

Instantly share code, notes, and snippets.

@sanjaypojo
Created August 2, 2017 19:05
Show Gist options
  • Select an option

  • Save sanjaypojo/e10054041d9ac45316731915b78701b1 to your computer and use it in GitHub Desktop.

Select an option

Save sanjaypojo/e10054041d9ac45316731915b78701b1 to your computer and use it in GitHub Desktop.
Postgres Stored Function Template (plpgsql)
DROP FUNCTION IF EXISTS my_stored_function(integer, varchar, varchar );
CREATE OR REPLACE FUNCTION table_space.my_stored_function (var1 integer, var2 varchar, var3 varchar)
RETURNS TABLE (col1 integer, col2 integer, col3 varchar, col4 varchar)
AS
$BODY$
DECLARE
local_variable integer;
BEGIN
SELECT average(age)
INTO local_variable
FROM some_table
WHERE country = 'USA';
RETURN QUERY
SELECT name, income
FROM some_other_table
WHERE age < local_variable;
END;
$BODY$
LANGUAGE plpgsql VOLATILE;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment