SQL CHECK 约束问题

发布于 2024-08-29 03:21:50 字数 527 浏览 5 评论 0原文

我使用的是 SQL Server 2008,并且有一个包含三列的表:LengthStartTimeEndTime。我想在这个表上创建一个 CHECK 约束,它表示:

if Length == NULL then
  StartTime <> NULL and EndTime <> NULL
else
  StartTime == NULL and EndTime == NULL

我已经开始尝试这样的事情:

Length == NULL AND StartTime <> NULL AND EndTime <> NULL

显然这还不够,但即使这个简单的表达式也不会验证。我收到错误:

“验证‘CK_Test_Length_Or_Time’时出错。您要编辑约束吗?”

关于如何去做这件事有什么想法吗?

I'm using SQL Server 2008 and I have a table with three columns: Length, StartTime and EndTime. I want to make a CHECK constraint on this table which says that:

if Length == NULL then
  StartTime <> NULL and EndTime <> NULL
else
  StartTime == NULL and EndTime == NULL

I've begun to try things like this:

Length == NULL AND StartTime <> NULL AND EndTime <> NULL

Obviously this is not enough, but even this simple expression will not validate. I get the error:

"Error validating 'CK_Test_Length_Or_Time'. Do you want to edit the constraint?"

Any ideas on how to go about doing this?

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

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

发布评论

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

评论(2

轻许诺言 2024-09-05 03:21:50
CHECK ([Length] IS NULL AND [StartTime] IS NOT NULL AND [EndTime] IS NOT NULL
      OR [Length] IS NOT NULL AND [StartTime] IS NULL AND [EndTime] IS NULL))
CHECK ([Length] IS NULL AND [StartTime] IS NOT NULL AND [EndTime] IS NOT NULL
      OR [Length] IS NOT NULL AND [StartTime] IS NULL AND [EndTime] IS NULL))
谁人与我共长歌 2024-09-05 03:21:50

SQL Server 中没有 == 运算符。在检查 null 时,您必须使用“is”

请尝试以下操作:

((Length is null AND starttime is not null AND endtime is not null) OR
(Length is not null AND starttime is null AND endtime is null))

HTH

There is no == operator in SQL Server. While checking for null you have to use "is"

Please try this:

((Length is null AND starttime is not null AND endtime is not null) OR
(Length is not null AND starttime is null AND endtime is null))

HTH

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