I have created below trigger to start logging the company changes from the table1 into another audit table. It works fine with single row but crashing with identical change with multiple rows. Can you please help me to update the trigger to handle multi-row scenario. Thanks.
GO
IFNOTEXISTS(SELECT *FROMsys.objectsWHEREobject_id=OBJECT_ID(N'[dbo].[Company_AuditPeriod]')ANDtype in(N'U'))
CREATETABLE [dbo].[Company_AuditPeriod](
[Client] [varchar](25)NOTNULL,
[Period] [varchar](25),
[Table_Name] [varchar](25),
[Field_Name] [varchar](25),
[Old_Value] [varchar](25),
[New_Value] [varchar](25),
[User_ID] [varchar](25),
[Last_Update] [datetime],
[agrtid] [bigint]IDENTITY(1,1)NOTNULL,
)ON [PRIMARY]
GO
--create trigger
SETQUOTED_IDENTIFIERON
GO
CREATETRIGGER [dbo].[Table1_Update]
ON [dbo].[Table1]
FORUPDATE
NOTFORREPLICATION
AS
BEGIN
DECLARE
@status varchar(3),
@user_id varchar(25),
@period varchar(25),
@client varchar(25),
@last_updatedatetime
DECLARE
@Old_status varchar(3),
@Old_user_id varchar(25),
@Old_period varchar(25),
@Old_client varchar(25)
SELECT
@status =status,
@user_id =user_id,
@period = period,
@client = client,
@last_update= last_update
FROM Inserted
SELECT
@Old_status=status,
@Old_user_id =user_id,
@Old_period = period,
@Old_client = client
FROM Deleted
If @Old_status <> @status
INSERTINTO Company_AuditPeriod
VALUES ( @client, @period,'Table1', 'period',@old_status, @status, @user_id, @last_update)
END
GO