SQL:触发器将列设置为空

发布于 2024-11-05 03:41:28 字数 415 浏览 0 评论 0原文

我创建了一个触发器来将列设置为 null,其中该列的值大于 GETDATE()

ALTER TRIGGER [dbo].[Null_Time_Trigger3]

  on [dbo].[Parking]
  FOR insert
  as
  update Parking
  set Column = NULL
  from Parking
  where Column >= CAST(GETDATE () AS TIME)

它在时间过去时出现,触发器不执行任何操作?时间仍在该列中,并且该列没有 NULL 值。

知道如何解决这个问题吗?

除了触发器之外还有什么办法可以做到这一点(将列设置为空)???

问候。

I created a trigger to set a column to null where the column has a value greater than GETDATE()

ALTER TRIGGER [dbo].[Null_Time_Trigger3]

  on [dbo].[Parking]
  FOR insert
  as
  update Parking
  set Column = NULL
  from Parking
  where Column >= CAST(GETDATE () AS TIME)

it appears when the time passes the trigger doesn't do anything? the time is still in the column and the column is not having the value of NULL.

Any idea how to resolve this ??

Is there anyway to do this (Setting the column to null) other than triggers???

Regards.

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

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

发布评论

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

评论(3

随波逐流 2024-11-12 03:41:28

insert 触发器仅在插入语句时触发。如果没有insert操作,您的触发器将不会运行。

由于您的触发器引用了 Parking 表本身而不是 inserted 表,因此它可能会在插入时起作用,但这完全不是触发器的用途。

SQL Server 中没有时间触发器会在时间过去时自动对列进行 NULL 化(正如您所期望的那样)。

您可以执行 CASE WHEN Column 改为在 SELECT 中使用 CAST(GETDATE () AS TIME) THEN Column END AS Column

insert triggers only fire on an insert statement. If there is no insert operation your trigger will not run.

As your trigger references the Parking table itself rather than the inserted table it might work on insert but this is totally not what triggers are for.

There are no temporal triggers in SQL Server that would automatically NULL-ify the columns when the time is passed (as it seems you might be expecting).

You can do CASE WHEN Column < CAST(GETDATE () AS TIME) THEN Column END AS Column in your SELECT instead.

客…行舟 2024-11-12 03:41:28

首先,我认为你应该使用 <在你的触发器定义中。无论如何,当您在数据库中插入新行时,会触发此触发器,并且对该行起作用,而不是在之前的行上!!!
您可以通过一个查询来完成此操作:

UPDATE Parking SET `Column`=NULL
WHERE `Column` < CAST(GETDATE () AS TIME)

已编辑:

CREATE TRIGGER [dbo].[Null_Time_Trigger4]
on [dbo].[Parking]
FOR INSERT,UPDATE
AS BEGIN
    UPDATE Parking SET `Column`=NULL
    WHERE `Column` < CAST(GETDATE () AS TIME)
END

First of all, I think you should use < in your trigger definition. Anyway this trigger is fired ONLY when you insert a new row in db and acts ONLY on that row, not on previous ones!!!
You could do that with one query:

UPDATE Parking SET `Column`=NULL
WHERE `Column` < CAST(GETDATE () AS TIME)

EDITED:

CREATE TRIGGER [dbo].[Null_Time_Trigger4]
on [dbo].[Parking]
FOR INSERT,UPDATE
AS BEGIN
    UPDATE Parking SET `Column`=NULL
    WHERE `Column` < CAST(GETDATE () AS TIME)
END
梦行七里 2024-11-12 03:41:28

您可以添加一个计算列,如下所示:

CREATE TABLE
(
...
Column  DATETIME,
CurrentColumn  AS CASE WHEN DATETIME < SYSDATETIME() THEN DATETIME END
....
)

但是,通常您应该将事件记录为新记录,而不是更新现有列。您当前的设计会破坏信息。

You could add a calculated column, something like this:

CREATE TABLE
(
...
Column  DATETIME,
CurrentColumn  AS CASE WHEN DATETIME < SYSDATETIME() THEN DATETIME END
....
)

However, in general you should log events as new records rather than updating an existing column. Your current design destroys information.

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