sgd-praktikum/SQL/PLSQL.sql

127 lines
4 KiB
MySQL
Raw Normal View History

CREATE OR REPLACE TYPE bookingIds AS TABLE OF NUMBER;
2023-05-12 17:09:20 +02:00
2023-05-12 16:52:59 +02:00
CREATE OR REPLACE FUNCTION mostValuableClerk(months IN NUMBER) RETURN NUMBER
IS
startMonat DATE;
2023-05-12 17:09:20 +02:00
finalclerkid NUMBER;
maxanzahlcleanings NUMBER;
2023-05-12 16:52:59 +02:00
BEGIN
2023-06-03 10:17:20 +02:00
IF months > 0 THEN
startMonat := ADD_MONTHS(SYSDATE,-months);
2023-05-12 16:52:59 +02:00
2023-06-03 10:17:20 +02:00
SELECT CLERKID, Anzahl INTO finalclerkid, maxanzahlcleanings FROM
2023-05-12 16:52:59 +02:00
(
2023-06-03 10:17:20 +02:00
SELECT DISTINCT CLERKID, COUNT(CLERKID) AS Anzahl
From
(
SELECT clcl.CLERKID
FROM CLEANINGCLERK clcl
INNER JOIN CLEANINGPLAN clpl ON clcl.CLEANINGPLANID = clpl.CLEANINGPLANID
WHERE clpl."date" >= startMonat
)
GROUP BY CLERKID ORDER BY Anzahl desc
)
WHERE ROWNUM <= 1;
ELSE
RAISE_APPLICATION_ERROR(-20111, 'Nur Positive Werte (>0) sind als Eingabe erlaubt');
END IF;
2023-05-12 16:52:59 +02:00
2023-05-12 17:09:20 +02:00
return finalclerkid;
2023-05-12 17:20:58 +02:00
END;
CREATE OR REPLACE FUNCTION getLastBookings(months IN NUMBER) RETURN bookingIds
2023-05-12 17:20:58 +02:00
IS
CURSOR bookingsCursor IS SELECT BOOKINGID FROM BOOKING WHERE ARRIVALDATE >= ADD_MONTHS(SYSDATE,-months);
ids bookingIds;
2023-05-12 17:20:58 +02:00
BEGIN
ids := bookingIds();
2023-05-12 17:20:58 +02:00
FOR booking IN bookingsCursor
LOOP
ids.extend;
ids(ids.count) := booking.BOOKINGID;
2023-05-12 17:20:58 +02:00
DBMS_OUTPUT.PUT_LINE(booking.BOOKINGID);
END LOOP;
return ids;
2023-05-12 17:25:09 +02:00
END;
2023-05-30 08:46:57 +02:00
CREATE OR REPLACE PROCEDURE rooms(clientz IN NUMBER, months IN NUMBER)
2023-05-12 17:25:09 +02:00
2023-05-30 08:46:57 +02:00
IS
2023-05-25 14:57:43 +02:00
CURSOR roomCursor is
2023-05-30 08:46:57 +02:00
SELECT br.ROOMNUMBER
FROM BOOKINGROOM br INNER JOIN (
SELECT *
FROM BOOKING INNER JOIN CLIENT
ON BOOKING.CLIENTID = CLIENT.PERSONID
WHERE CLIENT.PERSONID = clientz AND BOOKING.ARRIVALDATE >= add_months(sysdate, -months)
) bookings ON br.BOOKINGID = bookings.BOOKINGID;
BEGIN
2023-05-25 14:57:43 +02:00
FOR number IN roomCursor
LOOP
DBMS_OUTPUT.PUT_LINE(number.ROOMNUMBER);
END LOOP;
2023-05-30 08:46:57 +02:00
END rooms;
2023-05-25 14:57:43 +02:00
2023-05-25 15:42:37 +02:00
CREATE OR REPLACE TRIGGER booking_created
2023-06-03 10:27:02 +02:00
AFTER INSERT OR UPDATE OR DELETE
2023-05-25 15:42:37 +02:00
ON BOOKINGROOM
FOR EACH ROW
DECLARE
cleaningDate DATE;
BEGIN
2023-06-03 10:17:20 +02:00
IF INSERTING OR UPDATING THEN
SELECT arrivalDate INTO cleaningDate FROM BOOKING
WHERE BOOKING.BOOKINGID = :NEW.bookingId;
cleaningDate := cleaningDate - 1;
INSERT INTO CLEANINGPLAN VALUES (cleaningplan_seq.nextval, cleaningDate, 90, :NEW.roomNumber);
ELSIF DELETING THEN
SELECT arrivalDate INTO cleaningDate FROM BOOKING
WHERE BOOKING.BOOKINGID = :OLD.bookingId;
cleaningDate := cleaningDate - 1;
DELETE FROM CLEANINGPLAN
WHERE "date" = cleaningDate AND :OLD.roomNumber = CLEANINGPLAN.ROOMNUMBER;
END IF;
2023-05-25 15:42:37 +02:00
END;
CREATE OR REPLACE TRIGGER cleaning_while_booked
BEFORE INSERT OR UPDATE
ON CleaningPlan
FOR EACH ROW
DECLARE
bookingcount NUMBER;
BEGIN
SELECT COUNT(bk.bookingId) INTO bookingcount
FROM Booking bk INNER JOIN BookingRoom br ON bk.bookingId = br.bookingId
WHERE br.roomNumber = :NEW.roomNUMBER AND bk.arrivalDate <= :NEW."date" AND bk.departureDate >= :NEW."date";
IF bookingcount > 0 THEN
RAISE_APPLICATION_ERROR(-20111, 'CleaningPlan liegt in gebuchtem Zeitraum');
END IF;
END;
2023-05-25 14:57:43 +02:00
2023-05-30 08:46:57 +02:00
CREATE OR REPLACE TRIGGER total_price
2023-06-03 10:27:02 +02:00
BEFORE INSERT OR UPDATE OR DELETE ON BOOKING
2023-05-30 08:46:57 +02:00
FOR EACH ROW
DECLARE
total_price NUMBER;
2023-06-03 10:27:02 +02:00
nights NUMBER;
2023-05-30 08:46:57 +02:00
BEGIN
2023-06-03 10:27:02 +02:00
IF DELETING THEN
DBMS_OUTPUT.PUT_LINE('Buchung erfolgreich storniert');
ELSIF UPDATING AND :NEW.COST < :OLD.COST THEN
RAISE_APPLICATION_ERROR(-20111, 'Buchungen dürfen nicht vergünstigt werden!');
2023-05-25 15:52:59 +02:00
nights := :NEW.DEPARTUREDATE - :NEW.ARRIVALDATE;
2023-06-03 10:27:02 +02:00
IF nights < 1 THEN
RAISE_APPLICATION_ERROR(-20111, 'Invalide Dauer: Mindestens eine Nacht muss gebucht werden!');
END IF;
ELSE
nights := :NEW.DEPARTUREDATE - :NEW.ARRIVALDATE;
IF nights < 1 THEN
RAISE_APPLICATION_ERROR(-20111, 'Invalide Dauer: Mindestens eine Nacht muss gebucht werden!');
END IF;
2023-05-25 15:52:59 +02:00
total_price := nights * :NEW.COST;
dbms_output.put_line('Gesamtpreis:');
dbms_output.put_line(total_price);
2023-06-03 10:27:02 +02:00
END IF;
2023-05-30 08:46:57 +02:00
END;
2023-05-25 14:57:43 +02:00