I have a base table and audit table are like this
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Mytest](
[TestID] Int NOT NULL,
[TestDesc] [varchar](35) NOT NULL,
[EffBegDate] [datetime] NOT NULL,
[EffEndDate] [datetime] NOT NULL,
PRIMARY KEY CLUSTERED
(
[TestId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
and the same kind for my audit.
INSERT INTO MyTest (
TestId,TestDesc,EffBegDate,EffEndDate
)
Values (1,'test',getdate(), getdate());
INSERT INTO MyTest (
TestId,TestDesc,EffBegDate,EffEndDate
)
Values (2,'test2','20080101', '99991231');
Have an update trigger like this
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create TRIGGER MyTest_update
ON MyTest
for update
as
BEGIN
INSERT INTO Audit_MyTest (
TestId,TestDesc,EffBegDate,EffEndDate
)
-- Values (1,'test',getdate(), getdate())
(select
TestId,TestDesc,EffBegDate,EffEnddate from Inserted)
END;
Update
Update Mytest
set TestDesc='test2_updt'
where testid=2;
But what I want is when ever there is an update on the base table, I want to insert into the audit table with effenddate as the effbegdate of the base table record.
How do I do this?
TIA
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Mytest](
[TestID] Int NOT NULL,
[TestDesc] [varchar](35) NOT NULL,
[EffBegDate] [datetime] NOT NULL,
[EffEndDate] [datetime] NOT NULL,
PRIMARY KEY CLUSTERED
(
[TestId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
and the same kind for my audit.
INSERT INTO MyTest (
TestId,TestDesc,EffBegDate,EffEndDate
)
Values (1,'test',getdate(), getdate());
INSERT INTO MyTest (
TestId,TestDesc,EffBegDate,EffEndDate
)
Values (2,'test2','20080101', '99991231');
Have an update trigger like this
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create TRIGGER MyTest_update
ON MyTest
for update
as
BEGIN
INSERT INTO Audit_MyTest (
TestId,TestDesc,EffBegDate,EffEndDate
)
-- Values (1,'test',getdate(), getdate())
(select
TestId,TestDesc,EffBegDate,EffEnddate from Inserted)
END;
Update
Update Mytest
set TestDesc='test2_updt'
where testid=2;
But what I want is when ever there is an update on the base table, I want to insert into the audit table with effenddate as the effbegdate of the base table record.
How do I do this?
TIA