Re: Stored Procedure to Delete Rows and Return Count

"Jonathan S. Katz" <[email protected]>
Newsgroups gmane.comp.db.postgresql.sql
Message-ID <[email protected]>
> On Aug 29, 2018, at 5:50 PM, Jonathan S. Katz <[email protected]> wrote:
> 
>> 
>> On Aug 29, 2018, at 5:46 PM, Dave Bolt <[email protected] <mailto:[email protected]>> wrote:
>> 
>> Can someone give me a clue how this can be done and confirm that their solution has been tested so I know it's my end that has the problem, or better yet tell me what I'm doing wrong.
> 
> Being able to use a DELETE in a CTE was introduced in PostgreSQL 9.1.

Borrowing from an old post[1] I made this little function:

    CREATE FUNCTION delete_stuff (y int)
    RETURNS int
    AS $$
        DECLARE
            deleted int;
        BEGIN
            DELETE FROM a WHERE x = y;
            GET DIAGNOSTICS deleted = ROW_COUNT;
            RETURN deleted;
        END
    $$ LANGUAGE plpgsql;

which returns the total # of rows deleted by the above query, which seems like
it could be adapted for your purposes.

Hope this helps,

Jonathan

[1] https://www.postgresql.org/message-id/20071002204841.GD19690%40alvh.no-ip.org
signature.asc (application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE-----

iQIzBAEBCAAdFiEE+oS2la8r95ogZD/x8QSccp8cZScFAluHFn4ACgkQ8QSccp8c
ZScC+RAAkTBBCiTOJjf6KnBwJ0vwB5q0TgYlSc0Gf63WqORqHtU+wiZNPxpXkxQY
ZpV4uLScmO0+/XbHOIhnGgnKoGvq+CnNnjlBhW6W2UjY2qQX0adPfnOboGZpYRWN
57iljNQoGsktLEnT0EOU3/zsmLgVRSSzNi6o5bgpIGr+qbaoffLxZBsZen1nPV/S
z6wNyieuil3bZc4kj3+giidSdsAFR/kYvXJTL/EO5ZlISlSN2BOiFuZWyQS37sqw
d3c0dQSCVQBTt/losJGyCYRTcCssIP9swvW8fBHCfA7Dc4lFsVNHur3KViQfdfjT
TiOPWlXuU5x2QFZGbFvCSO3NZg/HozWjt8BofWTIy5P7eUowzIfsJ9gnTbHnK/La
pW+BPEiWii8u3PKdQ3r4OmON+L113dzSyMXGAhcTpHtRc7YzujmsrK7N3cMRDylD
q+sMP5yVnpUSusNoWgmtmYO7NWjwPoN5p/rcEClnrMZdjYUUIywBleZjq8cYtI0+
wVffh2Tuda8XuViTGXLR6eP95ZLTdDJQLHCvqvLgpn+NVJ9l9OIHISMJy6RC8Zsm
7oEs828fmAhFv8NgFurSpYfWL2PLa85H8O3VVgmvFwjZdwW/VSRaSE4mGj93jU/k
qFO0JtlFdzfs5fzDNBWFjvLIJ+AgWbkD57QoUjV0zKJVhnnaeZE=
=DwVK
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.