Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- USE [student]
- GO
- /****** Object: UserDefinedFunction [urb0061].[CheckStaff] Script Date: 03/24/2012 20:37:20 ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- ALTER function [urb0061].[CheckStaff](@p_idUser int, @p_idTrainRide int)
- returns varchar(5)
- as
- begin
- declare @v_confirm int; -- BUDE DRŽET SLOUPCE CONFIRM Z CURSORU C_REZERVATION
- declare @v_idstaff int; -- BUDE DRŽET SLOUPCE IDSTAFF Z CURSORU C_STAFF
- declare @v_counter int; -- POČÍTA POČET PRUCHODU CYKLU KTERÝ POROVNÁVA CONFIRM S IDSTAFF
- -- 5 SPOJENÝCH TABULEK, DO KURZORU DAVAM SLOUPEC CONFIRM Z RESERVATION + PODMÍNKA S PARAMETRY FUNKCE
- declare c_reservation cursor for select reservation.confirm from [user]
- join Reservation on [User].idUser = Reservation.idUser
- join TrainRide on Reservation.idTrainRide = TrainRide.idTrainRide
- join TrainCrew on TrainRide.idTrain = TrainCrew.idTrainRide
- join Staff on TrainCrew.idStaff = Staff.idStaff
- where [User].idUser = @p_idUser and Reservation.idTrainRide = @p_idTrainRide;
- -- 5 SPOJENÝCH TABULEK, DO KURZORU DAVAM SLOUPEC JEDINEČNÉ HODNOTY ZE SLOUPCE IDSTAFF Z TABULKY STAFF + PODMÍNKA S PARAMETRY FUNKCE
- declare c_staff cursor for select distinct(staff.idstaff) from [user]
- join Reservation on [User].idUser = Reservation.idUser
- join TrainRide on Reservation.idTrainRide = TrainRide.idTrainRide
- join TrainCrew on TrainRide.idTrain = TrainCrew.idTrainRide
- join Staff on TrainCrew.idStaff = Staff.idStaff
- where [User].idUser = @p_idUser and Reservation.idTrainRide = @p_idTrainRide;
- set @v_counter = 0; -- NASTAVÍM VÝCHOZÍ HODNOTU
- open c_reservation;
- fetch next from c_reservation into @v_confirm;
- -- PROCHÁZÍM VŠECHNY CONFIRMY OD DANÉHO UŽIVATELE NA DANÉM TRAINRIDE
- while @@FETCH_status = 0
- begin
- open c_staff
- fetch next from c_staff into @v_idstaff
- -- KAŽDY CONFIRM POROVNÁM SE VŠEMI ČLENY POSÁDKY
- while @@fetch_status = 0
- begin
- if @v_confirm = @v_idstaff -- POKUD NAJDU SHODU NENÍ TŘEBA POKRAČOVAT
- break;
- else
- fetch next from c_staff into @v_idstaff; -- JINAK POROVNÁVÁM S DALŠÍM ČLENEM POSÁDKY
- set @v_counter = @v_counter + 1;
- if @v_counter = 3 -- POKUD ANI TŘETÍ ČLEN NEPOTVRDIL REZERVACI - NENÍ TŘEBA POKRAČOVAT A VRACÍM FALSE
- return 'false'
- end
- set @v_counter = 0; -- RESETUJU COUNTER
- close c_staff;
- fetch next from c_reservation into @v_confirm; -- JDU NA DALŠÍ REZERVACI
- end
- close c_reservation;
- return 'true'; -- POKUD PROGRAM DOJDE AŽ SEM TAK POKAŽDÉ NAŠEL ČLENA POSÁDKY KTERÝ POTVRDIL REZERVACI -> VRACÍM TRUE
- end
Advertisement
Add Comment
Please, Sign In to add comment