• So, while I still think you're setting yourself up if you're not incredibly careful, here's one of the most cautious ways to approach this to make sure the data can't be double-touched between the select and the updates.

    Note, I switched the sample data over to a temp table so you could actually do an update.

    IF OBJECT_ID ('tempdb..#SampleData') IS NOT NULL

    DROP TABLE #SampleDAta

    CREATE TABLE #SampleData (PERSON INT ,dt DATETIME,[PAYTYPE] VARCHAR(20) ,[ID] INT ,[DOW] VARCHAR(20))

    INSERT INTO #SampleData

    SELECT 12913,'07/21/2014','Regular',101,'Monday'

    UNION ALL SELECT 12913,'07/22/2014','Regular',102,'Tuesday'

    UNION ALL SELECT 12913,'07/23/2014','Regular',103,'Wednesday'

    UNION ALL SELECT 12913,'07/24/2014','Regular',104,'Thursday'

    UNION ALL SELECT 12913,'07/25/2014','Regular',105,'Friday'

    UNION ALL SELECT 12913,'07/26/2014','Regular',104,'Saturday'

    UNION ALL SELECT 12913,'07/27/2014','Regular',105,'Sunday'

    UNION ALL SELECT 12913,'07/28/2014','Regular',101,'Monday'

    UNION ALL SELECT 12913,'07/29/2014','Regular',102,'Tuesday'

    UNION ALL SELECT 12913,'07/30/2014','Regular',103,'Wednesday'

    UNION ALL SELECT 12913,'07/31/2014','Regular',104,'Thursday'

    UNION ALL SELECT 12913,'08/01/2014','Regular',105,'Friday'

    DECLARE @RunDate DATETIME

    SET @RunDate = '20140730'

    DECLARE @Result TABLE

    (PERSON INT ,dt DATETIME,[PAYTYPE] VARCHAR(20) ,[ID] INT )

    SELECT

    dt,

    DATEPART( week, dt - 1) -- -1 because standard weeks start on sunday)

    FROM

    #SampleData

    WHERE

    DATEPART( week, dt - 1) = DATEPART( week, @RunDate - 1)

    AND dt <= @RunDate

    UPDATE #SampleData

    SETPayType = 'Regular2'

    OUTPUT

    inserted.Person,

    Inserted.dt,

    Inserted.PayType,

    inserted.id

    INTO

    @Result

    WHERE

    PAyType = 'Regular'

    AND DATEPART( week, dt - 1) = DATEPART( week, @RunDate - 1) -- -1 because standard weeks start on sunday)

    AND dt <= @RunDate

    SELECT * FROM @Result

    Let me know if there's any confusion or concerns. Be aware that the DATEPART component is going to die a horrid death because of no indexing. I'd recommend you turn that into a computed persisted column on the table and index it.


    - Craig Farrell

    Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

    For better assistance in answering your questions[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA