UFN_DESIGNATION_CAMPAIGNDATESOVERLAP

Determines if any dates overlap given a collection of campaigns and dates.

Return

Return Type
bit

Parameters

Parameter Parameter Type Mode Description
@CAMPAIGNS xml IN

Definition

Copy


            CREATE function dbo.UFN_DESIGNATION_CAMPAIGNDATESOVERLAP
            (            
                @CAMPAIGNS xml
            )
            returns bit    
            with execute as caller
            as
            begin
                declare @CAMPAIGNID uniqueidentifier;
                declare @SEQUENCE int;
                declare @DATEFROM datetime;
                declare @DATETO datetime;

                declare CAMPAIGN_CURSOR cursor local fast_forward for
                    select distinct
                        tf.CAMPAIGNID
                    from 
                        dbo.UFN_DESIGNATION_CAMPAIGNS_FROMITEMLISTXML(@CAMPAIGNS) tf;

                open CAMPAIGN_CURSOR;
                fetch next from CAMPAIGN_CURSOR into @CAMPAIGNID;

                while (@@FETCH_STATUS = 0)
                begin 
                    declare DATE_CURSOR cursor local fast_forward for
                        select SEQUENCE, DATEFROM, DATETO from dbo.UFN_DESIGNATION_CAMPAIGNS_FROMITEMLISTXML(@CAMPAIGNS) where CAMPAIGNID = @CAMPAIGNID;

                    open DATE_CURSOR 
                    fetch next from DATE_CURSOR into @SEQUENCE, @DATEFROM, @DATETO;

                    while (@@FETCH_STATUS = 0)
                    begin 
                        if exists (
                            select 1 from dbo.UFN_DESIGNATION_CAMPAIGNS_FROMITEMLISTXML(@CAMPAIGNS) tf
                            where (
                                    ((tf.DATETO between @DATEFROM and @DATETO) or (tf.DATEFROM between @DATEFROM and @DATETO) or (tf.DATEFROM is null and tf.DATETO is null)) --Both not null

                                    or 
                                    (@DATEFROM is null and ((tf.DATEFROM <= @DATETO) or (tf.DATETO <= @DATETO))) --@DATEFROM is null

                                    or
                                    (@DATETO is null and ((tf.DATEFROM >= @DATEFROM) or (tf.DATETO >= @DATEFROM))) --@DATETO is null

                                    or
                                    (@DATEFROM is null and @DATETO is null and tf.SEQUENCE is not null) -- Both are null

                                  )
                            and (tf.SEQUENCE <> @SEQUENCE)
                            and (tf.CAMPAIGNID = @CAMPAIGNID)
                        )
                        begin
                            close DATE_CURSOR;
                            deallocate DATE_CURSOR;

                            close CAMPAIGN_CURSOR;
                            deallocate CAMPAIGN_CURSOR;

                            return 1;
                        end

                        fetch next from DATE_CURSOR into @SEQUENCE, @DATEFROM, @DATETO;
                    end

                    close DATE_CURSOR;
                    deallocate DATE_CURSOR;



                    fetch next from CAMPAIGN_CURSOR into @CAMPAIGNID;
                end

                close CAMPAIGN_CURSOR;
                deallocate CAMPAIGN_CURSOR;

                return 0;
            end