From: Laurenz Albe <laurenz.albe@cybertec.at>
To: hamann.w@t-online.de
To: pgsql-general@postgresql.org
Subject: Re: sql questions
Date: Fri, 20 Jul 2018 10:41:01 +0200
Message-ID: <1532076061.2610.3.camel@cybertec.at> (raw)
In-Reply-To: <wolfgang-1180720102638.A0132395@noten19.local>
References: <wolfgang-1180720102638.A0132395@noten19.local>
hamann.w@t-online.de wrote:
> a) I am running some select query
> select ... order by ....
> Now, I would like to preserver the ordering through further processing by adding a sequence number
> Of course I can do:
> create temp sequence mseq;
> select xx.*, nextval('mseq') as ord from (select ... order by ....) xx;
> drop sequence mseq;
> Is there a simpler way (avoiding the create/drop parts)
A window function would be the best thing:
SELECT ...,
row_number() OVER (ORDER BY ...)
FROM ...
> b) can a sql function return the count of affected rows of some query?
> create function merge_names(int, int) returns void as
> $_$
> update namelinks set nid = $2 where nid = $1;
> -- want the affected rows of the above query
> delete from names where nid = $1
> -- return result here
> $_$
> language sql;
You cannot do it in an SQL function.
In PL/pgSQL you can use
GET DIAGNOSTICS avariable = ROW_COUNT;
Yours,
Laurenz Albe
--
Cybertec | https://www.cybertec-postgresql.com
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: laurenz.albe@cybertec.at, hamann.w@t-online.de
Subject: Re: sql questions
In-Reply-To: <1532076061.2610.3.camel@cybertec.at>
References: <wolfgang-1180720102638.A0132395@noten19.local> <1532076061.2610.3.camel@cybertec.at>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox