日期:2014-05-17  浏览次数:20481 次

如何从另一个表批量插入12条时辰数据?
A表已经有DT字段,从1900年到2050年所有日期.
现想建B表,在每日的基础上,插入12个时辰的记录.
a:
dt(日期)
1900-1-1
...
2050-12-31

B:
dt(日期)     时辰数字    时辰
1900-1-1        1       子
1900-1-1        2       丑
....
1900-1-2        1       子
....
2050-12-31      1       子


这条批量插入的SQL如何写好点?

------解决方案--------------------

create table A表(dt varchar(15))

create table B表
(dt varchar(16), 时辰数字 int, 时辰 varchar(5))

insert into A表(dt)
 select '1900-01-01' union all
 select '1900-01-02' union all
 select '1900-01-03' union all
 select '1900-01-04' union all
 select '1900-01-05'


insert into B表
select a.dt,s.时辰数字,s.时辰
 from A表 a
 cross join
 (select 1 '时辰数字', '子' '时辰' union all
  select 2 '时辰数字', '丑' '时辰' union all
  select 3 '时辰数字', '寅' '时辰' union all
  select 4 '时辰数字', '卯' '时辰' union all
  select 5 '时辰数字', '辰' '时辰' union all
  select 6 '时辰数字', '巳' '时辰' union all
  select 7 '时辰数字', '午' '时辰' union all
  select 8 '时辰数字', '未' '时辰' union all
  select 9 '时辰数字', '申' '时辰' union all
  select 10 '时辰数字', '酉' '时辰' union all
  select 11 '时辰数字', '戌' '时辰' union all
  select 12 '时辰数字', '亥' '时辰') s


select dt,时辰数字,时辰 from B表
/*
dt               时辰数字        时辰