If you replace $SELECTION$
with ( $SELECTION$ ) as t
it should also work when your selection is a complete query.
I don't use pgAdmin, but my SQL client supports the same replacement technique and I use the same kind of macro there.
Pergunta
In pgAdmin, you can define your own macro with '$SELECTION$'
placeholder. I have a macro to count number of rows in table:
select count(*) as rows_in_table from $SELECTION$;
This works well, but as I write a lot of queries at work I tought it would be nice to have macro to count rows for any select statement. The only way I found is to create function where two different situation are handled - only table name is selected or query is selected:
create or replace function sys.number_of_rows(in sql text)
returns table (number_of_rows bigint) as
$body$
begin
sql:=
case
when position(' ' in trim(sql)) = 0 then 'select count(*) from ' || sql
else 'select count(*) from (' || sql || ') a ' end;
return query execute sql;
end;
$body$
language plpgsql;
Now with macro select sys.number_of_rows('$SELECTION$');
I can pass either table name or whole query.
This works perfectly, but only when the function sys.number_of_rows
exists in database. Any idea how to achieve the same functionality without being dependent on function?
Solução
If you replace $SELECTION$
with ( $SELECTION$ ) as t
it should also work when your selection is a complete query.
I don't use pgAdmin, but my SQL client supports the same replacement technique and I use the same kind of macro there.