
CREATE OR REPLACE FUNCTION tcg.public.FN_Error_Get_Many (
      a_guid UUID
)
RETURNS TABLE (
      error_id INT
	, guid UUID
    , error_type_id INT
    , error_type_code TEXT
    , error_type_name TEXT
    , message TEXT
    , display_order INT
    , active BOOLEAN
    , created_on TIMESTAMP
    , created_by_user_id INT
    , updated_last_on TIMESTAMP
    , updated_last_by_user_id INT
    , change_set_id INT
)
LANGUAGE plpgsql
AS $$
BEGIN
    IF a_guid IS NULL THEN
        RAISE EXCEPTION 'GUID required.';
    END IF;

	-- Outputs
    RETURN QUERY SELECT
          ERROR.error_id
		, ERROR.guid AS guid
        , ERROR.error_type_id
        , ERROR_TYPE.code AS error_type_code
        , ERROR_TYPE.name AS error_type_name
        , ERROR.message
        , ERROR.display_order
        , ERROR.active
        , ERROR.created_on
        , ERROR.created_by_user_id
        , ERROR.updated_last_on
        , ERROR.updated_last_by_user_id
        , ERROR.change_set_id
    FROM tcg.public.Error ERROR
    LEFT JOIN tcg.public.Error_Type ERROR_TYPE ON ERROR.error_type_id = ERROR_TYPE.error_type_id
    WHERE ERROR.guid = a_guid
    ORDER BY COALESCE(ERROR.display_order, ERROR.error_id)
    ;
END;
$$;



-- Call it with:
SELECT *
FROM tcg.public.FN_Error_Get_Many (
      a_guid := '9a117327-63cc-49a7-88e7-54176f30be84'::UUID
)
;

/*


-- Call it with:
DO $$
DECLARE
    v_guid UUID := CAST('9a117327-63cc-49a7-88e7-54176f30be84' AS UUID);
BEGIN

	
	SELECT *
	FROM tcg.public.FN_Error_Get_Many (
	      a_guid := v_guid
	)
	;
END;
$$;
SELECT *
FROM tcg.public.Error
;
*/
