0

Can you please give me a hand in starting this?

I have a table with this format:

CREATE TABLE [dbo].[ACS_ARMAZEM]
(
    [ARM_ID] [INT] IDENTITY(20000,1) NOT NULL,
    [ARM_DES] [NVARCHAR](50) NOT NULL,
    [ARM_5_COD_DIG] [NVARCHAR](5) NOT NULL,
    [ARM_SIT] [INT] NOT NULL,
    [ARM_BIZ_TIP] [INT] NOT NULL,
    [ARM_STK_CTRL] [INT] NOT NULL,
    [ARM_DAT_CRI] [DATE] NOT NULL,
)

I need to log all changes (insert, delete or update) to a log table I have.

But I want to log the old and new values (even if a new row is inserted I want to log it as null in the old columns).

Best regards,

RL

1
  • 1
    Well, please show us what YOU have tried so far. We'll be glad to lend a helping hand - but SO isn't a free code-writing service where you just dump your requirements and someone writes the whole code for you .... start by reading the official Microsoft docs on how to create a trigger Commented Mar 16, 2019 at 12:18

1 Answer 1

1

Keeping it short, you need to use inserted and deleted tables. ie.

For insert/update:

UPDATE [YourTable]
   SET [YourColumn] = i + 1
OUTPUT deleted.[YourColumn], inserted.[YourColumn] INTO [YourLogTable]
WHERE i = 1;

(Here, deleted will give old values where inserted gives new)

For delete:

DELETE from [YourTable]
OUTPUT deleted.[YourColumn], NULL INTO [YourLogTable]
WHERE i = 2;

Of course, you will need to modify the statement to match with your log table structure. Please let me know if it helps. :)

Sign up to request clarification or add additional context in comments.

Comments

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.