使用年份+月份的方法拼接出如下数列:199001,199002,199003,...,199012,199101,...,299912 (6位数,前面是年份,后面是月份).我想先键三个表a,b,c 表a中字段aa存1990-2999,b中字段bb存01-12,c中保存'aa'||'bb'的结果,然后用存储过程游标取数据拼接来实现,中间得用两个循环嵌套.(三个表都已建好)求高手帮忙实现一下,或者有更好的实现方法?

解决方案 »

  1.   


    create table a(y number(4));
    create or replace procedure pro_insert
    as
      v_num number;
    begin
         for v_num in 1990..2999 loop
         insert into a(y) values(v_num);
         end loop;
         commit;
         exception
         when others then
              rollback;
    end pro_insert;
    --
    create table b(m varchar2(2))
    select * from b;
    M
    --
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    --
    返回一个笛卡尔积
    SQL> select count(*) from a;
      COUNT(*)
    ----------
          1010
    SQL> select count(*) from b;
      COUNT(*)
    ----------
            12
    SQL> select 1010*12 from dual;
       1010*12
    ----------
         12120
    create table c(dt varchar2(6));
    --添加数据
    insert into c
    select to_char(a.y)||b.m
    from a,b;SQL> select count(*) from c;
      COUNT(*)
    ----------
         12120
    --
    select * from c
    where dt like '199%';
    DT
    ------
    199001
    199101
    199201
    199301
    199401
    199501
    199601
    199701
    199801
    199901
    199002
    199102
    199202
    199302
    199402
    199502
    199602
    199702
    199802
    199902
    ...
    120 rows selected
      

  2.   


    SQL> select * from c
      2  where dt like '1990%';DT
    ------
    199003
    199001
    199002
    199006
    199007
    199008
    199004
    199005
    199010
    199011
    199012
    19900912 rows selected
      

  3.   


    --存储过程实现,还没直接写insert好,在sqldevelop环境下;
    --但是在实际程序中调用时写过程比较好:
    create or replace pro_insert_c
    as
    begin
         insert into c
         select to_char(a.y)||b.m
         from a,b;
         commit;
         exception
         when others then
              rollback;
    end pro_insert_c;
      

  4.   

    哦,试试connect by,可以简单的达到你的要求,而不需要建立这些表。
      

  5.   


    create table t1(y varchar2(4));
    insert into t1 select rownum+2000 from dual connect by rownum<=10
    create table t2(m varchar2(2));
    insert into t2 select lpad(rownum,2,'0') from dual connect by rownum<12
    create table t3(ym varchar2(6)); 
    insert into t3 select t1.y||t2.m from t1,t2200101
    200102
    200103
    200104
    200105
    200106
    200107
    200108
    200109
    200110
    200111
    200112
    200201
    200202
    200203
    200204
    200205
    200206
    200207
    200208
    200209
    200210
    200211
    200212
    200301
    200302
    200303
    200304
    200305
    200306
    200307
    200308
    200309
    200310
    200311
    200312
    200401
    200402
    200403
    200404
    200405
    200406
    200407
    200408
    200409
    200410
    200411
    200412
    200501
    200502
    200503
    200504
    200505
    200506
    200507
    200508
    200509
    200510
    200511
    200512
    200601
    200602
    200603
    200604
    200605
    200606
    200607
    200608
    200609
    200610
    200611
    200612
    200701
    200702
    200703
    200704
    200705
    200706
    200707
    200708
    200709
    200710
    200711
    200712
    200801
    200802
    200803
    200804
    200805
    200806
    200807
    200808
    200809
    200810
    200811
    200812
    200901
    200902
    200903
    200904
    200905
    200906
    200907
    200908
    200909
    200910
    200911
    200912
    201001
    201002
    201003
    201004
    201005
    201006
    201007
    201008
    201009
    201010
    201011
    201012
    201101
    201102
    201103
    201104
    201105
    201106
    201107
    201108
    201109
    201110
    201111
    201112