Variable containing the number of rows affected by previous DELETE? (in a function)

后端 未结 3 1066
猫巷女王i
猫巷女王i 2021-01-07 18:39

I have a function that is used as an INSERT trigger. This function deletes rows that would conflict with [the serial number in] the row being inserted. It works beautifully,

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-07 19:12

    For a very robust solution, that is part of PostgreSQL SQL and not just plpgsql you could also do the following:

    with a as (DELETE FROM feeds_item WHERE shareurl ~ re1 returning 1)
    select count(*) from a;
    

    You can actually get lots more information such as:

    with a as (delete from sales returning amount)
    select sum(amount) from a;
    

    to see totals, in this way you could get any aggregate and even group and filter it.

提交回复
热议问题