I have a stored procedure which inserts two records into a table, the difference between the records is that the time column of the second record is @MinToAdd
after the first:
CREATE PROCEDURE CreateEntry
/*Other columns*/
@StartTime time(2),
@EndTime time(2),
@MinutesToAdd smallint
AS
BEGIN
SET NOCOUNT ON;
SET @MinutesToAdd = @MinutesToAdd % 1440; --Prevent overflow if needed?
IF (@MinutesToAdd > 0)
BEGIN
INSERT INTO ClientNotification (/*Other columns*/ startTime, endTime)
OUTPUT inserted.id
VALUES
(/*Other columns*/ @StartTime, @EndTime),
(/*Other columns*/ @StartTime + @MinutesToAdd, @EndTime + @MinutesToAdd);
END
ELSE
BEGIN
/*Whatever ELSE does.*/
END
END
What is the correct way to add @MinutesToAdd
minutes to @StartTime
and @EndTime
?
Please note I am using the time
data type.
Update:
A correct answer should contain the following information:
- How to add minutes to to a
time
data type. - That the proposed solution does not result in a loss of precision.
- Issues or concerns to be aware of in the event that the minutes would be to too large to fit in a
time
variable, or risk of rolling thetime
variable over. If there are no issues then please state so.