I have a query inside a stored procedure that sums some values inside a table:
SELECT SUM(columnA) FROM my_table WHERE columnB = 1 INTO res;
After this select I subtract res
value with an integer retrieved by another query and return the result. If WHERE
clause is verified, all works fine. But if it's not, all my function returns is an empty column (maybe because I try to subtract a integer with an empty value).
How can I make my query return zero if the WHERE
clause is not satisfied?
INTO var
appended toSELECT ...
is only valid in PL/pgSQL code, not in plain SQL. I assume this is part of a PL/pgSQL function orDO
statement. Correct? – Upchurch