SQL:触发器将列设置为空
我创建了一个触发器来将列设置为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
insert
触发器仅在插入语句时触发。如果没有insert
操作,您的触发器将不会运行。由于您的触发器引用了 Parking 表本身而不是 inserted 表,因此它可能会在插入时起作用,但这完全不是触发器的用途。
SQL Server 中没有时间触发器会在时间过去时自动对列进行
NULL
化(正如您所期望的那样)。您可以执行
CASE WHEN Column
改为在
。SELECT
中使用 CAST(GETDATE () AS TIME) THEN Column END AS Columninsert
triggers only fire on an insert statement. If there is noinsert
operation your trigger will not run.As your trigger references the
Parking
table itself rather than theinserted
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 yourSELECT
instead.首先,我认为你应该使用 <在你的触发器定义中。无论如何,当您在数据库中插入新行时,仅会触发此触发器,并且仅对该行起作用,而不是在之前的行上!!!
您可以通过一个查询来完成此操作:
已编辑:
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:
EDITED:
您可以添加一个计算列,如下所示:
但是,通常您应该将事件记录为新记录,而不是更新现有列。您当前的设计会破坏信息。
You could add a calculated column, something like this:
However, in general you should log events as new records rather than updating an existing column. Your current design destroys information.