This is the code in the instead of insert trigger
ALTER TRIGGER [Cust_Audit] ON [dbo].[Customer]
INSTEAD OF INSERT
AS
DECLARE @CustNum int
DECLARE @CustomerCode INT,@CustomerIdentificationType INT
DECLARE @CustomerIdentificationGroupCode CHAR(50),@DocumentNumber CHAR(50)
DECLARE @FirstName CHAR(50),@LastName CHAR(50),@MiddleName CHAR(50)
DECLARE @CustomerName CHAR(50)
BEGIN
SET @CustomerCode = (select customercode from inserted)
SET @CustomerIdentificationType = (select CustomerIdentificationType from inserted)
SET @CustomerIdentificationGroupCode = (select CustomerIdentificationGroupCode from inserted)
SET @DocumentNumber = (select DocumentNumber from inserted)
SET @FirstName = (select FirstName from inserted)
SET @LastName = (select LastName from inserted)
SET @MiddleName = (select MiddleName from inserted)
SET @CustomerName =(select CustomerName from inserted)
END
Set @CustNum =(Select top 1 cc.CustomerCode From Customer cc, inserted i WHERE cc.CustomerCode = i.CustomerCode)
--set nocount off
If ISNULL(@CustNum,'')=''
BEGIN
INSERT INTO CUSTOMER(CustomerCode,CustomerIdentificationType ,
CustomerIdentificationGroupCode,DocumentNumber,
FirstName,LastName,MiddleName,
CustomerName)
values(@CustomerCode,@CustomerIdentificationType,@ CustomerIdentificationGroupCode,@DocumentNumber,
@FirstName,@LastName,@MiddleName,@CustomerName)
end
SELECT CustomerCode FROM Customer where CustomerCode = @CustomerCode
INSERT INTO
SysDatabase_Transactions( TableName,Operation,TransferDateTime)
Select b.CustomerCode,'Customer','I',Getdate()
From Inserted b
--END
ELSE
BEGIN
UPDATE Customer
SET CustomerCode = @CustomerCode,CustomerIdentificationType = @CustomerIdentificationType,
CustomerIdentificationGroupCode = @CustomerIdentificationGroupCode,
DocumentNumber = @DocumentNumber,FirstName=@FirstName,LastName=@Las tName,
MiddleName=@MiddleName,CustomerName=@CustomerName WHERE CustomerCode = @CustomerCode
--END
SELECT CustomerCode FROM Customer where CustomerCode = @CustNum
IF @@ROWCOUNT <> 0
INSERT INTO
SysDatabase_Transactions( Record_Id,TableName,Operation,TransferDateTime)
Select b.CustomerCode,'Customer','U',Getdate()
From Inserted b
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO