如何在函数中选择受影响的行数作为变量?

use*_*521 5 postgresql

create or replace function test()
returns void as $$
begin
  update tbl set col1 = true where col2 = false;
  -- now I want to raise exception if update query affected more than 2 rows to rollback the update
end;
$$ language plpgsql;
Run Code Online (Sandbox Code Playgroud)

如何在函数中为变量选择受影响的行数?

Pat*_*ck7 10

create or replace function test()
returns void as $$
declare
    v_cnt numeric;
begin
    v_cnt := 0;

        update tbl set col1 = true where col2 = false;
        GET DIAGNOSTICS v_cnt = ROW_COUNT;

        if v_cnt > 1 then
           RAISE EXCEPTION 'more than one row affected --> %', v_cnt; 
        end if;
end;
$$ language plpgsql;
Run Code Online (Sandbox Code Playgroud)