我想实现这样一个功能:
每从表test01 (A表)中读取一条记录时,将其复制到test02 (B表)中,其中AB表结构一致。
于是写了一个测试语句如下,假设A表有2条记录,运行完毕B应该也有2条记录,但是运行后发现,B表中有了4条记录!
再测试发现,如果A中有3条记录,则生成B表有9条记录,我对游标运用不熟悉,没想出所以然,求指点——
declare
cursor cc is select * from test01;
ccrec cc%rowtype;
rec_ind number;
begin
Execute immediate 'truncate table test02';for ccrec in cc loop
Execute immediate'
insert into test02
select
usr_nbr
,cnt
,vc_begin_cnt
,vc_begin_cnt
,vc__desc from test01
';
commit;end loop;
end;
每从表test01 (A表)中读取一条记录时,将其复制到test02 (B表)中,其中AB表结构一致。
于是写了一个测试语句如下,假设A表有2条记录,运行完毕B应该也有2条记录,但是运行后发现,B表中有了4条记录!
再测试发现,如果A中有3条记录,则生成B表有9条记录,我对游标运用不熟悉,没想出所以然,求指点——
declare
cursor cc is select * from test01;
ccrec cc%rowtype;
rec_ind number;
begin
Execute immediate 'truncate table test02';for ccrec in cc loop
Execute immediate'
insert into test02
select
usr_nbr
,cnt
,vc_begin_cnt
,vc_begin_cnt
,vc__desc from test01
';
commit;end loop;
end;
解决方案 »
- plsql或者其它oracle客户端工具连接oracle老是会断,要重新连接
- 怎么导出tablespace
- 在一处sql语句上遇到了麻烦,希望得到你们的指点
- oracle数据库启动相关问题讨论
- 无效的列索引
- 急!急!急!急!请教大家一个,oracle存储过程中处理结果,本姑娘在此先谢过!
- 执行SQL语句报错,请高手指点,多谢!
- 问下,rman备份需要什么权限以及条件啊?我fat用户有connect、resoures角色能不能做rman备份呢?
- 程序中如何调用EXP命令备份数据库?
- Oracle调用webservice接口
- sys_connect_by_path连接的字符串顺序反了?
- .dat文件怎样导入oracle数据库???
感谢,呵呵,又是你哦。我现在知道是全部插入,但跟我所理解的意思并不同。
是这样我说一下我对这语句的理解,游标变量ccrec每从cc中读取一条记录时,作一次循环,就是把它插到B表,然后再读取下一条,再做一次循环——A二条记录,全部读取完成后就退出循环。这样理解是哪里错了呢?
是吧?我去试试
--你都取出来了,你可以这样写
declare
cursor cc is select * from test01;
ccrec cc%rowtype;
rec_ind number;
begin
Execute immediate 'truncate table test02';for ccrec in cc
loop
Execute immediate '
begin
insert into test02
select '''||ccrec.usr_nbr
||''','''||ccrec.cnt
||''','''||vc_begin_cnt
||''','''||vc_begin_cnt--此处我怎么感觉你的列有问题,重复了
||''','''||ccrec.vc__desc
||''' from dual;
end;';
end loop;
commit;
end;
/
--还可以这样写
declare
cursor cc is select * from test01;
ccrec cc%rowtype;
rec_ind number;
begin
Execute immediate 'truncate table test02';for ccrec in cc
loop
Execute immediate 'insert into test02
select :1,:2,:3,:4,:5 from dual'
using ccrec.usr_nbr,ccrec.cnt,ccrec.vc_begin_cnt,ccrec.vc_begin_cnt,ccrec.vc__desc;
end loop;
commit;
end;
/
谢谢谢谢。