Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old January 23rd, 2006, 03:37 PM
Authorized User
Join Date: Jun 2003
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to kev_79
Default trigger to insert current date on insert

I am trying to write a trigger that inserts the date when a record is inserted. I have not found any good examples on how to do this. Can anyone point me in the right direction.

Old January 23rd, 2006, 04:52 PM
SQLScott's Avatar
Wrox Author
Join Date: Dec 2004
Posts: 338
Thanks: 0
Thanked 2 Times in 2 Posts

   ON table_name
   AFTER insert

insert into table (column1, column2, columnn3)
SELECT column1, column2, column3 from inserted

Old January 23rd, 2006, 04:54 PM
SQLScott's Avatar
Wrox Author
Join Date: Dec 2004
Posts: 338
Thanks: 0
Thanked 2 Times in 2 Posts

Oops, sorry. Misread that. Why do you need a trigger? Put a default on the column you need the date inserted in to.

Old January 23rd, 2006, 05:58 PM
Friend of Wrox
Join Date: Dec 2005
Posts: 146
Thanks: 0
Thanked 1 Time in 1 Post

--IF you want to accomodate updates then
CREATE TRIGGER trg_myTable_ModDate
  ON myTable

UPDATE myTable set ModDate = GETDATE()
FROM myTable
JOIN inserted i
ON i.ID = myTable.ID -- where ID is your primary key

David Lundell
Principal Consultant and Trainer

Similar Threads
Thread Thread Starter Forum Replies Last Post
insert current date vsempoux XSLT 10 July 11th, 2008 07:31 AM
Insert Trigger debbiecoates SQL Server 2000 3 December 24th, 2007 08:00 AM
insert,update trigger rolle SQL Server 2000 3 September 29th, 2005 08:41 PM
Insert Trigger Arsi SQL Server 2000 2 February 25th, 2005 05:41 PM
Insert Update Trigger mstuart60 SQL Server 2000 10 September 30th, 2003 06:54 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.