Capturar excepciones específicas
Vamos a crear una función DO que capture cuando glucose se establece en null y registre un mensaje indicando explícitamente que Glucose no puede ser null.
Este ejercicio forma parte del curso
Transacciones y manejo de errores en PostgreSQL
Instrucciones del ejercicio
- Dentro del bloque
BEGINde la función DO, haz unINSERTenpatientsde la fila (a1c=7.5,glucose=nullyfasting=TRUE). - Añade una excepción
not_null_violationque inserte"Glucose can not be null."en la columna detail deerrorsen caso de error.
Ejercicio interactivo práctico
Prueba este ejercicio y completa el código de muestra.
-- 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;