如果有序列号的话:
insert into tbname2 select col1,col2... from tbname1 where id>0 and id<1001;

解决方案 »

  1.   

    没有序列号,所以我打算用rownum和一种可以在游标里定位的东西结合起来一次进行1000条的插入。就象java中的rs.absolute()定位一样。在pl/sql里有这样的语句吗?
      

  2.   

    insert into table2 select col1,col2,... from table1 where rownum >0 and rownum <= 1000
      

  3.   

    用rownum不好,需要先select然后在引用,速度慢
      

  4.   

    不明白为什么要用cursor,难道直接用sql完成不了吗?
      

  5.   

    insert into table2 select col1,col2,... from table1 where rownum >0 and rownum <= 1000
    第1到1000条当然可以这样用,但是第1001到2000呢?我是想要找一种最快的并且不锁表可以让其他用户正常使用的,而我后台可以不停循环的处理最后直到50多万条记录全部导完为止。但是显然不能用rownum>1001 and rownum<2000的啊?
    直接用sql不行,因为数据之间有很多差异,需要先单独把数据从大表拿出来然后经过处理过后再写到另外两个小表里。所以就放在procedure里用cursor来做了
      

  6.   

    你并没有说明你的所谓的处理能否由sql来完成
    也许你认为不可以实际却可以呢?再说了,处理50万条记录,也不是什么大事,很快就可以完成
    当然,如果你这种操作是要写一个程序来频繁的调用,那是值得考虑的
    否则,你尽管去做好了
      

  7.   

    一条SQL语句肯定是不可能完成的。所以我才要写一个procedure来进行处理。如果一次把全部记录都放到一个cursor里的话需要一个多小时才能完成,并且一次处理要保证与DB SERVER不掉线才行,所以我才想分开来处理。但需要速度还是尽可能的快才行
      

  8.   

    以下是我的代码
    create or replace procedure trans_user_info 
    IS
    cursor c1 is select * from table1;
      emp_record c1%rowtype;
      TEMP_USERNAME           VARCHAR2(30);
      TEMP_NICKNAME           VARCHAR2(30);
      TEMP_PASSWD             VARCHAR2(30);
      TEMP_RAWPASSWD          VARCHAR2(30);
      TEMP_REALNAME           VARCHAR2(30);
      TEMP_EMAIL              VARCHAR2(50);
      TEMP_SEX                NUMBER(2);
      TEMP_BIRTH              date;
      ...  TEMP_STRING             VARCHAR2(1000);
      TEMP_NUMBER             number(12);
    BEGIN
      for emp_record in c1 loop
        TEMP_USERNAME := emp_record.USERNAME;
        TEMP_NICKNAME := emp_record.NICKNAME;
        ...    if (TEMP_STRING = '女') then 
          TEMP_SEX := 1;
        else
          TEMP_SEX := 0;
        end if;
        TEMP_BIRTH :=nvl(emp_record.BIRTH,sysdate); 
        TEMP_BLOODTYPE := emp_record.BLOODTYPE;
        TEMP_STRING := emp_record.HIGHT;
        TEMP_HEIGHT := 0;
        if (length(TEMP_STRING)<=3) then
          begin      
            TEMP_HEIGHT := to_number(TEMP_STRING);
          exception 
            when others then 
              null;
          end;
        end if;
        TEMP_STRING := emp_record.WEIGHT;
        TEMP_WEIGHT := 0;
        if (length(TEMP_STRING)<=3) then
          begin      
            TEMP_WEIGHT := to_number(TEMP_STRING);
          exception 
            when others then 
              null;
          end;
        end if;
        TEMP_BODY := emp_record.BODY;
        TEMP_MARRIAGE := emp_record.MARRIAGE;
        TEMP_REGISTER_DATE := emp_record.JOINDATE;
        TEMP_USERTYPE := emp_record.USERTYPE;
        TEMP_CONSTELLATION := emp_record.CONSTELLATION;
        TEMP_STRING := emp_record.EDUCATION;
        TEMP_EDUCATION := 8;
        if (TEMP_STRING='初中') then
          TEMP_EDUCATION := 0;
        elsif (TEMP_STRING='高中') then
          TEMP_EDUCATION := 1;
        elsif (TEMP_STRING='中专') then
          TEMP_EDUCATION := 2;
        elsif (TEMP_STRING='大专') then
          TEMP_EDUCATION := 3;
        elsif (TEMP_STRING='大学') then
          TEMP_EDUCATION := 4;
        elsif (TEMP_STRING='双学位') then
          TEMP_EDUCATION := 5;
        elsif (TEMP_STRING='硕士') then
          TEMP_EDUCATION := 6;
        elsif (TEMP_STRING='博士') then
          TEMP_EDUCATION := 7;
        else
          TEMP_EDUCATION := 8;
        end if;
        TEMP_HOUSING := emp_record.HOUSING;
        TEMP_STRING := emp_record.INCOME;
        TEMP_INCOME := 0;
        if (TEMP_STRING='RMB1000以下') then
          TEMP_INCOME := 1;
        elsif (TEMP_STRING='RMB1000-2000') then
          TEMP_INCOME := 2;
        elsif (TEMP_STRING='RMB2000-3500') then
          TEMP_INCOME := 3;
        elsif (TEMP_STRING='RMB3500-5000') then
          TEMP_INCOME := 4;
        elsif (TEMP_STRING='RMB5000-7500') then
          TEMP_INCOME := 5;
        elsif (TEMP_STRING='RMB7500-10000') then
          TEMP_INCOME := 6;
        elsif (TEMP_STRING='RMB10000以上') then
          TEMP_INCOME := 7;
        else
          TEMP_INCOME := 0;
        end if;
        ...    if (TEMP_NUMBER=1) then
          TEMP_PICTURE := 1;
        end if;
        TEMP_UPLOADDATE := emp_record.UPLOADDATE;
        TEMP_COMMON_BAK1 := '';
        TEMP_COMMON_BAK2 := '';
        TEMP_STRING := emp_record.PENNAME;
        TEMP_NICKNAME_CHANGED := 0;
        if (TEMP_STRING='**ALREADY CHANGED**') then
          TEMP_NICKNAME_CHANGED := 1;
        end if;
        TEMP_LAST_VALID_LOGIN := emp_record.LOGIN;
        
        if (length(TEMP_STRING)<=3) then
          begin      
            TEMP_CIRCUMFERENCE := to_number(TEMP_STRING);
          exception 
            when others then 
              null;
          end;
        end if;
        TEMP_STRING := emp_record.WAISTLINE;
        TEMP_WAISTLINE := 0;
        if (length(TEMP_STRING)<=3) then
          begin      
            TEMP_WAISTLINE := to_number(TEMP_STRING);
          exception 
            when others then 
              null;
          end;
        end if;
        TEMP_STRING := emp_record.TUNWEI;
        TEMP_TUNWEI := 0;
        if (length(TEMP_STRING)<=3) then
          begin      
            TEMP_TUNWEI := to_number(TEMP_STRING);
          exception 
            when others then 
              null;
          end;
        end if;
        TEMP_PARENT_APPRAISE := emp_record.PARENT_APPRAISE;
        ...
        
        insert into table2 (USERNAME,NICKNAME,PASSWD,RAWPASSWD,
          REALNAME,EMAIL,SEX,...) values (
          TEMP_USERNAME,TEMP_NICKNAME,...);
        insert into table3(USERNAME,NICKNAME_CHANGED,...) 
          values (TEMP_USERNAME,TEMP_NICKNAME_CHANGED,TEMP_LAST_VALID_LOGIN,
          ...);
        
        commit;
      end loop;
      
    END;
    主要目的就是把table1中的数据导到table2和table3中,中间省略了很多变量的声明及普通的赋值操作。(省略地方用...来表示)如果这么做的话,一个多小时可以做完,但是如果网络连接中断的话,因为数据都是放在一个大记录集中所以很危险。但如果用rownum和minus结合来每次小批量做速度好象更慢的。不知各位高手有何意见。
      

  9.   

    先用select语句将50万条记录到到文本文件中,再用sql loader的direct方式将数据到入目标表。这需要编写sql loader的控制文件。
      

  10.   

    这么长的一段,没什么复杂的逻辑,用两句sql插入table2,table3不就行了吗?下面是一个示例,希望你能理解
    insert into table2 (USERNAME,NICKNAME,PASSWD,RAWPASSWD,REALNAME,EMAIL,SEX,...)
    select USERNAME,NICKNAME,....,
           decode(sex,'女',1,0),...,
           decode(education,'初中',0,'高中',1,'中专',2,...,8),...
     from table1;
      

  11.   

    比你们都菜的人请教一下:
    insert into table2 select col1,col2,... from table1 where rownum >1000 and rownum <= 2000
    这样的句子能否取出结果集
      

  12.   

    不能,你可以这样写:
    insert into table2 
    (select col1,col2,... from table1 where rownum <= 2000
     minus
     select col1,col2,... from table1 where rownum <1001)
      

  13.   

    不能,你可以这样写:
    insert into table2 
    (select col1,col2,... from table1 where rownum <= 2000
     minus
     select col1,col2,... from table1 where rownum <1001)
      

  14.   

    我搞不懂为什么原来的height, weight要用字符型?即使用字符型也可以增加约束啊
      

  15.   

    如楼上弱水所言,使用decode基本足够为什么要武断的认为sql无法处理呢?再说了,我实在无法想象50万条记录处理需要1小时  :)