Skip to content

Instantly share code, notes, and snippets.

@malkab
Last active December 22, 2020 09:38
Show Gist options
  • Save malkab/889889b7255fed7516a82da3250ff663 to your computer and use it in GitHub Desktop.
Save malkab/889889b7255fed7516a82da3250ff663 to your computer and use it in GitHub Desktop.
PostgreSQL - Variables in SQL Scripts
/**
Usage and expansion of variables in SQL scripts to be executed with psql \i command.
*/
-- Expansion with quotes as varchar variables example
\set analysis_id a
delete from warehouse.analysis_tasks where analysis_id = :'analysis_id';
delete from warehouse.properties where analysis_id = :'analysis_id';
delete from sunnsaas.analysis where analysis_id = :'analysis_id';
-- Use in SQL commands body example
\set myvariable value
select * from :myvariable.table1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment