Trigger for automatically date

  • Hello everyone,

    I created a trigger that inserts the automatic time in the Date column as soon as an update is made in the Comments text field.

    The problem here is, , that it constantly overwrites all updates that have already been carried out. It should only add the new date to the article that is currently being carried out.

    Can someone help me with this and make an appropriate adjustment to the code?

    ALTER TRIGGER [dbo].[time_trigger] 
    ON [dbo].[products]
    AFTER UPDATE
    AS
    BEGIN
    Update dbo.products
    SET DATE= GETDATE()
    WHERE COMMENT IN (Select Distinct COMMENT FROM products)
    END

    If I now run an update to product number 888 with the text Hello, the date 27-12-2022 appears with the time 11:00

    If I carry out another update with the product number 999 with the text Bye, the 27-12-2022 with the time 12:35 appears.

    The problem here is that the date for item 888 is also now 12:35. However, this should not be overwritten.

    pic2

  • while this looks like spam post I'll give it a go in case it isn't.

    you should first read the manuals for triggers in sql server to fully understand how they work and what you have available within a trigger. by reading them you will also be able to understand the following sample code which is what  you require after you change it to match your table definition

    CREATE TRIGGER LastModified
    ON tablea
    AFTER INSERT, UPDATE
    AS
    UPDATE a
    SET LastModifiedDate = GETDATE()
    FROM TableA a
    INNER JOIN inserted AS i
    ON i.key = a.key

     

  • This was removed by the editor as SPAM

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply