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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 update modifydate and insertdate column by trigger

Author  Topic 

sachingovekar
Posting Yak Master

101 Posts

Posted - 2009-11-15 : 03:27:23
Hi,

I want to update the modifydate column to get current date whenever a particular record is update.
I want to update the insertdate column to get current date whenever a new record is inserted, but the modifydate should be null as we are only inserting data and not modifying it.

How to do it by trigger? any other best way of doing it?

create table dbo.table1
(
emp_id int,
empname varchar(100),
department varchar(100),
insertdate datetime,
modifydate datetime
)

insert into dbo.table1 VALUES(1,'richard','finance',null,null)
insert into dbo.table1 VALUES(2,'stewart','marketing',null,null)
insert into dbo.table1 VALUES(3,'andrew','IT',null,null)

SELECT * FROM dbo.TABLE1

Regards,
Sachin

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2009-11-15 : 07:15:26
You can do it with out trigger,

Add it in insert and Update Statement.


Insert

insert into dbo.table1 VALUES(1,'richard','finance',getdate(),null)

Update

Update set col=value,col2=value,modifydate=getdate() where condition..

Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/
Go to Top of Page
   

- Advertisement -