Skip to content

Instantly share code, notes, and snippets.

@docteurklein
Created January 18, 2021 16:00
Show Gist options
  • Select an option

  • Save docteurklein/d43d6e63dc7ce10bcee827fc97bf564d to your computer and use it in GitHub Desktop.

Select an option

Save docteurklein/d43d6e63dc7ce10bcee827fc97bf564d to your computer and use it in GitHub Desktop.

Revisions

  1. docteurklein created this gist Jan 18, 2021.
    17 changes: 17 additions & 0 deletions dynamic.sql
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    drop function if exists exec(text, text[]);
    create function exec(sql text, params text[] default '{}') returns void
    language 'plpgsql' as $$
    begin
    execute format(sql, variadic params);
    end
    $$;
    -- select exec('select 1');

    drop function if exists query(text, text[]);
    create function query(sql text, params text[] default '{}') returns setof record
    language 'plpgsql' as $$
    begin
    return query execute format(sql, variadic params);
    end
    $$;
    -- select * from generate_series(1, 10) i, query('select generate_series(%s, %s::int + 10)', array[i::text, i::text]) as g(ii int);