sgd-praktikum/SQL/PLSQL.sql

99 lines
2.7 KiB
MySQL
Raw Normal View History

2023-05-12 16:52:59 +02:00
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
startMonat := ADD_MONTHS(SYSDATE,-months);
2023-05-25 14:57:43 +02:00
SELECT CLERKID, Anzahl INTO finalclerkid, maxanzahlcleanings FROM
2023-05-12 16:52:59 +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
)
2023-05-25 14:57:43 +02:00
GROUP BY CLERKID ORDER BY Anzahl desc
2023-05-12 17:09:20 +02:00
)
2023-05-25 14:57:43 +02:00
WHERE ROWNUM <= 1;
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 PROCEDURE getLastBookings(months IN NUMBER)
IS
CURSOR bookingsCursor IS SELECT BOOKINGID FROM BOOKING WHERE ARRIVALDATE >= ADD_MONTHS(SYSDATE,-months);
BEGIN
FOR booking IN bookingsCursor
LOOP
DBMS_OUTPUT.PUT_LINE(booking.BOOKINGID);
END LOOP;
2023-05-12 17:25:09 +02:00
END;
create or replace procedure rooms(clientz in number, months in number)
is
2023-05-25 14:57:43 +02:00
CURSOR roomCursor is
2023-05-12 17:25:09 +02:00
select br.ROOMNUMBER
from BOOKINGROOM br inner join (
select *
from BOOKING inner join CLIENT
on BOOKING.CLIENTID = CLIENT.PERSONID
2023-05-25 14:57:43 +02:00
where CLIENT.PERSONID = clientz and BOOKING.ARRIVALDATE >= add_months(sysdate, -months)
2023-05-12 17:25:09 +02:00
) bookings on br.BOOKINGID = bookings.BOOKINGID;
2023-05-25 14:57:43 +02:00
begin
FOR number IN roomCursor
LOOP
DBMS_OUTPUT.PUT_LINE(number.ROOMNUMBER);
END LOOP;
2023-05-12 17:25:09 +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
BEFORE INSERT OR UPDATE
ON BOOKINGROOM
FOR EACH ROW
DECLARE
cleaningDate DATE;
BEGIN
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);
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-25 15:52:59 +02:00
create or replace trigger total_price
after insert or update on BOOKING
for each row
declare
total_price number;
nights number;
begin
nights := :NEW.DEPARTUREDATE - :NEW.ARRIVALDATE;
total_price := nights * :NEW.COST;
dbms_output.put_line('Gesamtpreis:');
dbms_output.put_line(total_price);
end;
2023-05-25 14:57:43 +02:00