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 Oraclecreate trigger test_trgbefore insert on testfor each rowbegin :new.curr_datetime := sysdate; end;/ For Sql server, I tried the below, create table test (num smallint, curr_datetime datetime)gocreate trigger test_trgon testfor insert asinsert into test (curr_datetime) select getdate() from dual I inserted a record into test:insert into test (num) values (8)select * from testnum curr_datetime8 NULLNULL 2008-06-10 09:49:06:317Trigger 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 |
 |
|
psangeetha
Yak Posting Veteran
95 Posts |
Posted - 2008-06-10 : 09:59:13
|
I tried the below trigger:create trigger test_trgon testfor insert asdeclare @dt datetimeset @dt = getdate() insert into test (num,curr_datetime) select num, @dt from test Inserted a record:insert into test (num) values (7)select * from testnum curr_datetime7 NULL7 2008-06-10 09:58:20:887Please help. Thanks much. |
 |
|
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. |
 |
|
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_trgon testfor insert asdeclare @dt datetimeset @dt = getdate() insert into test (num,curr_datetime) select num, @dt from test Thank you for all your help. |
 |
|
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 |
 |
|
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 |
 |
|
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. |
 |
|
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_trgon testfor insert asdeclare @dt datetimeset @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 |
 |
|
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_trgon testfor insert asdeclare @dt datetimeset @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 thisSELECT GETDATE()I firmly believe you're trying to explicitly pass a date value from your application somewhere. |
 |
|
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. |
 |
|
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 thiscreate trigger test_trgon testfor insert asupdate tset t.curr_datetime=getdate() from test tinner join inserted ion i.pk=t.pkgo where pk is your tables primary key. |
 |
|
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 testnum curr_datetime2 2008-06-16 12:14:46.243create trigger test_trgon testfor insert asupdate tset t.curr_datetime=getdate() from test tinner join inserted ion i.num=t.numgo insert into test (num) values (90)1 row(s) affected1 row(s) affectedselect * from testnum curr_datetime2 2008-06-16 12:14:46.2439 2008-06-16 12:15:23.340Any suggestions? Thanks a lot |
 |
|
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 testnum curr_datetime2 2008-06-16 12:14:46.243create trigger test_trgon testfor insert asupdate tset t.curr_datetime=getdate() from test tinner join inserted ion i.num=t.numgo insert into test (num) values (90)1 row(s) affected1 row(s) affectedselect * from testnum curr_datetime2 2008-06-16 12:14:46.2439 2008-06-16 12:15:23.340Any suggestions? Thanks a lot
the second 1 row affected is for update done by trigger. dont worry about it. |
 |
|
psangeetha
Yak Posting Veteran
95 Posts |
Posted - 2008-06-16 : 12:44:45
|
Oh thanks, Visakh. |
 |
|
blindman
Master Smack Fu Yak Hacker
2365 Posts |
Posted - 2008-06-16 : 13:22:04
|
quote: Originally posted by psangeethaI 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 |
 |
|
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,JimJim |
 |
|
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 |
 |
|
|