# F_MAX_NUMERO_EVENTI_RAGGIUNTO

## Codice Sorgente

```sql
FUNCTION F_MAX_NUMERO_EVENTI_RAGGIUNTO 
(
  P_GIORNO IN DATE 
) RETURN NUMBER AS
  v_max_eventi TB_CALENDAR_LOCKS.max_eventi%TYPE;  -- Variable to hold the max_eventi value from TB_CALENDAR_LOCKS
  v_event_count NUMBER;                            -- Variable to hold the count of events from EVENTI
BEGIN
  -- Step 1: Check if P_GIORNO is present in TB_CALENDAR_LOCKS and get the max_eventi value for that day
  BEGIN
    SELECT max_eventi
    INTO v_max_eventi
    FROM TB_CALENDAR_LOCKS
    WHERE giorno = P_GIORNO;
  EXCEPTION
    WHEN NO_DATA_FOUND THEN
      -- If the date is not found in TB_CALENDAR_LOCKS, return -1 (or some other code to indicate the absence)
      RETURN -1;
  END;

  -- Step 2: Count how many events occurred on P_GIORNO in the EVENTI table
  SELECT COUNT(*)
  INTO v_event_count
  FROM EVENTI
  WHERE TRUNC(DATA) = TRUNC(P_GIORNO);  -- Use TRUNC to compare only the date part

  -- Step 3: Compare the event count with the max_eventi and return the appropriate result
  IF v_event_count >= v_max_eventi THEN
    RETURN 1;  -- Maximum number of events has been reached or exceeded
  ELSE
    RETURN 0;  -- Maximum number of events has not been reached
  END IF;

END F_MAX_NUMERO_EVENTI_RAGGIUNTO;```
