0

我们有一个设计为在 MS SQL 2005 上运行的软件,我们已将数据库升级到 2008 R2,并试图通过使用 Datetime2 而不是 Datetime 来提高时间戳的准确性。运行的软件仍然没有变化,并且将具有日期时间的准确性。但是有一个额外的字段用正确的 ms 更新。

所以我的问题是;对于采用 datetime2 并用正确值替换 MS 部分的触发器是否有任何最佳实践?我希望避免对杯子要求很高的转换为字符串并再次返回,但一直无法找到一个好的函数。

4

1 回答 1

0

This seems to do the job:

create table T (ID int not null, Stamp1 datetime2 not null,
                RealMillis int not null);
go
create trigger TT on T
instead of insert
as
    set nocount on;
    insert into T(ID,Stamp1,RealMillis)
    select
        ID,
        DATEADD(millisecond,RealMillis - DATEPART(millisecond,Stamp1),Stamp1),
        RealMillis --or 0 now that it's been used?
    from inserted;
go
insert into T(ID,Stamp1,RealMillis) values (1,'2014-03-14T10:01:29.343',344);
go
select * from T;

I chose to do it as an instead of trigger since it avoids having to write the wrong data to the table in the first place.

于 2014-03-14T10:03:43.200 回答