怎这样的数据怎样插入oracle一张表中

时间:2022-06-11 20:52:38
数据库表
create tables A(
shidm varchar2(50),//市代码
shinm varchar2(50), //市名称
zhendm shidm varchar2(50),
zhennm shidm varchar2(50)
)

数据
1234  虚拟市1    null  (A,B,C,D,E,F,G,H,I)//这是excel表单中的一列数据;


插入数据库规则
  插入数据库表A    shidm》》1234,shinm》》虚拟市1, zhendm》》这个根据后面有多少列来插入,数据的编码规则是,存在一列1234+01(效果123401),依次递增。 zhennm》》每次获取一个,加上前面三个字段,形成一行(
下面是导入到数据库的两行
1234  虚拟市1   123401  A;
1234  虚拟市1    123402  B)。

其他的依次生成

请问我应该怎样将数据有excel直接插入到oracle。

4 个解决方案

#1


--创建一个中间表,把execle数据先导进中间表,
create table temp(
   shidm varchar2(50),
   shinm varchar2(50),
   zhennm varchar2(500)
)

insert into temp value ('1234','虚拟市1','A,B,C,D,E,F,G,H,I');
insert into temp value ('5678','虚拟市2','J,K,L,M');
commit;

select * from temp;

select t1.shidm,
       t1.shinm,
       t1.shidm || lpad(r, 2, '0') zhendm,
       substr(t1.zhennm, 2 * r - 1, 1) zhennm
  from temp t1,
       (select rownum r
          from dual
        connect by rownum <=
                   (select max(length(replace(zhennm, ','))) from temp)) t2
 where length(replace(t1.zhennm, ',')) >= r
 order by t1.shidm, t1.shinm, t2.r
 
 SHIDM                                              SHINM                                              ZHENDM                                                 ZHENNM
-------------------------------------------------- -------------------------------------------------- ------------------------------------------------------ ------
1234                                               虚拟市1                                            123401                                                 A
1234                                               虚拟市1                                            123402                                                 B
1234                                               虚拟市1                                            123403                                                 C
1234                                               虚拟市1                                            123404                                                 D
1234                                               虚拟市1                                            123405                                                 E
1234                                               虚拟市1                                            123406                                                 F
1234                                               虚拟市1                                            123407                                                 G
1234                                               虚拟市1                                            123408                                                 H
1234                                               虚拟市1                                            123409                                                 I
5678                                               虚拟市2                                            567801                                                 J
5678                                               虚拟市2                                            567802                                                 K
5678                                               虚拟市2                                            567803                                                 L
5678                                               虚拟市2                                            567804                                                 M
 
13 rows selected

#2


你好!谢谢你的回答。可能第一次发帖吧。我的需求不清吧。
我想要的结果是 
  shidm    shinm       zhendm        zhennm     
  1234     虚拟市1     123401          A
  1234     虚拟市1     123402          B
  1234     虚拟市1     123403          C
镇名称,有多少个,就在是代码(1234)后面自动添加一位。
可以的话请帮忙想想

   

#3


引用 2 楼 fish_dream 的回复:
你好!谢谢你的回答。可能第一次发帖吧。我的需求不清吧。
我想要的结果是 
  shidm    shinm       zhendm        zhennm     
  1234     虚拟市1     123401          A
  1234     虚拟市1     123402          B
  1234     虚拟市1     ……

啥意思,我上面的不是已经加1了么

#4


可以是可以了。可是我的是9i的 ,connect by不能有子查询。谢谢结贴了

#1


--创建一个中间表,把execle数据先导进中间表,
create table temp(
   shidm varchar2(50),
   shinm varchar2(50),
   zhennm varchar2(500)
)

insert into temp value ('1234','虚拟市1','A,B,C,D,E,F,G,H,I');
insert into temp value ('5678','虚拟市2','J,K,L,M');
commit;

select * from temp;

select t1.shidm,
       t1.shinm,
       t1.shidm || lpad(r, 2, '0') zhendm,
       substr(t1.zhennm, 2 * r - 1, 1) zhennm
  from temp t1,
       (select rownum r
          from dual
        connect by rownum <=
                   (select max(length(replace(zhennm, ','))) from temp)) t2
 where length(replace(t1.zhennm, ',')) >= r
 order by t1.shidm, t1.shinm, t2.r
 
 SHIDM                                              SHINM                                              ZHENDM                                                 ZHENNM
-------------------------------------------------- -------------------------------------------------- ------------------------------------------------------ ------
1234                                               虚拟市1                                            123401                                                 A
1234                                               虚拟市1                                            123402                                                 B
1234                                               虚拟市1                                            123403                                                 C
1234                                               虚拟市1                                            123404                                                 D
1234                                               虚拟市1                                            123405                                                 E
1234                                               虚拟市1                                            123406                                                 F
1234                                               虚拟市1                                            123407                                                 G
1234                                               虚拟市1                                            123408                                                 H
1234                                               虚拟市1                                            123409                                                 I
5678                                               虚拟市2                                            567801                                                 J
5678                                               虚拟市2                                            567802                                                 K
5678                                               虚拟市2                                            567803                                                 L
5678                                               虚拟市2                                            567804                                                 M
 
13 rows selected

#2


你好!谢谢你的回答。可能第一次发帖吧。我的需求不清吧。
我想要的结果是 
  shidm    shinm       zhendm        zhennm     
  1234     虚拟市1     123401          A
  1234     虚拟市1     123402          B
  1234     虚拟市1     123403          C
镇名称,有多少个,就在是代码(1234)后面自动添加一位。
可以的话请帮忙想想

   

#3


引用 2 楼 fish_dream 的回复:
你好!谢谢你的回答。可能第一次发帖吧。我的需求不清吧。
我想要的结果是 
  shidm    shinm       zhendm        zhennm     
  1234     虚拟市1     123401          A
  1234     虚拟市1     123402          B
  1234     虚拟市1     ……

啥意思,我上面的不是已经加1了么

#4


可以是可以了。可是我的是9i的 ,connect by不能有子查询。谢谢结贴了