我需要运行一个语句,每次超出日期时,将使用相同的信息填充数据库.

我目前正在尝试这些方面的东西,没有任何运气.

DECLARE
i_int BINARY_INTEGER := 1;
BEGIN
WHILE i_int <= 30 LOOP
INSERT INTO bs_events (eventDate,eventTime,title,spaces,description,entryFee,payment_required,max_qty,allow_multiple) VALUES ('2011-03-i_int 00:00:00', '10:00','Carpet Cleaner 3000','4','This is rad','25.99','1','1','2');
i_int := i_int + 1;
END LOOP;
END;
/

我似乎无法让它工作,我陷入了代码洞,不能直接思考.

我基本上希望日期从01到30增加一个然后我将手动更改月份并在一年中的所有12个月再次运行脚本.

解决方法:

我在我使用的任何数据库中创建的第一个表是一个数字表,1到1000000.这样的表有很多用途,比如在SQL中实现循环.此外,它可用于生成我在我使用的任何数据库上创建的第二个表:Calendar表.

日历表对于每个日期都有一行,从数据库中的第一个记录的业务事件开始(加上一年左右).为所有相关业务查询保留足够的未来日期(加上几年安全).

您可以使用上述任一表解决您的具体问题,但日历表方法会更容易.

我将在MySQL下面给你一个简单但有效的例子:

create table digits(n int);
insert into digits values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
create table numbers(
n int not null
,primary key(n)
);

数字表只是一个工作表,一旦创建了实数表,它就会被删除.数字表只有一列,它是主键.接下来,从1开始生成100万个连续的整数.(听起来很慢,但它实际上在我2岁的笔记本电脑上在10秒内完成).

insert
into numbers(n)
select 1
+ (d1.n * 1)
+ (d2.n * 10)
+ (d3.n * 100)
+ (d4.n * 1000)
+ (d5.n * 10000)
+ (d6.n * 100000) as n
from digits d1
,digits d2
,digits d3
,digits d4
,digits d5
,digits d6;
/* Drop the working table. */
drop table digits;

接下来,我将创建一个日历表.显然它目前没用,因为它没有任何有用的列.有用列的示例包括年,月名,周数,isFirstMonthDay,isLastMonthDay,财务年度,isHoliday,Holidayname,dayname,quarter,tertial.对于非标准时期,这是金色的.

create table calendar(
datum date not null
,primary key(datum)
);

好的,现在我们可以使用数字表作为行生成器来构建我们的日历表.假设我要生成2000-01-01和2019-12-31之间的所有日期.那将是7305天.简单,只需从数字表中选择多行,并将int列N添加到日期.这将创建一个日期增加的列表.

insert
into calendar(datum)
select date_add('1999-12-31', interval n day)
from numbers
where n <=7305;

完成.您可能只是通过使用数字表来了解如何解决问题?

最后,这是一个如何使用日历表解决您的特定问题的示例.如果添加Year和Month的列,当然会更容易.在这里,我将您的问题解释为“在每个月的每一天为一年中的其余时间生成一个相同的行”.

insert
into bs_events(
eventDate, eventTime, title
,spaces, description, entryFee
,payment_required, max_qty, allow_multiple
)
select datum, '10:00', 'Carpet Cleaner 3000'
,'4', 'This is rad', '25.99'
,'1', '1', '2'
from calendar
where datum >= date '2011-03-01'
and datum <= date '2011-12-31';