Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2004
    Posts
    22

    Unanswered: Indentifying which rows have been updated since a certain date time

    I have a simple table structure:

    id (pk)
    description (varchar(max))

    Now this structure exists within a legacy database and is used by a legacy application, so I am restricted on what I can do here. I can add columns, but I cannot change any existing items or change how the application interacts with this table (no stored procedures here )

    As part of a separate process I need to identify which rows in the table have been updated since a given date.

    So I need something that will automatically update an addition "LastChange (datetime)" column, then I can simple select where LastChange > GivenDate. Job done.

    Does anybody know of a way of doing this, maybe triggers on insert/update? Maybe there is something else I can use.

    It is worth saying that although I refer to datetimes above, the solution doesn't have to use these I don't care when it was updated. So another potential solution would be an auto incrementing number etc

  2. #2
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    you could set a default of GETDATE() on a column. that will cover new stuff. updates might have to be trigger.
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  3. #3
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Code:
    --  Create what you've got now
    
    CREATE TABLE MrEyes (
       id			INT		IDENTITY
       CONSTRAINT XPKMrEyes
          PRIMARY KEY (id)
    ,  [description]	VARCHAR(MAX)	NOT NULL
       )
    GO
    
    --  Add new columns
    
    ALTER TABLE MrEyes
       ADD
       [who]		NVARCHAR(256)	NULL
    
    ALTER TABLE MrEyes
       ADD
       [when]		DATETIME	NULL
    		
    --  Add trigger to keep columns updated
    
    GO
    CREATE TRIGGER trMrEyes
    ON MrEyes
    FOR INSERT, UPDATE
    AS
    
    UPDATE MrEyes
       SET [who] = suser_sname()
    ,     [when] = GetUTCDate()
       WHERE EXISTS (SELECT *
          FROM inserted
          WHERE  inserted.id = MrEyes.id)
    
    RETURN
    GO
    
    -- Insert some sample data
    
    INSERT INTO MrEyes ([description])
       SELECT 'One' UNION ALL
       SELECT 'Two' UNION ALL
       SELECT 'Three'
    GO
    
    -- Update just one row
    
    UPDATE MrEyes
       SET [when] = '1900-01-01'
       WHERE  'Two' = [description]
    
    --  Show what we've got
    
    SELECT *
       FROM MrEyes
    
    -- Note the time difference for row two, and how much good
    -- attempting to fudge the date did
    -PatP
    In theory, theory and practice are identical. In practice, theory and practice are unrelated.

  4. #4
    Join Date
    Jan 2004
    Posts
    22
    Many thanks, that solution works perfectly

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •