Database Reference
In-Depth Information
Now, the following example shows how the client program can abort a statement
that triggers an exception:
warehouse_db=# CREATE OR REPLACE FUNCTION how_to_raise()
RETURNS INTEGER AS $$
DECLARE
total INTEGER;
BEGIN
SELECT COUNT(*) INTO total FROM warehouse_tbl ;
IF (total > 0) THEN
RETURN total;
ELSIF (total = 0) THEN
--inform user that table is empty
RAISE NOTICE 'Table is empty. Currently, % , row', total;
-- insert a row in table
INSERT INTO warehouse_tbl
(
warehouse_id,
warehouse_name,
year_created,
street_address,
city,
state,
zip
)
VALUES
(
4,
'Bill & Co',
2014,
'Lilly Road',
'New London',
'CT',
4321
);
--raise exception level message and transaction will be
aborted as well.
RAISE EXCEPTION 'Transaction will be aborted';
END IF;
END;
$$ LANGUAGE plpgsql;
 
Search WWH ::




Custom Search