CREATE OR REPLACE FUNCTION get_millesime_data(param text)
RETURNS TABLE (millesime text) AS
$$
BEGIN
RETURN QUERY
SELECT req.millesime
FROM (
SELECT
CASE
WHEN table_name ILIKE 'commune_%' THEN 'cog'
WHEN table_name ILIKE 'car_200m_%' THEN 'recensement'
WHEN table_name ILIKE 'infracom_%' THEN 'fisc'
WHEN table_name ILIKE 'route_%' THEN 'osm'
WHEN table_name ILIKE 'etablissement_%' THEN 'sirene'
END donnees,
CASE
WHEN table_name ILIKE 'commune_%' THEN RIGHT(table_name, 4)
WHEN table_name ILIKE 'car_200m_%' THEN RIGHT(table_name, 4)
WHEN table_name ILIKE 'infracom_%' THEN RIGHT(table_name, 4)
WHEN table_name ILIKE 'route_%' THEN RIGHT(table_name, 8)
WHEN table_name ILIKE 'etablissement_%' THEN RIGHT(table_name, 8)
END millesime
FROM information_schema.TABLES
WHERE table_name ILIKE 'commune_%' OR
table_name ILIKE 'car_200m_%' OR
table_name ILIKE 'infracom_%' OR
table_name ILIKE 'route_%' OR
table_name ILIKE 'etablissement_%'
) AS req
WHERE req.donnees = param;
END;
$$
LANGUAGE plpgsql;