For a training class, user insert info like startdt, enddt, time range, requester name
in the main table cal_tr_requests
. In case of training with more than one day, the enddt
is after startdt
. I have created another table cal_tr_Multidate
to insert all the dates with classId
and time range from main table. So if the cal_tr_requests
enter this:
ClassID -- req_start_Date---req_end_date ---req_time--requsetor_name
1234567 2016-08-30 2016-08-31 8:00-10:00 test
then the cal_tr_Multidates
table will enter:
ClassID --- ReserveDt ---- ReserveTm
1234567 2016-08-30 8:00-10:00
1234567 2016-08-31 8:00-10:00
I am having problem to create the stored procedure with the loop. This is what I have:
Alter PROCEDURE [dbo].[AddTrainingRoomRq_test]
@req_start_date DateTime,
@req_end_date DateTime,
@req_times text,
@requestor_name text
AS
BEGIN
Declare
@ClassID int,
@Multidates int
--Create the ClassID as a random number
Set @ClassID = (SELECT CAST(RAND() * 1000000000 AS INT) AS [RandomNumber])
INSERT INTO cal_tr_requests
(ClassID, req_start_date, req_end_date, req_times, requestor_name, date_of_req)
VALUES (@ClassID,@req_start_date,@req_end_date,@req_times,@requestor_name,getdate())
set @Multidates = (select datediff(d, req_start_date, req_end_date)
from cal_tr_requests
where ClassID = @ClassID)
if @Multidates = 0
Insert into Cal_tr_Multidates (ClassID, ReserveDt,ReserveTm)
Values(@ClassID, @req_start_date, @req_times)
else if @Multidates > 0
While @Multidates = 0
begin
--do the insert for all dates with classid and reserveTm
End
END
Can someone please point me to right direction? Thanks in advance.