spDelete_CalendarEvent_Recur

Parameters

Parameter Parameter Type Mode Description
@PKID int IN
@EventCalendarID int IN
@RecurUpdateFlag int IN
@RecurID int IN
@ClientUsersID int IN

Definition

Copy


            CREATE   PROCEDURE [dbo].[spDelete_CalendarEvent_Recur]
            (
                @PKID    int,
                @EventCalendarID    int,
                @RecurUpdateFlag    int,        -- 1: all dates, 2: all future dates, 3: this date only    
                @RecurID    int,
                @ClientUsersID    int
            )
            as
            set nocount on

            -- if (@RecurUpdateFlag = 1) delete ALL DATES is taken care of as a single event delete in spDelete_CalendarEvent

            if (@RecurUpdateFlag = 2)  -- delete FUTURE DATES
            begin
                BEGIN TRANSACTION 
                -- Is this date the only occurrence in the series?
                -- OR, is this date the first in the series?
                    if ((select count(*) from dbo.CalendarEvtRecur where EventID = @PKID) = 1
                        OR ((SELECT min(A.BeginDate) FROM dbo.CalendarEvtRecur A WHERE A.EventID = @PKID)
                            = (SELECT B.BeginDate FROM dbo.CalendarEvtRecur B WHERE B.RecurID = @RecurID AND B.EventID = @PKID)))
                        begin
                            -- treat as delete all dates
                            EXEC dbo.spDelete_CalendarEvent @PKID, @ClientUsersID
                        end
                    else -- there are > 1 occurrences and this date is not the first in the series
                        begin
                            -- separate occurrences before this date from those on/after this date
                            -- STEP 1. what is the recur end date of the occurrence just before this date?
                            declare @dPrevEndDate as datetime
                            set @dPrevEndDate = (SELECT max(A.EndDate) FROM dbo.CalendarEvtRecur A 
                                                WHERE A.EventID = @PKID 
                                                AND A.BeginDate < (SELECT B.BeginDate FROM dbo.CalendarEvtRecur B 
                                                                    WHERE B.RecurID = @RecurID AND B.EventID = @PKID))

                            -- use this date to update the RecurEndDate in CalendarEvent table of the current series
                            UPDATE dbo.CalendarEvent SET RecurEndDate = @dPrevEndDate WHERE ID = @PKID

                            -- STEP 2. delete occurrences on/after this date
                            DELETE from dbo.CalendarEvtRecur 
                            where EventID = @PKID and EndDate > @dPrevEndDate
                        end
                COMMIT TRANSACTION
            end

            if (@RecurUpdateFlag = 3)  -- delete THIS DATE ONLY
            begin
                BEGIN TRANSACTION
                -- Is this date the only occurrence in the series?
                    if ((select count(*) from dbo.CalendarEvtRecur where EventID = @PKID) = 1)
                    begin
                        -- treat as delete all dates
                        EXEC dbo.spDelete_CalendarEvent @PKID, @ClientUsersID
                    end
                    else    -- there are > 1 occurrences
                    begin
                        -- STEP 1. exclude this date occurrence from the series by deleting it from CalendarEvtRecur table
                        DELETE from dbo.CalendarEvtRecur 
                        where EventID = @PKID and RecurID = @RecurID 

                        -- STEP 2. update the begin date, end date and recur start date of the series,
                        --            in case this date occurrence is the first one in the series
                        declare @dNextBeginDate as datetime
                        set @dNextBeginDate = (select min(BeginDate) from dbo.CalendarEvtRecur where EventID = @PKID)
                        declare @dNextEndDate as datetime
                        set @dNextEndDate = (select min(EndDate) from dbo.CalendarEvtRecur where EventID = @PKID)

                        UPDATE dbo.CalendarEvent SET
                            BeginDate = @dNextBeginDate
                            EndDate = @dNextEndDate,
                            RecurStartDate = @dNextBeginDate
                        WHERE ID = @PKID
                    end
                COMMIT TRANSACTION
            end