Question : SQL Server Delete Trigger, want to retain values of record being deleted

I have simple Trigger that I want to track Inserts, Updates and Deletes on records in a table and store those values in a log table.   The Insert and Update works fine but when I do a Delete trigger I lose the values, its seems to be deleting the record before I can grap the values of specific fields.   I tried using the AFTER statement instead of FOR the trigger as shown in my code below, but it did not work

ALTER TRIGGER [dbo].[trig_SiteLookupDelete]ON [dbo].[LVRG_CTRL_SiteLookupTable]
AFTER DELETE
AS

DECLARE @CompanyID int
DECLARE @CompanyCode varchar(5)
DECLARE @LocationCode varchar(10)
DECLARE @TableName varchar(100)
DECLARE @Comments varchar(255)


/*  Gets values of fields in the source table being deleted*/
select
@CompanyID=CompanyID,
@LocationCode=LOCATION_CODE
from inserted

/*Set CompanyCode*/
SET @CompanyCode = (Select CompanyCode from dbo.LVRG_CTRL_CompanyTable WHERE CompanyID = @CompanyID)
SET @TableName = 'LVRG_CTRL_SiteLookupTable'
SET @Comments = 'Record Deleted for Company ' + @CompanyCode + ' and Location_Code value: ' + @LocationCode


/*Insert Values into*/
insert into dbo.LVRG_CTRL_ControlTableChangeLog(TableName,Comments)
select @TableName,@Comments



As I mentioned the Insert and Update Triggers look pretty much the same and work fine.  Any help would be appreciated.

Answer : SQL Server Delete Trigger, want to retain values of record being deleted

ALTER TRIGGER [dbo].[trig_SiteLookupDelete]ON [dbo].[LVRG_CTRL_SiteLookupTable]
AFTER DELETE
AS

insert into dbo.LVRG_CTRL_ControlTableChangeLog(TableName,Comments)
select 'LVRG_CTRL_SiteLookupTable','Record Deleted for Company ' + d.CompanyCode + ' and Location_Code value: ' + d.LocationCode
from DELETED d
JOIN dbo.LVRG_CTRL_CompanyTable t on d.companyid = t.companyid  --or whatever common fields.
Random Solutions  
 
programming4us programming4us