I need to create after insert and after update trigger. If the new row is inserted in table, it should run after insert trigger and put timestamp in other table. But if this row is edited again, it should update the other table also.
The status can be changed to active, pending etc. So when each status changes, I need to put timestamp. And for every new record I need to put new row.
Here is the table structure:
| ID | Name | Status |
|----|------|--------|
| 1 | xyz | Active |
| | | |
| | | |
Let's say this is new row inserted in table, so it should be inserted into another table. But when I change its status, it should update the other table against this ID.
| ID | Name | Active Staus | Other Status |
|----|------|--------------|--------------|
| 1 | xyz | TimeStamp | Time Stamp |
| | | | |
| | | | |`
USE [DemoDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[test_INSERT]
ON [dbo].[Demo]
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
DECLARE ID BIGINT
Declare @status varchar(50)
SELECT @ID = INSERTED.ID
FROM INSERTED
INSERT INTO [dbo].[LogTble]
VALUES(@ID,'timestamp')
END