代码之家  ›  专栏  ›  技术社区  ›  i.n.n.m

DATEDIFF不返回整数值T-SQL

  •  1
  • i.n.n.m  · 技术社区  · 8 年前

    我想看看在SQL Server 2012中向表中插入一些值需要多长时间。

    这就是我要做的:

    DECLARE @starttime DATETIME
    DECLARE @timetook DATETIME
    
    CREATE TABLE MyTable (id INT, name CHAR(10));
    
    SET @starttime = SYSDATETIME()
    
    INSERT INTO MyTable (id, name) 
    VALUES (1, 'Bob'), (2, 'Peter'), (3, 'Joe'),
           (4, 'Boby'), (5, 'Peters'), (6, 'Joey'),
           (7, 'Bobs'), (8, 'Petery'), (9, 'Joes');
    
    SET @timetook = DATEDIFF(ss, @starttime, SYSDATETIME())
    
    SELECT @timetook
    

    此返回 1900-01-01 00:00:00.000 . 根据 datediff docs ,应该是 00:00:00.000 或整数值。由于我是以秒为单位,所以我应该以秒为单位获得数字。(本例中应为0,但不含1900-01-01)

    我错过了什么?如有任何建议,将不胜感激。

    1 回复  |  直到 8 年前
        1
  •  3
  •   Jacob H    8 年前

    将@TimeTake声明为datetime将为您提供datetimeresult。datetime=0表示 1900-01-01 00:00:00.000 .

    declare @starttime datetime
    declare @timetook int --Issue was this declaration as datetime
    
    CREATE TABLE MyTable (id int, name char(10));
    
    set @starttime = SYSDATETIME()
    
    INSERT INTO MyTable (id, name) VALUES (1, 'Bob'), (2, 'Peter'), (3, 'Joe'),
                                          (4, 'Boby'), (5, 'Peters'), (6, 'Joey'),
                                          (7, 'Bobs'), (8, 'Petery'), (9, 'Joes');
    set @timetook = DATEDIFF(ss, @starttime, SYSDATETIME())
    
    select @timetook
    

    通过将@timetake声明为int,您将得到您要查找的结果: 0