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
 Getdate() in insert trigger

Author  Topic 

psangeetha
Yak Posting Veteran

95 Posts

Posted - 2008-06-10 : 09:51:12
Hi all,

I am trying to create a trigger for a table, when a record is inserted into the table, the currdate_time field should be inserted with the current date and time. I tried below in Oracle

create trigger test_trg
before insert on test
for each row
begin
:new.curr_datetime := sysdate;
end;
/


For Sql server, I tried the below,

create table test (num smallint, curr_datetime datetime)
go

create trigger test_trg
on test
for insert
as

insert into test (curr_datetime) select getdate() from dual


I inserted a record into test:

insert into test (num) values (8)

select * from test

num curr_datetime

8 NULL
NULL 2008-06-10 09:49:06:317

Trigger is inserting NULL values. Please give me your suggestions.Thanks.


jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2008-06-10 : 09:58:35
Why use a trigger at all?
INSERT INTO test(num,curr_datetime)
VALUES(8,getdate())

Jim
Go to Top of Page

psangeetha
Yak Posting Veteran

95 Posts

Posted - 2008-06-10 : 09:59:13
I tried the below trigger:


create trigger test_trg
on test
for insert
as
declare @dt datetime

set @dt = getdate()

insert into test (num,curr_datetime) select num, @dt from test


Inserted a record:


insert into test (num) values (7)

select * from test

num curr_datetime

7 NULL
7 2008-06-10 09:58:20:887

Please help. Thanks much.
Go to Top of Page

psangeetha
Yak Posting Veteran

95 Posts

Posted - 2008-06-10 : 10:04:16
THanks,Jim.

The business people will insert a record from the application, so we are trying to create trigger at the database end, so when a record is inserted it will insert the current datetime as well.
Go to Top of Page

psangeetha
Yak Posting Veteran

95 Posts

Posted - 2008-06-10 : 10:12:31
Is there a way to insert a record without the NULL value in the curr_datetime field? THe below trigger I used is inserting a record with NULL value and inserting 2nd record with the currect date.


create trigger test_trg
on test
for insert
as
declare @dt datetime

set @dt = getdate()

insert into test (num,curr_datetime) select num, @dt from test


Thank you for all your help.
Go to Top of Page

blindman
Master Smack Fu Yak Hacker

2365 Posts

Posted - 2008-06-10 : 10:14:59
You only need the trigger if you want the value modified when a record is updated. For the problem you have described, which involves setting the value on inserts only, a simple default on the column will work.

e4 d5 xd5 Nf6
Go to Top of Page

psangeetha
Yak Posting Veteran

95 Posts

Posted - 2008-06-10 : 10:18:58
Can you please tell me how I can do it? Not sure how to do that.

Did you mean set the column to default?

Thank you
Go to Top of Page

psangeetha
Yak Posting Veteran

95 Posts

Posted - 2008-06-10 : 10:21:35
Ok.I got it.

create table test (num smallint not null,curr_datetime datetime default getdate())

Thanks very much.
Go to Top of Page

psangeetha
Yak Posting Veteran

95 Posts

Posted - 2008-06-16 : 11:37:16
Hi all,

I altered the column curr_datetime to have default as 'getdate()'.
It works fine when I insert a record into the table at the database end. But when the application inserts a record it does not show the time(only enters the date).

I guess, at this poiint we have to go with an insert trigger.

create trigger test_trg
on test
for insert
as
declare @dt datetime

set @dt = getdate()

insert into test (num,curr_datetime) select num, @dt from test


The above trigger inserts the record with NULL value and inserting 2nd record with the correct date. So its basically inserting 2 records 2nd record with the correct date and the 1st record with NULL value.
Please give me your suggestions. Thank you
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-16 : 11:46:19
quote:
Originally posted by psangeetha

Hi all,

I altered the column curr_datetime to have default as 'getdate()'.
It works fine when I insert a record into the table at the database end. But when the application inserts a record it does not show the time(only enters the date).

I guess, at this poiint we have to go with an insert trigger.

create trigger test_trg
on test
for insert
as
declare @dt datetime

set @dt = getdate()

insert into test (num,curr_datetime) select num, @dt from test


The above trigger inserts the record with NULL value and inserting 2nd record with the correct date. So its basically inserting 2 records 2nd record with the correct date and the 1st record with NULL value.
Please give me your suggestions. Thank you


Why? It wont be problem unless you're explicitly trying to pass a date alone value from application. The default constraint will always populate current date value unless you pass a value for date field. And GETDATE() always returns time along with date. check this

