如题,比如我现在有一个表A,
A01(流水号) A02(编号) A03(数量)
--------------------------------------------------------------
1 a001 3
。。。
想要得到如下结果:原记录不删除,然后生成三条记录,编号不变,数量全部是1
A01(流水号) A02(编号) A03(数量) A04(条码)
-----------------------------------------------------------------------------------------------------------
1 a001 1
2 a001 1
3 a001 1
解决方法:
SELECT t1.a01,t1.a02,t2.rn
FROM tab t1,(SELECT ROWNUM rn FROM dual CONNECT BY ROWNUM<10000) t2
WHERE t1.a03>=t2.rn
把取到的数量值强制转换为1就OK 了
第二种方法:
创建数据表,插入数据:
create table test
(品名 varchar2(20),
数量 int);
insert into test values ('苹果',3);
insert into test values ('栗子',2);
commit;
创建临时表,用于接收数据:
create table test_tmp as select * from test where 1<>1;
执行存储过程:
declare
v_name varchar2(20);
v_count int;
v_s int;
cursor c_row is
select 品名, 数量 from test;
begin
open c_row;
loop
fetch c_row
into v_name, v_count;
exit when c_row%notfound;
v_s := 1;
while v_s <= v_count loop
insert into test_tmp
select v_name, 1 from dual;
v_s := v_s + 1;
end loop;
commit;
end loop;
close c_row;
end;
检查结果,查询test_tmp表:
selet * from test_tmp