create table app
(
start_date date,
end_date date,
start_time time(7),
end_time time(7)
);
输入:5/2/2016 08:00:00 to 12:30:00
我需要通过像
这样的存储过程将行插入到上面的表中08:00至12:30之间每隔一小时
insert into app values ('5/2/2016', '5/2/2016', '08:00:00', '09:00:00')
insert into app values ('5/2/2016', '5/2/2016', '09:00:00', '10:00:00')
insert into app values ('5/2/2016', '5/2/2016', '10:00:00', '11:00:00')
insert into app values ('5/2/2016', '5/2/2016', '11:00:00', '12:00:00')
我的代码是
@j time(7);
@time time(7);
select @time = time from someTable;
SET @j = @startTime;
while (@j <= @endTime)
begin
INSERT INTO appointmentt(start_date, end_date, start_time, end_time)
VALUES (@startDate, @startDate, @j, @j + @time, @tutorId);
SET @j = @j + @time;
end;
收到如下错误:
操作数数据类型时间对于添加运算符
无效
答案 0 :(得分:0)
在这种情况下,要为日期时间添加时间,您可以先将其投射为this example:
DECLARE @date DATETIME, @time time
SET @date='2010-10-01'
SET @time='15:00:00'
SET @date=@date+CAST(@time AS DATETIME)
SELECT @date AS DATETIME
要将整数添加到日期时间,您需要使用dateadd() function。
例如:
DATEADD(second,1,@j)
答案 1 :(得分:0)
更改
SET @j=@j+@time;
要
select @j = DATEADD(MINUTE, DATEPART(MINUTE, @time),
DATEADD(HOUR, DATEPART(HOUR, @time), @j))