Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update the Audit Table with an update Trigger

Status
Not open for further replies.

manmaria

Technical User
Aug 8, 2003
286
0
0
US
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top