如何使用SQL语句在HANA中插入连续日期?

时间:2018-10-03 12:57:08

标签: sql hana hana-sql-script

我必须在2018年1月1日至2018年12月31日之间插入如下数据。

insert into "schema"."tablename" (FISCAL_DATE,FISCAL_DT,REGION_CD,FISCAL_REGION_CD,FISCAL_DATE1,NEW_REGION_CD) values ('2018-01-01', '2018-01-01','EMEA','EUR','01/01/2018', 'EURO'); 
insert into "schema"."tablename" (FISCAL_DATE,FISCAL_DT,REGION_CD,FISCAL_REGION_CD,FISCAL_DATE1,NEW_REGION_CD) values ('2018-01-01', '2018-01-01','EMEA','EUR','01/01/2018', 'EURO'); 
insert into "schema"."tablename" (FISCAL_DATE,FISCAL_DT,REGION_CD,FISCAL_REGION_CD,FISCAL_DATE1,NEW_REGION_CD) values ('2018-01-01', '2018-01-01','EMEA','EUR','01/01/2018', 'EURO'); 
insert into "schema"."tablename" (FISCAL_DATE,FISCAL_DT,REGION_CD,FISCAL_REGION_CD,FISCAL_DATE1,NEW_REGION_CD) values ('2018-01-01', '2018-01-01','EMEA','EUR','01/01/2018', 'EURO');
...
insert into "schema"."tablename" (FISCAL_DATE,FISCAL_DT,REGION_CD,FISCAL_REGION_CD,FISCAL_DATE1,NEW_REGION_CD) values ('2018-12-31', '2018-12-31','EMEA','EUR','12/31/2018', 'EURO');

是否有其他可选的SQL语句来实现此目的,或者需要手动逐个更新日期。

请分享您的意见/建议。

3 个答案:

答案 0 :(得分:0)

如果我对您的理解正确,则希望插入多个记录,记录的日期为2018-01-012018-12-31。 您可以使用WHILE语句来实现。

DO 
BEGIN

    DECLARE INPUT_DATE date := '2018-01-01';


    WHILE INPUT_DATE <= '2018-12-31'  DO

        INSERT INTO "schema"."tablename" (FISCAL_DATE,FISCAL_DT,REGION_CD,FISCAL_REGION_CD,FISCAL_DATE1,NEW_REGION_CD) values (:INPUT_DATE, :INPUT_DATE,'EMEA','EUR',:INPUT_DATE, 'EURO');

        INPUT_DATE = ADD_DAYS(:INPUT_DATE,1);

    END WHILE;

END

答案 1 :(得分:0)

您还可以使用SQL dates table on HANA database,使用SQLScript创建非常容易。

请检查以下示例(请根据您的模型替换表名和列名)

do
begin

declare date_start date := '01.01.2018';
declare date_end date := '31.12.2018';

insert into mySampleTransactionData (tdate)
SELECT generated_period_start
 FROM SERIES_GENERATE_DATE('INTERVAL 1 DAY', :date_start, add_days(:date_end,1));

end

答案 2 :(得分:0)

我已使用以下查询成功插入数据。

insert into "Schema"."tablename" (select to_nvarchar(gen_date, 'YYYY-MM-DD') as fiscal_date,
       to_nvarchar(gen_date, 'YYYY-MM-DD') as fiscal_dt,
       'AMR' as region_cd,
       'AMR' as fiscal_region_cd,
       to_nvarchar(gen_date, 'MM/DD/YYYY') as fiscal_date1,
       'AMR' as led_region_cd
from (
  select generated_period_start as gen_date from SERIES_GENERATE_DATE ('INTERVAL 1 DAY', '2018-01-01', '2018-12-31')
));

谢谢大家的宝贵建议!