2011-02-25 58 views
0

我需要运行一个语句,每次除了日期之外都会使用相同的信息填充数据库。循环使用日期的SQL插入语句

即时通讯正在尝试沿着这些线路,并没有任何运气。

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个月的运行该脚本。

+0

除了eventDate吗? – 2011-02-25 10:57:54

+1

你已经用'php'标记了这个 - 用php这样的程序语言来实现这个过程非常简单,而且对于像SQL这样的声明性语言而言(相对)非常困难。使用PHP! – simon 2011-02-25 10:59:44

+0

我希望活动日期是唯一改变的事情 – Simon 2011-02-25 11:00:24

回答

1

你可以尝试DATEADD(ms,i_int,GETDATE())您的日期。

0
INSERT INTO bs_events (eventDate, ...) 
VALUES ('2011-03-i_int 00:00:00', ...); 

该值为2011-03-i_int 00:00:00,是一个字符串,而不是时间戳。你希望你的SQL环境能够用i_int的值代替它的符号。它不会那样做。

相反,在INSERT语句外执行日期算术。

在PostgreSQL(PL/pgsql的),你可以这样做是为了增加一天类型为“戳”的变量。

this_date = this_date + interval '1 day'; 

而在INSERT语句,

INSERT INTO bs_events (eventDate, ...) 
VALUES (this_date, ...); 

检查您的文档,看语法可能是什么您的平台。

0
DECLARE 
i_int BINARY_INTEGER := 0; 
    BEGIN 
    WHILE i_int < 30 LOOP 
     INSERT INTO bs_events (
     eventDate, eventTime, title, 
     spaces, description, entryFee, 
     payment_required, max_qty, allow_multiple) 
     VALUES (
     DATEADD(day, i_int, '2011-03-01 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; 
/
4

考虑创建一个Calendar table

第一个表我和我一起工作的任何数据库创建,是一个数字表,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,财政年度,假期,假日名称,日期名称,季度,季度。对于非标准时期,这是黄金。

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; 

完成。你可能会看到你如何通过使用数字表来解决你的问题?

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

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';