• zawhtway (8/21/2010)


    Hi,

    Here is the code I have done and tested and seems working.

    Correct me if something is wrong. I'll have some more to do

    use MobilePOD

    Declare @chkHoliday int

    Declare @chkTomorrow int

    declare @DayName varchar(20)

    Declare @DayNo int

    SELECT @chkHoliday = count(*) from holidays where CONVERT(CHAR(10),freedate,101) = CONVERT(CHAR(10),GETDATE(),101)

    SELECT @chkTomorrow = count(*) from holidays where CONVERT(CHAR(10),freedate,101) = CONVERT(CHAR(10),GETDATE()+1,101)

    set @DayNo=(DATEPART(dw, getdate()) + @@DATEFIRST) % 7

    Insert into Pickup (StaffID, RouteID, PickupDate)

    select StaffID, RouteID, (getdate())

    from Routes

    where @chkHoliday = 0

    Insert into Pickup (StaffID, RouteID, PickupDate)

    select StaffID, RouteID, (getdate()+1)

    from Routes

    where @chkHoliday = 0 and Routes.SatDayDelivery = 1 and@DayNo = 6

    Insert into Pickup (StaffID, RouteID, PickupDate)

    select StaffID, RouteID, (getdate()+2)

    from Routes

    where @chkHoliday = 0 and Routes.SatDayDelivery = 1 and @chkTomorrow = 1 and @DayNo = 5

    Ask yourself if you'll have the correct number of entries if the second INSERT kicks in according to the criteria... if you think so, then look at the first INSERT and see why you'll end up with double the number of rows you expect.

    Also, did you actually want times on the PickupDate your inserting into the PickupTable with?

    Be advised that @@DateFirst is fickle if someone changes it. @@DateFirst should probably NOT be used here.

    Last but not least, the SELECTs you used on the Holiday table make it impossible for an index to be used correctly in at least two different ways. We really need for you to generate the CREATE TABLE statements for the Holidays and Routes tables along with their indexes. Start by right clicking on each table and look for "Script Table as" and follow your nose. If it doesn't script the indexes at the same time, then do the same for the indexes.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)