CREATE OR REPLACE FUNCTION schema.name_funcion()
RETURNS jsonb
LANGUAGE 'plpgsql'
COST 100
VOLATILE
AS $BODY$
DECLARE
-- Constantes
MSJ_SUCCESS CONSTANT CHARACTER VARYING DEFAULT 'Se registró';
MSJ_ERROR CONSTANT CHARACTER VARYING DEFAULT 'Hubo un error';
-- Variables
__result JSONB;
__msj_excep CHARACTER VARYING;
BEGIN
-- Respuesta
__result := JSONB_BUILD_OBJECT(
'msj' , MSJ_SUCCESS
);
RETURN __result;
EXCEPTION
WHEN SQLSTATE 'SMILE' THEN
__result = JSONB_BUILD_OBJECT('status', 400, 'msj' , SQLERRM);
RETURN __result;
WHEN OTHERS THEN
GET STACKED DIAGNOSTICS __msj_excep = PG_EXCEPTION_CONTEXT;
__result = JSONB_BUILD_OBJECT('status', 500, 'msj' , MSJ_ERROR, 'err', CONCAT(SQLERRM,' - ',__msj_excep));
RETURN __result;
END;
$BODY$;
Create function basic for postgresql
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.