SELECT GETDATE()

I firmly believe you're trying to explicitly pass a date value from your application somewhere.
Go to Top of Page

psangeetha
Yak Posting Veteran

95 Posts

Posted - 2008-06-16 : 11:52:35
Thanks for your reply, Visakh. I am not sure how the record is been inserted from the application. But when the application people insert a record, this column doesnt show the time in the database.
But when I insert a record from the database, it does show the date and time. It is weird but the application people would like to have a insert trigger for this instead of getdate() as default.

Since I do not have exposure on the application side, I have to create an insert trigger and check if that will work. Please help.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-16 : 12:05:57
quote:
Originally posted by psangeetha

Thanks for your reply, Visakh. I am not sure how the record is been inserted from the application. But when the application people insert a record, this column doesnt show the time in the database.
But when I insert a record from the database, it does show the date and time. It is weird but the application people would like to have a insert trigger for this instead of getdate() as default.

Since I do not have exposure on the application side, I have to create an insert trigger and check if that will work. Please help.


Ok. But keep in mind that insert trigger will affect the performance.

in that case change you trigger like this

create trigger test_trg
on test
for insert
as

update t
set t.curr_datetime=getdate()
from test t
inner join inserted i
on i.pk=t.pk

go

where pk is your tables primary key.
Go to Top of Page

psangeetha
Yak Posting Veteran

95 Posts

Posted - 2008-06-16 : 12:20:19
Thanks, Visakh. I tried the trigger you mentioned it works fine.
But when I insert a record it says 2 row(2) affected.
But when I select the table, it only inserted 1 record with the correct date and time. Is it something I should be concerned of?

create table test (num smallint not null, curr_datetime datetime)

insert into test (num) values (2)

select * from test

num curr_datetime
2 2008-06-16 12:14:46.243

create trigger test_trg
on test
for insert
as

update t
set t.curr_datetime=getdate()
from test t
inner join inserted i
on i.num=t.num

go


insert into test (num) values (90)

1 row(s) affected

1 row(s) affected

select * from test

num curr_datetime
2 2008-06-16 12:14:46.243
9 2008-06-16 12:15:23.340

Any suggestions? Thanks a lot



Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-16 : 12:27:55
quote:
Originally posted by psangeetha

Thanks, Visakh. I tried the trigger you mentioned it works fine.
But when I insert a record it says 2 row(2) affected.
But when I select the table, it only inserted 1 record with the correct date and time. Is it something I should be concerned of?

create table test (num smallint not null, curr_datetime datetime)

insert into test (num) values (2)

select * from test

num curr_datetime
2 2008-06-16 12:14:46.243

create trigger test_trg
on test
for insert
as

update t
set t.curr_datetime=getdate()
from test t
inner join inserted i
on i.num=t.num

go


insert into test (num) values (90)

1 row(s) affected

1 row(s) affected

select * from test

num curr_datetime
2 2008-06-16 12:14:46.243
9 2008-06-16 12:15:23.340

Any suggestions? Thanks a lot






the second 1 row affected is for update done by trigger. dont worry about it.
Go to Top of Page

psangeetha
Yak Posting Veteran

95 Posts

Posted - 2008-06-16 : 12:44:45
Oh thanks, Visakh.
Go to Top of Page

blindman
Master Smack Fu Yak Hacker

2365 Posts

Posted - 2008-06-16 : 13:22:04
quote:
Originally posted by psangeetha
I altered the column curr_datetime to have default as 'getdate()'.
It works fine when I insert a record into the table at the database end. But when the application inserts a record it does not show the time(only enters the date).
The application should not be entering ANYTHING into this field. Not even "NULL". It should not even in include it in the column list.
Writing a trigger to do this is VERY sloppy design.

e4 d5 xd5 Nf6
Go to Top of Page

JimBrent
Starting Member

1 Post

Posted - 2008-07-23 : 23:26:58
Hi Blindman,

Ahhh... So what is the preferred way to enter in the time the record is created in the db?

Supposedly it isn't considered good design to send a SysDate from the application over the wire just to insert the record creation date and time in the db.

So what is a good design for this functionality?

Please let me know.

Thanks,

Jim

Jim
Go to Top of Page

blindman
Master Smack Fu Yak Hacker

2365 Posts

Posted - 2008-07-23 : 23:56:01
Set the default for the column to getdate().

e4 d5 xd5 Nf6
Go to Top of Page
   

- Advertisement -