把表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:
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语句?
解决方案 »
- 如何解决left join 被关联表不能带条件的问题
- win7安装oracle11g,plsql后,通过plsql登陆,总是显示tns-12170:连接超时,怎么办?
- ORALCE DATA GUARD 关于最大保护的问题。
- 删除oracle用户时,电脑卡死,怎么回事?
- 致急!oracle9在使用联想同传功能后客户端不能访问服务器了
- 各位老师救命:如何写一个sql脚本,删除oracle某个用户下的所有表
- 一条查询语句的存储过程为何无法编译成功
- 求一个把表记录导入到文本文件,并且带‘|’分隔符。(MySQL)
- 帮忙推荐几本ORACLE/JAVA的入门书
- ERROR:ORA-12560: TNS: 协议适配器错误
- 建表的问题
- 求助:-) oracle 一个查询的问题。
--提前声明,这招只有在表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;
/
如果有10万条数据的话,这样做大概需要多长时间?
如果怕出问题,先把过程里的commit屏蔽掉,执行下,看看数据有没有问题,没问题就commit有问题就rollback
在16楼
begin
test;
end;
就可以了
exec test;
也可以
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;