问题描述
我需要运行一个语句,在每次EXCEPT日期之后,用相同的信息填充数据库。
im目前正在尝试这些行,没有任何运气。
DECLARE
i_int BINARY_INTEGER:= 1;
BEGIN
在i_int< = 30 LOOP
INSERT INTO bs_events(EVENTDATE,EVENTTIME,标题,空间,描述,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中实现循环。此外,它可以用于生成我在任何数据库上创建的第二个表:日历表。
日历表每个日期有一行,开始在您的数据库中的第一个记录的业务事件(加一年左右)。为所有相关商家查询保留足够的未来日期(再加上几年是安全的)。
您的具体问题可以通过上述表格解决,但日历
<$> 我将在下面的MySQL中给你一个简单的但是工作的例子:
创建表数字(n int);
插入数字值(0),(1),(2),(3),(4),(5),(6),(7),(8)
创建表号(
n int not null
,主键(n)
);
数字表只是一个工作表,一旦创建了真实数字表。数字表只有一列,它是主键。接下来,从1开始生成1百万个连续的整数。(这听起来很慢,但实际上在我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
从数字d1
,数字d2
,数字d3
,数字d4
,数字d5
,数字d6;
/ *删除工作表。 * /
drop table digits;
接下来,我将创建一个日历表。显然,它是相当无用的,因为它没有任何有用的列。有用的列的示例是年,月名,周数,isFirstMonthDay,isLastMonthDay,财政年度,isHoliday,假日名称,日期,季度,季度。对于非标准期间,这是黄金。
创建表日历(
基准日期非空
,主键(数据)
);
好的,现在我们可以使用数字表来作为一个行生成器我们的日历表。假设我想生成2000-01-01和2019-12-31之间的所有日期。这将是7305天。很容易,只需从数字表中选择许多行,并将 N
添加到日期。
将
插入日历(基准)
select date_add '1999-12-31',interval n day)
from numbers
其中n
完成。你可能看到你可以通过使用数字表格来解决你的问题。
最后,这里是一个如何使用日历来解决你的问题的例子表。如果您为年和月添加列,当然会更容易。在这里,我将您的问题解释为:为每个月的每一天生成一个完全相同的行,用于其余时间。
插入
到bs_events(
EVENTDATE,EVENTTIME,标题
,空格,描述,entryFee
,payment_required,max_qty,allow_multiple
)
选择数据,'10:00','地毯清洁器3000'
,'4','这是rad','25 .99'
,'1','1' ,'2'
来自日历
其中datum> = date'2011-03-01'
和datum
I need to run a statement that will populate a data base with the same information every time EXCEPT for the date.
im currently trying something along these lines and aren't having any luck.
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;
/
I can't seem to get it to work and im stuck in a code hole and cannot think straight.
I basically want the date to up by one from 01-30 and then i will manually change the month and run the script again for all 12 months of the year.
Consider creating a Calendar table.
The first table I create in any database I work with, is a table of numbers, 1 to 1000000. Such a table is useful for lots of purposes, like implementing loops in SQL. Also, it can be used to generate the second table I create on any database I work with: The Calendar table.
The calendar table has one row for each date, starting at the first recorded business event in your database (plus a year or so). Keep enough future dates for all relevant business queries (plus a few years to be safe).
Your specific problem can be solved with either of the above tables, but the calendar table approach would be much easier.
I'll give you a trivial but working example in MySQL below:
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)
);
The digits table is just a working table, it will be dropped once the real numbers table has been created. The numbers table has just one column, it's the primary key. Next, generate 1 million consecutive integers starting at 1. (It sounds slow, but it actually completes in under 10 sec on my 2 year old laptop).
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;
Next, I'll create a calendar table. Obviously it is quite useless at the moment as it doesn't have any useful columns. Examples of useful columns are year, monthname, week number, isFirstMonthDay, isLastMonthDay, Financial Year, isHoliday, Holidayname, dayname, quarter, tertial. For non-standard periods, this is golden.
create table calendar(
datum date not null
,primary key(datum)
);
Ok, so now we can use the numbers table for example, to act as a row generator to build our calendar table. Let's say I want to generate all dates between 2000-01-01 and 2019-12-31. That would be 7305 days. Easy, just select that many rows from the numbers table, and add the int column N
to a date. This will create a list of increasing dates.
insert
into calendar(datum)
select date_add('1999-12-31', interval n day)
from numbers
where n <=7305;
Done. You could probably see how you could have solved your problem just by using the numbers table?
Finally, here is an example of how to solve your specific problem by using the calendar table. It would of course be even easier if you added columns for Year and Month. Here I'm interpreting your question to mean "Generate one identical row for each day of each month, for the rest of the year".
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';
这篇关于循环具有日期的SQL插入语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!