【问题标题】:Divide by zero error encountered - SQL Server [closed]遇到除以零错误-SQL Server [关闭]
【发布时间】:2017-08-22 18:51:02
【问题描述】:

我正在使用以下示例代码在 SQL Server 表中生成随机 DateTime

示例代码:

DECLARE @SeedInt INT = 0;
SELECT DATEADD(minute,(-1 * (ABS(Checksum(NewID()) % (CASE WHEN @SeedInt IS NULL OR @SeedInt <= 0 THEN 2 ELSE @SeedInt END - 1))) + 1), SYSUTCDATETIME());

它工作正常。但是我在 UPDATE Query 中尝试了相同的方法,它无法更新并引发异常

表格架构:StudentMark

CREATE TABLE [dbo].[StudentMark]
(
    [StudentMarkId] [int] IDENTITY(1,1) NOT NULL,
    [StudentId] [uniqueidentifier] NOT NULL,
    [SubjectId] [uniqueidentifier] NOT NULL,
    [Score] [int] NOT NULL,
    [ScoreInfo] [xml] NOT NULL,
    [GeneratedOn] [datetime2](2) NOT NULL,
    [IsPass] [bit] NOT NULL,
    CONSTRAINT [PK_StudentMark] 
       PRIMARY KEY CLUSTERED ([StudentMarkId] ASC)
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

样本种子数据

INSERT INTO [dbo].[StudentMark] ([StudentId], [SubjectId], [ScoreInfo], [GeneratedOn], [Score], [IsPass])
VALUES ('FC3CB475-B480-4129-9190-6DE880E2D581', '0D72F79E-FB48-4D3E-9906-B78A9D105081', '<StudentMarkAttribute xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"></StudentMarkAttribute>', '2017-08-10 10:10:15', 95, 1),
       ('0F4EF48C-93E3-41AA-8295-F6B0E8D8C3A2', '0D72F79E-FB48-4D3E-9906-B78A9D105081', '<StudentMarkAttribute xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"></StudentMarkAttribute>', '2017-08-10 10:10:15', 100, 1),
       ('0F4EF48C-93E3-41AA-8295-F6B0E8D8C3A2', 'AB172272-D2E9-49E1-8040-6117BB6743DB', '<StudentMarkAttribute xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"></StudentMarkAttribute>', '2017-08-16 09:06:20', 25, 0),
       ('FC3CB475-B480-4129-9190-6DE880E2D581', 'AB172272-D2E9-49E1-8040-6117BB6743DB', '<StudentMarkAttribute xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"></StudentMarkAttribute>', '2017-08-16 09:06:20', 82, 1);

更新查询:(SQL Server)

UPDATE SInfo
    SET
        GeneratedOn = DATEADD(minute,(-1 * (ABS(Checksum(NewID()) % (CASE WHEN SInfo.StudentMarkId IS NULL OR SInfo.StudentMarkId <= 0 THEN 2 ELSE SInfo.StudentMarkId END - 1))) + 1), SYSUTCDATETIME())
    FROM dbo.StudentMark AS SInfo

但是查询抛出异常

消息 8134,第 16 级,状态 1,第 1 行 遇到除以零错误。 该语句已终止。

请帮助我查询中有什么问题,需要做些什么来解决这个问题?

【问题讨论】:

  • 如果 StudentMarkId 为 1,则减去 1 后将得到 0。要修复,不要除以零。我无法开始理解该计算试图完成什么,所以我不知道确切需要什么
  • @Ghost - 在我的示例代码中,我使用DECLARE @SeedInt INT = 0; 生成了DateTime 显示它将如何处理问题?
  • 将种子值设置为 1,您将得到除以零
  • @Ghost - 是的,你是对的。非常感谢...

标签: sql sql-server sql-server-2016 divide-by-zero dateadd


【解决方案1】:

% 操作数为 0 时会发生这种情况。因此,请使用 NULLIF()

UPDATE SInfo
    SET GeneratedOn = DATEADD(minute,
                              (-1 * (ABS(Checksum(NewID()) % NULLIF(CASE WHEN SInfo.StudentMarkId IS NULL OR SInfo.StudentMarkId <= 0 THEN 2 ELSE SInfo.StudentMarkId END - 1), 0)) + 1)), SYSUTCDATETIME())
    FROM dbo.StudentMark SInfo;

【讨论】:

  • 请检查查询,它有语法问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多