SQL - 需要填充记录的序列

时间:2015-03-08 21:09:31

标签: sql oracle sequence records

我有下表(PART_MAP)。

PART    | OFFER     |
--------+------------
part1   | offer01   |
part1   | offer02   |
part1   | offer03   |
part1   | offer04   |
part1   | offer05   |
part2   | offer06   |
part2   | offer07   |
part2   | offer08   |
part2   | offer09   |
part2   | offer10   |
part3   | offer11   |
part3   | offer12   |
part3   | offer13   |
part3   | offer14   |
part3   | offer15   |
part3   | offer16   |
...
part100 | offer1007 |
part100 | offer1008 |
part100 | offer1009 |
part100 | offer1110 |

使用该表,我需要在另一个表(PART_MAP_SET)中创建插入

SERIALNUM  | GRP         | PART    | OFFER     |
------------------------------------------------
0          | part1_grp   | part1   | offer01   | 
1          | part1_grp   | part1   | offer02   | 
2          | part1_grp   | part1   | offer03   | 
3          | part1_grp   | part1   | offer04   | 
4          | part1_grp   | part1   | offer05   | 
0          | part2_grp   | part2   | offer06   | 
1          | part2_grp   | part2   | offer07   | 
2          | part2_grp   | part2   | offer08   | 
3          | part2_grp   | part2   | offer09   | 
4          | part2_grp   | part2   | offer10   | 
0          | part3_grp   | part3   | offer11   | 
1          | part3_grp   | part3   | offer12   | 
2          | part3_grp   | part3   | offer13   | 
3          | part3_grp   | part3   | offer14   | 
4          | part3_grp   | part3   | offer15   | 
5          | part3_grp   | part3   | offer16   | 
...
0          | part100_grp | part100 | offer1007 | 
1          | part100_grp | part100 | offer1008 | 
2          | part100_grp | part100 | offer1009 | 
3          | part100_grp | part100 | offer1110 | 

我正在使用unix shell脚本在oracle上调用sqlplus。 为了最小化我需要连接到数据库的次数,我试图将插入语句创建为来自一个查询的假脱机输出,然后将该假脱机输出作为脚本运行。

我正在假设以下查询的输出

select 'insert into part_map_set 
(serialnum, grp, part, offer) 
values
(' || XXXX
   ||', '''
   || part ||'_grp'', '''
   || part 
   || ''', '''
   || offer
   || ''');' 
from (part_map); 

我在 XXXX 部分遇到问题。如果我能够填充它,我的假脱机输出将是

insert into part_map_set (serialnum, grp, part, offer) values (0, 'part1_grp', 'part1', 'offer01');
insert into part_map_set (serialnum, grp, part, offer) values (1, 'part1_grp', 'part1', 'offer02');
...
insert into part_map_set (serialnum, grp, part, offer) values (0, 'part2_grp', 'part2', 'offer06');
...

任何建议我如何从0到N为每个伙伴生成该序列。

我的最后一招是创建100个临时序列(每个伙伴1个),然后获取序列。但我还没去那里:)

以下内容可帮助您进行设置:

Create table part_map (part varchar2(20), offer varchar2(20));
Create table part_map_set (serialnum number(4), grp varchar2(20), part varchar2(20), offer varchar2(20));
insert into part_map (part, offer) values ('part1', 'offer01');
insert into part_map (part, offer) values ('part1', 'offer02');
insert into part_map (part, offer) values ('part1', 'offer03');
insert into part_map (part, offer) values ('part1', 'offer04');
insert into part_map (part, offer) values ('part1', 'offer05');
insert into part_map (part, offer) values ('part2', 'offer06');
insert into part_map (part, offer) values ('part2', 'offer07');
insert into part_map (part, offer) values ('part2', 'offer08');
insert into part_map (part, offer) values ('part2', 'offer09');
insert into part_map (part, offer) values ('part2', 'offer10');
insert into part_map (part, offer) values ('part3', 'offer11');
insert into part_map (part, offer) values ('part3', 'offer12');
insert into part_map (part, offer) values ('part3', 'offer13');
insert into part_map (part, offer) values ('part3', 'offer14');
insert into part_map (part, offer) values ('part3', 'offer15');
insert into part_map (part, offer) values ('part3', 'offer16');
insert into part_map (part, offer) values ('part100', 'offer1007');
insert into part_map (part, offer) values ('part100', 'offer1008');
insert into part_map (part, offer) values ('part100', 'offer1009');
insert into part_map (part, offer) values ('part100', 'offer1110');

1 个答案:

答案 0 :(得分:2)

对于XXXX部分,我认为您需要row_number()

select 'insert into part_map_set 
(serialnum, grp, part, offer) 
values
(' || row_number() over (partition by part order by part)
   ||', '''
   || part ||'_grp'', '''
   || part 
   || ''', '''
   || offer
   || ''');' 
from part_map; 

您可能有其他原因以这种方式生成代码,但您可以将整个语句作为单个insert运行:

insert into part_map_set (serialnum, grp, part, offer) 
    select row_number() over (partition by part order by part),
           part ||'_grp', part,  offer
    from part_map;

这应该比数以千计的单独insert语句快得多。