把表1中isStored='否'的数据分解存到表2、3、4中,并将isStored改为'是'。 
表1: 
id  question    answer  isStored 
1 ...... 
2 ...... 
3  刘德华有多大  快50了    否 
4  张学友帅吗    一般      否 
5  郭富城帅吗    很帅      是 
6 ...... 
7 ...... 本来表2、3、4里面没有数据,将表1的数据插入到表2、3、4以后就是下面这样。 
表2: 
id  question 
1  刘德华有多大  
2  张学友帅吗 表3: 
id  answer 
1  快50了 
2  一般 表4: 
id  question_id  answer_id 
1      1          1 
2      2          2 请问达到上面的效果怎么写sql语句?

解决方案 »

  1.   

    实在不行,用cursor吧取出来向表2和表3插入一条后,马上把对应关系插入表4
      

  2.   

    大体上是这么个意思
    --提前声明,这招只有在表2和表3自动生成的主键是不断增加的情况下才好使,而且有可能执行起来比较慢
    create or replace procedure test as
    v_id number;
    v_id1 number;             --这俩分别跟表2和表3的id类型一致
    v_question varchar2(100); --这个跟question类型一致
    v_answer varchar2(100);   --这个跟answer的类型一致
    v_id2 number;             --这个跟表1的id类型一致
    cursor v_cur is select question,answer,id from table1 where sStored ='否';
    begin
     open v_cur;
     fetch v_cur into v_quertion,v_answer,v_id2;
     while v_cur %found loop
      insert into table2 (question) values (v_question);
      insert into table3 (answer) values (v_answer);
      select max(id) into v_id from table2;
      select max(id) into v_id1 from table3;
      insert into table3 (question_id,answer_id) values(v_id,v_id1);
      update table1 set sStored ='是' where id=v_id2;
      commit;
      fetch v_cur into v_quertion,v_answer,v_id2;
     end loop;
     close v_cur;
    end;
    /
      

  3.   

    是insert时候触发的吗,如果是不断增长的就没问题
      

  4.   

    对的,是insert时候触发,应该没问题吧?
    如果有10万条数据的话,这样做大概需要多长时间?
      

  5.   

    速度具体我也不清楚,反正是一条一条处理的,还有,执行前别忘了把变量按我描述的改成你相应的类型
    如果怕出问题,先把过程里的commit屏蔽掉,执行下,看看数据有没有问题,没问题就commit有问题就rollback
      

  6.   

    你看看这位兄弟的方法怎么样http://topic.csdn.net/u/20090423/11/7c549d27-0065-44c3-95dd-1a42326772f3.html
    在16楼
      

  7.   

    落花惊魂:我弱弱的问一句,建好procedure以后怎么调用它?
      

  8.   

    直接执行吗?
    begin
     test;
    end;
    就可以了
    exec test;
    也可以
      

  9.   

    这样是不是能简单一点:
    create table t1(id int,question varchar2(20),answer varchar2(20),isstored varchar2(2))
    create table t2(id int,question varchar2(20))
    create table t3(id int,answer varchar2(20))
    create table t4(id int,question_id int,answer_id int)insert into t1 values(1,'刘德华有多大', '快50了','否'); 
    insert into t1 values(2,'张学友帅吗', '一般','否');  
    insert into t1 values(3,'郭富城帅吗', '很帅','是'); select * from t1;
    begin
      insert all
        into t2 values(id,question)
        into t3 values(id,answer)
        into t4 values(id,id,id)
      select id,question,answer from t1 where isstored='否';
      
      update t1 set isstored='是' where isstored='否';
    end;
      

  10.   

    这个我没用过,看不太懂-_-||,是不是传说当中的同时插入多表,还有这个过程中插入t4的后两个id是t2和t3的主键吗?
      

  11.   

    insert all是可以通过一个表向多个表中insert数据,对于insert into t4我是根据他提供的数据进行做的,至于是否合理没验证,:)"INSERT ALL是9i新增的语法,它扩充了原有的INSERT语句,使得INSERT语句从原来的只能插入到一张表发展到可以同时插入多张表,还可以根据判断条件来决定每条记录插入到哪张或哪几张表中。"