SQL:触发器未执行
我创建了一个触发器,当插入的时间小于 GETDATE() 时,将数据类型 time 的列的值设置为 null
ALTER trigger [dbo].[NULL_TIME_TRIGGER]
on [dbo].[Products]
after insert
as
update Products
set ParkingStartTime = NULL
from Products
join inserted i
on i.ParkingStartTime = Products.ParkingStartTime
where i.ParkingStartTime < CAST(GETDATE() AS TIME);
问题是,当 I select * From table
时,记录仍然是有时间而不是NULL
。
有什么想法吗?
问候。
I have created a trigger to set the value of a column of the data type time to null when the time inserted is Less than GETDATE()
ALTER trigger [dbo].[NULL_TIME_TRIGGER]
on [dbo].[Products]
after insert
as
update Products
set ParkingStartTime = NULL
from Products
join inserted i
on i.ParkingStartTime = Products.ParkingStartTime
where i.ParkingStartTime < CAST(GETDATE() AS TIME);
The problem is that when I select * From table
the record is still having the time rather than being NULL
.
Any Ideas ?
Regards.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为最后,您需要一个 sql 作业,当时间条件小于 getDate() 时,它只是查看并将时间设置为 null
I think in end, you need a sql job, that just looks through and sets the time to null when there time criteria is less than getDate()
rsenna 有一种选择,并且在很多方面都更胜一筹。以下是我将如何播放您的 AFTER INSERT 版本(假设 int 为 PK):
rsenna has one option, and it is superior in many respects. Here is how I would play your AFTER INSERT version (asssume int as PK):
我真诚地不知道你的触发器出了什么问题,据我所知它应该可以工作。
但这绝对不是正确的选择。您似乎正在尝试验证一些插入的值。如果是这种情况,您应该使用
INSTEAD OF INSERT
触发器,而不是像您当前所做的那样使用AFTER INSERT
。所以它会是这样的:
这样 SQL Server 将拦截对
Products
表的所有插入,并且只会执行触发器内的插入,从而修复ParkingStartTime
值。因此触发器不需要执行另一个更新来使它们正确。希望有帮助。
I sincerely don't know whats wrong with your trigger, it should be working AFAIK.
But this is definitely not the way to go. You seem to be trying to validate some inserted values. If that's the case, you should be using an
INSTEAD OF INSERT
trigger, not anAFTER INSERT
, like you're currently doing.So it would be like this:
That way SQL Server will intercept all inserts into the
Products
table, and will only execute the insert inside the trigger, which fixes theParkingStartTime
value. So the trigger doesn't need to execute another update to make them right.Hope it helps.
我有一个想法,是的。插入行时,时间值大于当前时间,因此保持不变。当您检查表的内容时,某些值在检查时已经超过了当前时间。
I have an idea, yes. When the row is inserted, the time value is greater than the current time so it's left untouched. When you are checking the contents of the table, some values are already past the current time at the moment of checking.