从startdate和enddate SQL计算持续时间的过程

问题描述 投票:0回答:1

我正在为学校工作创建一个数据库,我遇到了一件我想做的事情。 我正在研究一个真实案例,所以我在关系中有EndDate,StartDate和Duration属性,我必须为Duration创建一个触发器。但是,首先我需要计算它。它是StartDate和EndDate的派生属性,但我不知道如何做到这一点。我一直在检查程序和函数来执行它(我认为它必须是一个过程,以便SQL在插入元组时计算它)。我一直在尝试使用DATEDIFF,但我真的不知道如何使用现有的属性。谁能帮我?

sql stored-procedures
1个回答
0
投票

我认为您应该更深入地了解DecoderReloaded对您帖子的评论。

您可以在查询开始时将一个INSTEAD OF trigger保留在列中,并在结束时使用AFTER触发器在另一列中保留。

例:

CREATE TRIGGER TriggerInsteadOf  
ON YourTable  
INSTEAD OF INSERT, UPDATE  
 --You should repeat your queries here (as always needed with INSTEAD OF triggers) and set the datetime column value for the start time.
GO  

CREATE TRIGGER TriggerAfter  
ON YourTable  
AFTER INSERT  
 --Don't need to repeat your queries here (as always happen with AFTER triggers) and set the datetime column value for the end time, or a DATEDIFF() to obtain the timespan.
GO  

注意多次更新,您也可以考虑DELETE命令,具体取决于您想要实现的目标。

这不是最终的想法,但如果实施它将会很好。祝好运。

© www.soinside.com 2019 - 2024. All rights reserved.