1

We just want to maintain table log (every changes) history in MS SQL by write trigger please suggest

i tried but not working

CREATE TRIGGER [dbo].[update_ServiceDescriptionTable]
ON ServiceDescriptionMaster
After UPDATE
AS
BEGIN
        declare @Rate money;
        Select @Rate = Rate from inserted;

        update [dbo].[ServiceDescriptionMasterlog] set Rate = @Rate 
        where Service_Description = '';
END

1 Answer 1

1

Ya good. If you want to maintain evry changes log then you can insert in same log table with all field like as follows:

1) create same table like "ServiceDescriptionMasterlog" with one Extra Field (Column) Entry_DateTime set default bind getdate() method.

2) write a trigger on "ServiceDescriptionMaster" table as follows:

ALTER TRIGGER [dbo].[ServiceDescriptionMaster_OnUpdate]
ON [dbo].[ServiceDescriptionMaster]
After UPDATE
AS
BEGIN
       SET NOCOUNT ON; 
       INSERT INTO [dbo].[ServiceDescriptionMasterLog]
       (S_No,Rate,.....)
       select S_No,Rate,.....
       from Deleted;
END

you can also maintain on delete:

ALTER TRIGGER [dbo].[ServiceDescriptionMaster_OnDelete]
ON [dbo].[ServiceDescriptionMaster]
For Delete
AS
BEGIN
       SET NOCOUNT ON; 
       INSERT INTO [dbo].[ServiceDescriptionMasterLog]
       (S_No,Rate,.....)
       select S_No,Rate,.....
       from Deleted;
END
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Yogesh Sharma
Thank you so much @shamim sir :) (Y)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.