建一个存储过程
在里面写个循环就行了
datetime := 20120407--起点值
while datetime<20150428 loop --截止时间
sqlstr='insert into calendar(c_date) values('||datetime||')';
execute immediate sqlstr;
datetime=to_char(to_date(datetime,'yyyymmdd')+1,'yyyymmdd');--取下一天
end loop;
Create proc uspTestDateInsert
@startDate datetime
,@endDate datetime
as
begin
while(@startDate<=@enddate)
begin
insert into dbo.calendar (c_date) values
(Convert(bigint,CONVERT(char(8),@startDate,112)))
set @startDate=DATEADD(day,1,@startdate)
end
end
--exec uspTestDateInsert '2009-02-12','2009-03-01'
select* from calendar
这个可能需要区分数据库种类
DB2实现:
BEGIN
DECLARE V_DATE DATE;
V_DATE = DATE('2011-04-27');
WHILE(V_DATE >= DATE('2011-04-27') AND V_DATE < DATE('2015-04-27'))
DO
INSERT INTO calendar (c_date) VALUES(DECIMAL(V_DATE));
SET V_DATE = V_DATE + 1 DAY;
END WHILE;
END
Create proc uspTestDateInsert
@startDate datetime
,@endDate datetime
as
begin
while(@startDate<=@enddate)
begin
insert into dbo.calendar (c_date) values
(Convert(bigint,CONVERT(char(8),@startDate,112)))
set @startDate=DATEADD(day,1,@startdate)
end
end
--exec uspTestDateInsert '2009-02-12','2009-03-01'
select* from calendar
输入变量为 月份 4位
变量1 月初
变量2 月末
将 月初,月末 变成数字类型
插入 当 值大于 月初,小于月末的时候插入