Re: Row Count - Mailing list pgsql-novice

From Sergey Konoplev
Subject Re: Row Count
Date
Msg-id CAL_0b1tZ50rp8ms5LOiS8wnDSXpCUCJ22JKxKT=6M5Yp=u_NVg@mail.gmail.com
Whole thread Raw
In response to Row Count  (Sergey Gigoyan <sergey.gigoyan@gmail.com>)
List pgsql-novice
On Mon, Feb 11, 2013 at 11:05 PM, Sergey Gigoyan
<sergey.gigoyan@gmail.com> wrote:
> How can I get selected row count in one statement?

Usually it is immediately available after statement execution without
additional query.

For example in PL/PgSQL you can use GET DIAGNOSTICS straight after your SELECT:

SELECT INTO _id, _name, _address id, name, address
FROM users WHERE name <> 'examplename';

GET DIAGNOSTICS _row_count = ROW_COUNT;

In external languages like Perl:

$sth = $dbh->prepare(
    "select id, name, address, rowcount as rowcount".
    "from users where name<>'examplename'");

$row_count = $sth->execute(...);

> For example, I can do that by two select statements
>
> select id, name, addrress, (select count(*) from(select id
>                                                                      from
> users
>                                                                       where
> name<>'examplename')
> from users
> where name<>'examplename'
>
> or
>
> declare rowcount integer
> ...............
> rowcount:=select count(*) from(select id
>                                                from users
>                                                 where name<>'examplename')
> as rst
>
> select id, name, address, rowcount as rowcount
> from users
> where name<>'examplename'
>



--
Sergey Konoplev
Database and Software Architect
http://www.linkedin.com/in/grayhemp

Phones:
USA +1 415 867 9984
Russia, Moscow +7 901 903 0499
Russia, Krasnodar +7 988 888 1979

Skype: gray-hemp
Jabber: gray.ru@gmail.com


pgsql-novice by date:

Previous
From: Andreas Kretschmer
Date:
Subject: Re: Row Count
Next
From: Thomas Kellerer
Date:
Subject: Re: Row Count