Issue
Given the following skeleton pl/pgsql function, how can I make it return true if the update was successful (something was actually updated) and false otherwise?
CREATE FUNCTION UpdateThingy(
/* input parameters */
) RETURNS BOOLEAN AS $$
BEGIN
UPDATE thingies SET /* blah blah */ WHERE /* blah blah */;
RETURN true;
END;
$$ LANGUAGE plpgsql;
Currently this will always return true, even if no rows where updated. I want to return false in the case of no rows being affected by the update.
Solution
After the UPDATE, you can obtain the number of updated rows using GET DIAGNOSTICS or the FOUND status variable as described in the manual:
http://www.postgresql.org/docs/current/static/plpgsql-statements.html#PLPGSQL-STATEMENTS-DIAGNOSTICS
Answered By - a_horse_with_no_name Answer Checked By - Candace Johnson (sql.easybugfix Volunteer)
Post a Comment
Post a Comment