• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Simple T-SQL Trigger question

Bulldog13

Golden Member
I have a table, TABLE_A.

TABLE_A has 3 fields. PK (auto, identity), Name(varchar(50)), and Date_Created (datetime).

When I insert a row into TABLE_A, PK is automatically generated. Name is passed in via a variable in a stored procedure (basic insert statement).

I would like a trigger to automatically fill in the current date time into the Date_Created field.

Here is what I have so far:

CREATE TRIGGER INSERT_TABLE_A

on TABLE_A

for INSERT AS

DECLARE
@NOW DateTime
set @NOW = getdate()


This is where I get confused....how do I insert the current date time into the newly created record?

Thanks in advance

 
You can, of course, do this with a trigger, and it's not difficult at all. But you should use the Default value feature and set Date_Created column's default value to be getdate().
 
If you are adamant about the trigger, it would not actually be an insert, but rather, an update:
CREATE TRIGGER INSERT_TABLE_A

on TABLE_A

for INSERT AS

UPDATE TABLE_A SET Date_Created = getdate() where PK IN (SELECT PK FROM inserted)
 
Back
Top