以前写过的一个递归SQL,返回一天中的所有时间(精确到秒,即00:00:00-23:59:59),在此分享下!
create table test_table(id int,d_second time) IN DATA_TS;
create table temp_table(id int,d_second time) IN DATA_TS;
insert into temp_table(id,d_second) values(1,'00:00:00');
insert into test_table with temp_table(id,d_second) as(select 1,time('00:00:00') from sysibm.sysdummy1 union all select id+1,d_second+1 second from temp_table where id < 86400) select * from temp_table;
commit;
I:>db2 -td; -vf recursion.sql
create table test_table(id int,d_second time) IN DATA_TS
DB20000I SQL 命令成功完成。
create table temp_table(id int,d_second time) IN DATA_TS
DB20000I SQL 命令成功完成。
insert into temp_table(id,d_second) values(1,'00:00:00')
DB20000I SQL 命令成功完成。
insert into test_table with temp_table(id,d_second) as(select 1,time('00:00:00') from sysi
bm.sysdummy1 union all select id+1,d_second+1 second from temp_table where id < 86400) sel
ect * from temp_table
DB20000I SQL 命令成功完成。
commit
DB20000I SQL 命令成功完成。
I:>db2 select * from test_table fetch first 10 rows only
ID D_SECOND
----------- --------
1 00:00:00
2 00:00:01
3 00:00:02
4 00:00:03
5 00:00:04
6 00:00:05
7 00:00:06
8 00:00:07
9 00:00:08
10 00:00:09
10 条记录已选择。
添加新评论0 条评论