Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 create the create date and last updated date???

Author  Topic 

yvette
Yak Posting Veteran

74 Posts

Posted - 2010-09-17 : 00:37:40
Hi,
i have a database name call cw and have some table in cw example like Employee1. Inside Employee1, i have EmployeeID,...,CreatedDate and LastUpdated.
the created Date and Last Updated is use for when insert data or update data,it will record of current data.

Here is my sql script, but doesn't work.
Anyone can help???? or have another good way to do it???

CREATE TRIGGER tr[Employee1]CreateDate ON [Employee1]
FOR INSERT
AS
UPDATE [Employee1] SET [Employee1].CreatedDate=getdate()
FROM [Employee1] INNER JOIN Inserted ON [Employee1].[EmployeeID]= Inserted.[EmployeeID]

GO

CREATE TRIGGER tr[Employee1]LastUpdatedDate ON [Employee1]
FOR UPDATE
AS
UPDATE [Employee1] SET [Employee1].LastUpdated=getdate()
FROM [Employee1] INNER JOIN Inserted ON [Employee1].[EmployeeID]= Inserted.[EmployeeID]

Thanks...

Kristen
Test

22859 Posts

Posted - 2010-09-17 : 01:42:33
Looks fine to me, except I have a doubt about the square brackets in the Trigger Name (dunno if that is in your code, or just the example you have posted here?)

tr[Employee1]CreateDate
Go to Top of Page

yvette
Yak Posting Veteran

74 Posts

Posted - 2010-09-17 : 03:04:20
quote:
Originally posted by Kristen

Looks fine to me, except I have a doubt about the square brackets in the Trigger Name (dunno if that is in your code, or just the example you have posted here?)

tr[Employee1]CreateDate



Thanks for reply...
but i will have this error:
Msg 8197, Level 16, State 4, Procedure trEmployeeCreateDate, Line 1
The object 'Employee' does not exist or is invalid for this operation.
Msg 8197, Level 16, State 4, Procedure trEmployeeLastUpdatedDate, Line 2
The object 'Employee' does not exist or is invalid for this operation.
Go to Top of Page

yvette
Yak Posting Veteran

74 Posts

Posted - 2010-09-17 : 03:05:31
quote:
Originally posted by yvette

quote:
Originally posted by Kristen

Looks fine to me, except I have a doubt about the square brackets in the Trigger Name (dunno if that is in your code, or just the example you have posted here?)

tr[Employee1]CreateDate



Thanks for reply...
but i will have this error:
Msg 8197, Level 16, State 4, Procedure trEmployeeCreateDate, Line 1
The object 'Employee' does not exist or is invalid for this operation.
Msg 8197, Level 16, State 4, Procedure trEmployeeLastUpdatedDate, Line 2
The object 'Employee' does not exist or is invalid for this operation.



i change my table name to Employee.
Go to Top of Page
   

- Advertisement -