set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER TRIGGER [dbo].[Cost_Change]
ON [dbo].[Inventory]
AFTER UPDATE
AS
BEGIN
SET NOCOUNT ON;
--if update(cost)
if exists (Select deleted.cost from deleted inner join inserted on deleted.Local_sku = inserted.local_sku where deleted.cost <> inserted.cost)
Begin
Insert into Audit_Cost (username, "when", SKU, old, new) select SUSER_SNAME(), GetDate(), inserted.local_sku, deleted.cost, inserted.cost from inserted inner join deleted on inserted.local_sku = deleted.local_sku
DECLARE @tableHTML NVARCHAR(MAX) ;
Declare @sku nvarchar(50);
Declare @old money;
Declare @new money;
Select @sku= inserted.local_sku, @old= deleted.cost, @new=inserted.cost from inserted inner join deleted on inserted.local_sku = deleted.local_sku
SET @tableHTML =
N'<H3>The following cost changes were made:</H3>' +
'SKU ' + @sku + '<br />Old cost: <b>' + cast(@old as nvarchar(20)) + '</b><br />New cost: <b>' + cast(@new as nvarchar(20)) + '</b><br />Changed by ' + SUSER_SNAME()
EXEC msdb.dbo.sp_send_dbmail @recipients='[email protected]',
@subject = 'Cost Change',
@body = @tableHTML,
@body_format = 'HTML' ;
end
END
|