DO $$
DECLARE
    r RECORD;
    cnt BIGINT;
BEGIN
    FOR r IN SELECT entidad_id FROM public.snc_catalog_mapping WHERE dpto_snc = 'H' ORDER BY entidad_id LOOP
        BEGIN
            EXECUTE 'SELECT count(*) FROM public.e' || r.entidad_id || '_lotes_activos' INTO cnt;
            RAISE NOTICE 'Entidad %: % registros', r.entidad_id, cnt;
        EXCEPTION WHEN OTHERS THEN
            RAISE NOTICE 'Entidad %: TABLA NO ENCONTRADA O ERROR', r.entidad_id;
        END;
    END LOOP;
END $$;
