Hi,
I have any problem to create stored procedure in Oracle, Can you help me?
I have to create a stored procedure (or package) in order to reserve the free rooms to the students in the period comprised between the DATE_START and DATE_END.
Table of the present rooms in building BL1 (RM): SEX 1 = M - SEX 2 = F SEX = 0 (ROOM WITHOUT SEX)
select count (1)
into v_appo
from em
where em_id = stud_name
and cod_stud = cod_stud;
if v_appo = 0 then
insert new student:
insert into em (em_id,cod_sud,sex)
values (stud_name,cod_stud,sex_in);
Now I must verify the free rooms in the period comprised between the DATE_START_IN and DATE_END_IN.
I tried this query:
select bl_id,fl_id,rm_id,RM_STD
from rm
where (bl_id,fl_id,rm_id,RM_STD) not in (select a.bl_id,a.fl_id,a.rm_id,A.RM_STD
from rm a, rmpct b
where a.bl_id=b.bl_id
and a.fl_id=b.fl_id
and a.rm_id=b.rm_id
AND((b.date_start <= TO_DATE(date_start_in, 'dd-mm-YYYY')
AND b.date_end >= TO_DATE(date_end_in, 'dd-mm-YYYY'))
OR ( b.date_end <= TO_DATE(date_end_in, 'dd-mm-YYYY'))
AND b.date_end >= TO_DATE(date_start_in,
'dd-mm-YYYY')
OR ( b.date_start >= TO_DATE(date_start_in, 'dd-mm-YYYY')
and b.date_start <= TO_DATE(date_end_in,
'dd-mm-YYYY')
AND b.date_end >= TO_DATE(date_end_in, 'dd-mm-YYYY'))))
with this query I get all free rooms in period date_start_in - date_end_in, but I must,also,verify if there are double or triple rooms (reserved) with minus of 2 (double) or minus of 3 (triple) students. If there are I can reserved these rooms.
I tried to verify with these steps:
CREATE OR REPLACE VIEW COUNT_EM ( BL_ID,
FL_ID, RM_ID, NUMBER_EM ) AS
(SELECT rm.bl_id, rm.fl_id, rm.rm_id, COUNT(*) as numero_em
FROM em, rm
WHERE em.bl_id(+) = rm.bl_id
AND em.fl_id(+) = rm.fl_id
AND em.rm_id(+) = rm.rm_id
and rm.rm_std in ('S', 'D', 'T')
group by rm.bl_id, rm.fl_id, rm.rm_id)
CREATE OR REPLACE VIEW COUNT_RMPCT ( BL_ID,
FL_ID, RM_ID, STD_EM, NUMBER_RMPCT
) AS
SELECT rm.bl_id, rm.fl_id, rm.rm_id, rmstd.std_em, COUNT(*) as numero_rmpct
FROM rm, rmpct,rmstd
WHERE rmpct.bl_id(+) = rm.bl_id
AND rmpct.fl_id(+) = rm.fl_id
AND rmpct.rm_id(+) = rm.rm_id
and rm.rm_std=rmstd.rm_std
and rm.rm_std in ('S', 'D', 'T')
AND((rmpct.date_start <= TO_DATE(date_start_in', 'dd-mm-YYYY')
AND rmpct.date_end >= TO_DATE(date_end_in, 'dd-mm-YYYY'))
OR ( rmpct.date_end <= TO_DATE(date_end_in, 'dd-mm-YYYY'))
AND rmpct.date_end >= TO_DATE(date_start_in, 'dd-mm-YYYY')
OR ( rmpct.date_start >= TO_DATE(date_start_in, 'dd-mm-YYYY')
and rmpct.date_start <= TO_DATE(date_end_in, 'dd-mm-YYYY')
AND rmpct.date_end >= TO_DATE(date_end_in, 'dd-mm-YYYY')))
group by rm.bl_id, rm.fl_id, rm.rm_id, rmstd.std_em
ok, my problem is this:
I have to create a stored procedure in order to reserve the free rooms to the students in the period comprised between the DATE_START and DATE_END.
step1: verify if the student is in table em:
if the student is not in table em, then insert new student
step2: find available room
if find it reserve room INTO rmpct
step3: update students in em
step4: update sex of room or apartment floor
step5: if no room is available raise error
I tried this procedure:
CREATE OR REPLACE PROCEDURE reserve_rooms
-- input parameters:
(p_em_id IN em.em_id%TYPE,
p_cod_stud IN em.cod_stud%TYPE,
p_bl_id IN rm.bl_id%TYPE,
p_fl_id IN rm.fl_id%TYPE,
p_rm_id IN rm.rm_id%TYPE,
p_sex IN rm.sex%TYPE,
p_date_start IN VARCHAR2,
p_date_end IN VARCHAR2)
AS
-- local variables:
v_appo INTEGER;
v_bl_id rm.bl_id%TYPE;
v_fl_id rm.fl_id%TYPE;
v_rm_id rm.rm_id%TYPE;
v_rm_cat rm.rm_cat%TYPE;
v_sex rm.sex%TYPE;
BEGIN
-- verify if the student is in table em:
SELECT COUNT (*)
INTO v_appo
FROM em
WHERE em_id = p_em_id
AND cod_stud = p_cod_stud;
-- if the student is not in table em, then insert new student:
IF v_appo = 0 THEN
INSERT INTO em (em_id, cod_stud)
VALUES (p_em_id, p_cod_stud);
END IF;
BEGIN
-- find available room:
SELECT bl_id, fl_id, rm_id, sex, rm_cat
INTO v_bl_id, v_fl_id, v_rm_id, v_sex, v_rm_cat
FROM (SELECT rm.bl_id, rm.fl_id, rm.rm_id, rm.sex, rm.rm_cat
FROM rmpct, rm
WHERE rm.bl_id = rmpct.bl_id (+)
AND rm.fl_id = rmpct.fl_id (+)
AND rm.rm_id = rmpct.rm_id (+)
AND rm.bl_id = NVL (p_bl_id, rm.bl_id)
AND rm.fl_id = NVL (p_fl_id, rm.fl_id)
AND rm.rm_id = NVL (p_rm_id, rm.rm_id)
AND (rm.sex = p_sex OR rm.sex = 0)
AND rmpct.date_start (+) <= TO_DATE (p_date_end, 'DD-MM-YYYY')
AND rmpct.date_end (+) >= TO_DATE (p_date_start, 'DD-MM-YYYY')
GROUP BY rm.bl_id, rm.fl_id, rm.rm_id, rm.sex, rm.rm_cat, rm.rm_std
HAVING SUM (decode(rmpct.rm_id (+),null,0,1))
< DECODE (rm_std, 'S', 1, 'D', 2, 'T', 3)
ORDER BY DBMS_RANDOM.RANDOM)
WHERE ROWNUM = 1;
-- reserve room:
INSERT INTO rmpct (bl_id, fl_id, rm_id, em_id, cod_stud, date_start, date_end)
VALUES (v_bl_id, v_fl_id, v_rm_id, p_em_id, p_cod_stud,
TO_DATE (p_date_start, 'DD-MM-YYYY'),
TO_DATE (p_date_end, 'DD-MM-YYYY'));
-- update students:
UPDATE em
SET bl_id = v_bl_id, fl_id = v_fl_id, rm_id = v_rm_id
WHERE em_id = p_em_id
AND cod_stud = p_cod_stud;
-- update sex of room or apartment floor:
IF v_sex = 0 THEN
IF v_rm_cat = 'ROOM' THEN
UPDATE rm
SET sex = p_sex
WHERE bl_id = v_bl_id
AND fl_id = v_fl_id
AND rm_id = v_rm_id;
ELSIF v_rm_cat = 'APARTMENT' THEN
UPDATE rm
SET sex = p_sex
WHERE bl_id = v_bl_id
AND fl_id = v_fl_id;
END IF;
END IF;
EXCEPTION
-- if no room is available:
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR (-20001, 'Sorry, there is no such room available.');
END;
END reserve_rooms;
Originally posted by marist89 I thought of that too, but I'm only down to 3 bullets and will probably need them for my own guys today...
Table of the present rooms in building BL1 (RM): SEX 1 = M - SEX 2 = F SEX = 0 (ROOM WITHOUT SEX)
I thought you guys were going to say "who wants to live in a room with a sex code of zero?"
Raf, you aren't going to get someone to completely do you job for you. Besides I see 4 procedures and a function in a single package not one procedure. Steps 1, 3 and 4 can be separates procedures in a package. Step two is a function that returns an available room. Step 5 should be called when you want to assign someone to a room. It in turn will call the other procedures and the function. You need to break down the problem more and test each part seperately, then write the main procedure. You might also want to look at error handling and error logging.
step1: verify if the student is in table em:
if the student is not in table em, then insert new student
step2: find available room
if find it reserve room INTO rmpct
Bookmarks