用字符串表示日期的比较和累加
create procedure CreateEvent
@eventRepeat bit = 0,
@eventStartDate varchar(20),@eventEndDate varchar(20),
@eventTitle varchar(30),
@userName varchar(30)
as
@eventTempDate varchar(20)
begin
set @eventTempDate = @eventStartDate
if @eventRepeat = 1
begin
while @EventTempDate < @EventEndDate
begin
insert into T_Event(EventStartDate,EventEndDate,EventTitle,UserName)
values(@eventTempDate,@eventEndDate,@eventTitle,@userName)
set @EventTempDate ++ ?
end
end
else
begin
insert into T_Event(EventStartDate,EventEndDate,EventTitle,UserName)
values(@eventStartDate,@eventEndDate,@eventTitle,@userName)
end
end
go
@eventStartDate varchar(20),@eventEndDate varchar(20) 的值都是形为 '2007-01-11 '表示日期的字符串,
按照时间的先后比较2个的大小,如果@eventStartDate < @eventEndDate 则把@eventTempDate作为参数插入到记录中,插入猴并增加1天,
直到@eventTempDate等于 @eventEndDate为止,这里要考虑到日期的有效性,啥时候在天上累加,啥时候在月上累加,特别是润年,润月.
------解决方案--------------------select dateadd(day,1, '2007-10-11 ')
-----------------------
2007-10-12 00:00:00.000
(1 row(s) affected)
------解决方案--------------------转换为时间型后加一在转换回来.以@eventTempDate为例.
set @eventTempDate = cast(dateadd(day,1,cast(@eventTempDate as datetime)) as varchar)
------解决方案--------------------create procedure CreateEvent
@eventRepeat bit = 0,
@eventStartDate varchar(20),@eventEndDate varchar(20),
@eventTitle varchar(30),
@userName varchar(30)
as
if isdate(@eventStartDate)=0 or isdate(@eventEndDate)=0
begin
raiserror( '日期 @eventStartDate=%s 或者 @eventEndDate=%s 格式不正确 ',16,1,@eventStartDate,@eventEndDate)
return (-1)
end
--@aebventTempDate varchar(20)
--begin
set @eventStartDate = @eventStartDate
if @eventRepeat = 1
begin
while @eventStartDate < @EventEndDate
begin
insert into T_Event(EventStartDate,EventEndDate,EventTitle,UserName)
values(@eventStartDate,@eventEndDate,@eventTitle,@userName)
set @eventStartDate = convert(varchar(10), dateadd(day,1,@eventStartDate), 120)
end
end
else
begin
insert into T_Event(EventStartDate,EventEndDate,EventTitle,UserName)
values(@eventStartDate,@eventEndDate,@eventTitle,@userName)
end
--end
go