为什么是[日期] + ([时间] - [偏移]) SQL Server 2008 中的不确定性?

发布于 2024-09-06 01:52:17 字数 615 浏览 12 评论 0原文

我正在尝试对 IIS 日志表执行以下操作:

ALTER TABLE [W3CLog]
ADD [LogTime] AS [date] + ([time] - '1900-01-01') PERSISTED

但是,SQL Server 2008 告诉我:

Computed column 'LogTime' in table 'W3CLog' cannot be persisted because the column is non-deterministic.

该表具有以下定义:

CREATE TABLE [dbo].[W3CLog](
    [Id] [bigint] IDENTITY(1,1) NOT NULL,
    ...
    [date] [datetime] NULL,
    [time] [datetime] NULL,
    ...
)

为什么是非确定性的?

我确实需要对该字段建立索引。该表目前有 1598170 行,如果我们不能全程进行索引查找,那么查询会很痛苦。由于这是与其他一些日志格式进行 UNION 的,因此我们不能很容易地单独使用这两列。

I'm trying to do the following for my IIS logs table:

ALTER TABLE [W3CLog]
ADD [LogTime] AS [date] + ([time] - '1900-01-01') PERSISTED

However, SQL Server 2008 tells me:

Computed column 'LogTime' in table 'W3CLog' cannot be persisted because the column is non-deterministic.

The table has this definition:

CREATE TABLE [dbo].[W3CLog](
    [Id] [bigint] IDENTITY(1,1) NOT NULL,
    ...
    [date] [datetime] NULL,
    [time] [datetime] NULL,
    ...
)

Why is that non-deterministic?

I really need to index that field. The table currently has 1598170 rows, and it is a pain to query if we can't do an index seek on the full time. Since this is being UNION'd with some other log formats, we can't very easily just use the two columns separately.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静待花开 2024-09-13 01:52:17

您的 '1900-01-01' 是不确定的,因为它取决于语言设置。当然,这对于 DMY 或 MDY 设置来说是明确的,但一般来说,它是不明确的。

尝试 '19000101':SQL Server 处理日期和时间的方式有些奇怪:“yyyy-mm-dd”可以被视为“yyyy-” dd-mm" 如果你有英国设置,尽管理论上是 ISO

编辑:使用它来删除日期方面: DATEADD(day, 0, DATEDIFF(day, 0, [time]))

Edit2: 1900 年 1 月 1 日在日期时间格式中为零,因此无需减去它。您可以发布示例数据和输出吗?

Your '1900-01-01' is non-deterministic because it depends on language settings. of course, this is unambiguous for DMY or MDY settings bit generally it's ambiguous

Try '19000101': SQL Server treats dates and times somewhat oddly: "yyyy-mm-dd" can be treated as "yyyy-dd-mm" if you have British settings despite being ISO in theory

Edit: use this to remove the date aspect: DATEADD(day, 0, DATEDIFF(day, 0, [time]))

Edit2: 1st Jan 1900 is zero in the datetime formats, so no need to subtract it. Can you post sample data and output please?

青丝拂面 2024-09-13 01:52:17

好吧,感谢 @gbn 的回答,我明白了。

以下三者是等价的:

SELECT TOP 100
    [date] + ([time] - '19000101'),
    [date] + ([time] - 0),
    [date] + [time]
FROM
    dbo.[W3CLog]

底部是确定性的。

Alright, I figured it out, thanks to @gbn's answer.

The following three are equivalent:

SELECT TOP 100
    [date] + ([time] - '19000101'),
    [date] + ([time] - 0),
    [date] + [time]
FROM
    dbo.[W3CLog]

The bottom is deterministic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文