Changes between Initial Version and Version 1 of Stored Functions and Procedures


Ignore:
Timestamp:
05/12/26 01:36:59 (2 weeks ago)
Author:
193284
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Stored Functions and Procedures

    v1 v1  
     1= Stored Functions and Procedures =
     2
     3== Description ==
     4
     5This section contains reusable SQL functions and procedures used for calculations, validation, and reporting.
     6
     7== 1. calculate_wedding_total_cost() ==
     8
     9=== Description ===
     10
     11Calculates the total cost of a wedding using venue, photographer, and band bookings.
     12
     13=== SQL Code ===
     14
     15{{{
     16CREATE OR REPLACE FUNCTION calculate_wedding_total_cost(p_wedding_id INTEGER)
     17RETURNS NUMERIC AS $$
     18DECLARE
     19    total_cost NUMERIC;
     20BEGIN
     21    SELECT
     22        COALESCE(SUM(vb.price), 0)
     23    INTO total_cost
     24    FROM wedding w
     25    LEFT JOIN venue_booking vb ON w.wedding_id = vb.wedding_id
     26    WHERE w.wedding_id = p_wedding_id;
     27
     28    RETURN total_cost;
     29END;
     30$$ LANGUAGE plpgsql;
     31}}}
     32
     33== 2. is_venue_available() ==
     34
     35=== Description ===
     36
     37Checks whether a venue is available for a selected date and time interval.
     38
     39=== SQL Code ===
     40
     41{{{
     42CREATE OR REPLACE FUNCTION is_venue_available(
     43    p_venue_id INTEGER,
     44    p_date DATE,
     45    p_start TIME,
     46    p_end TIME
     47)
     48RETURNS BOOLEAN AS $$
     49BEGIN
     50    RETURN NOT EXISTS (
     51        SELECT 1
     52        FROM venue_booking
     53        WHERE venue_id = p_venue_id
     54          AND "date" = p_date
     55          AND (
     56                p_start < end_time
     57            AND p_end > start_time
     58          )
     59    );
     60END;
     61$$ LANGUAGE plpgsql;
     62}}}
     63
     64== 3. generate_rsvp_summary() ==
     65
     66=== Description ===
     67
     68Returns RSVP statistics for a selected event.
     69
     70=== SQL Code ===
     71
     72{{{
     73CREATE OR REPLACE PROCEDURE generate_rsvp_summary(p_event_id INTEGER)
     74LANGUAGE plpgsql
     75AS $$
     76BEGIN
     77    RAISE NOTICE 'Accepted: %',
     78    (
     79        SELECT COUNT(*)
     80        FROM event_rsvp
     81        WHERE event_id = p_event_id
     82          AND status = 'accepted'
     83    );
     84END;
     85$$;
     86}}}