Home Forums SQL Server 2008 T-SQL (SS2K8) How to usdate records after some time periode of its insretion in table. RE: How to usdate records after some time periode of its insretion in table.

  • If you want to get fancy-smancy, you could do it like this:

    CREATE TABLE #Users

    (UserID VARCHAR(20) NOT NULL

    ,Created_DT DATETIME DEFAULT (GETDATE())

    ,Expiry_DT DATETIME DEFAULT (DATEADD(hour, 2, GETDATE()))

    ,Is_Expired AS CASE WHEN GETDATE() > Expiry_DT THEN 'Y' ELSE 'N' END)

    INSERT INTO #Users (UserID)

    SELECT 'Dwain'

    SELECT * FROM #Users

    -- When User validates

    UPDATE #Users SET Expiry_DT = '9999-12-31' WHERE UserID = 'Dwain'

    SELECT * FROM #Users

    DROP TABLE #Users

    Now you have an Is_Expired flag you can query.

    Edit: The only downside to this approach is you can't index on the computed Is_Expired column because it is not persisted, and you can't make it persisted (I believe) because it is non-deterministic.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St