Spezifische Ausnahmen erfassen
Erstelle eine DO-Funktion, die erfasst, wenn glucose auf null gesetzt wird, und eine Meldung protokolliert, die ausdrücklich angibt, dass Glucose nicht null sein darf.
Diese Übung ist Teil des Kurses
Transaktionen und Fehlerbehandlung in PostgreSQL
Anleitung zur Übung
- Füge im
BEGIN-Block der DO-Funktion perINSERTinpatientsdie Zeile ein (a1c=7.5,glucose=nullundfasting=TRUE). - Füge eine
not_null_violation-Ausnahme hinzu, die im Fehlerfall"Glucose can not be null."in die Spalte detail vonerrorseinträgt.
Interaktive Übung
Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.
-- Make a DO function
DO $$
-- Open a transaction block
BEGIN
INSERT INTO patients (a1c, glucose, fasting)
VALUES (7.5, ___, TRUE);
-- Catch an Exception
EXCEPTION
-- Make it catch not_null_constraint exception types
WHEN ___ ___
-- Insert the proper msg and detail
INSERT INTO errors (___, detail)
VALUES ('failed to insert', '___');
END$$;
-- Select all the errors recorded
SELECT * FROM errors;