Dear Friends,
I want to insert in my database, in a table field the system time value on that moment.
For example: I want to create the follow stored procedure:
CREATE PROCEDURE TEST
@.ID INT
AS
UPDATE TABLE1 SET MyFieldTime=@.MySystemTime WHERE MyFieldID=@.ID
I want to save in my database th system time...
Thanks!!
UPDATE TABLE1 SET MyFieldTime=getdate() WHERE MyFieldID=@.ID|||it always good practice to use the UTC time instead System's local time. In future if you transfer the data from one server (time zone) to another you need not to applay any changes..
Another benifit on UI you can convert to any Local time from UTC with out any overhead...
use the following query..
Update Table1 Set MyFiedlTime = GetUTCDate() Where MyFieldId = @.ID
|||You should do this using a DEFAULT on the column and then use DEFAULT keyword in the SET clause of whatever UPDATE statement that modifies the data. Using a separate SP is not really a good idea since you will decouple the actual update and the time when it was done. It is also costly to perform multiple updates on the same row when you can do it once.
You can make below changes:
alter table TABLE1 add default( CURRENT_TIMESTAMP ) for MyFieldTime
After that when you do the actual UPDATE then do:
UPDATE TABLE1
SET col1 = ...
, col2 = ...
, MyFieldTime = DEFAULT
WHERE ...
No comments:
Post a Comment