--Main table--
CREATE TABLE [dbo].[Employee_Test]
(
[Emp_ID] [int] IDENTITY(1,1) NOT NULL,
[Emp_name] [text] NULL,
[Emp_Sal] [int] NULL,
[Address1] [varchar](100) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
--Trigger table--
CREATE TABLE [dbo].[Emp_trigg]
(
[Name] [varchar](max) NULL,
[Old_name] [varchar](max) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
--Trigger--
CREATE TABLE [dbo].[Employee_Test]
(
[Emp_ID] [int] IDENTITY(1,1) NOT NULL,
[Emp_name] [text] NULL,
[Emp_Sal] [int] NULL,
[Address1] [varchar](100) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
--Trigger table--
CREATE TABLE [dbo].[Emp_trigg]
(
[Name] [varchar](max) NULL,
[Old_name] [varchar](max) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
--Trigger--
ALTER TRIGGER [dbo].[FieldUpdated]
ON [dbo].[Employee_Test]
INSTEAD OF UPDATE
AS
UPDATE
[Employee_Test] SET Emp_name = (SELECT Emp_name FROM Inserted)
WHERE Emp_ID = (SELECT Emp_ID FROM Inserted)
IF (UPDATE (Emp_name))
BEGIN
DECLARE
@oldValue nvarchar(max)
DECLARE
@newValue nvarchar(max)
SET
@newValue = (SELECT CONVERT(nvarchar(max), Emp_name) FROM Inserted)
SET @oldValue = (SELECT CONVERT(nvarchar(max), Emp_name) FROM Deleted)
IF (@oldValue != @newValue)
BEGIN
insert
into Emp_trigg(Name,Old_name) values(@newValue,@oldValue)
END
END
No comments:
Post a Comment