Skip to content

Instantly share code, notes, and snippets.

@joshlk
Last active September 6, 2018 16:11
Show Gist options
  • Save joshlk/40bac1f7e9441f55ac9aaa2bf1902c24 to your computer and use it in GitHub Desktop.
Save joshlk/40bac1f7e9441f55ac9aaa2bf1902c24 to your computer and use it in GitHub Desktop.
Using a variable for input and output table in Postgres SQL pl/pgsql
CREATE OR REPLACE FUNCTION query(
in_tb character varying,
out_tb character varying
)
RETURNS integer
LANGUAGE plpgsql
AS $function$
BEGIN
EXECUTE
'CREATE TABLE '
|| out_tb
|| ' AS SELECT * FROM '
|| in_tb
|| ' LIMIT 10';
RETURN 1;
END
$function$;
select query('input_table', 'output_table');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment