师妹向各位师G求教游标运行动态SQL的问题

qingth 2003-10-29 05:41:12
各位师兄:
大家好,我知道动态运行SQL比如:
declare
str varchar(2000);
begin
str:='insert into test values(1,2)';
execute immediate str;
end;
可以运行。可是我将SQL语句存放到一个表中,然后打开表,用游标将SQL语句
一条条提出来运行,却不行,出现什么连不上数据库的错误!!!
怎么回事,应该如何解决呢?谢谢赐教!
...全文
26 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
beckhambobo 2003-10-30
  • 打赏
  • 举报
回复
你说的是连接不上数据库,哪与oracle服务器可能性大,检查吧
qingth 2003-10-30
  • 打赏
  • 举报
回复
绝了,我一条条动态SQL运行,就是没错,而且很快,
可是用游标处理就是不行。出现的错误依旧。
TO beckhambobo(beckham)
第一个问题是否与oracle服务器内存不足,或其它问题,检查一下吧?
用游标就提取两条动态SQL语句也会内存不足吗?如果真是那怎么办?
谢谢赐教。
newyu1127 2003-10-30
  • 打赏
  • 举报
回复
UP
newyu1127 2003-10-30
  • 打赏
  • 举报
回复
哦,TO beckhambobo(beckham) ,那我就按你的写了。
还有我知道
查看未关闭的游标
select * from sys.gv_$open_cursor;
但如何关闭这些未关闭的游标呢?
qingth 2003-10-30
  • 打赏
  • 举报
回复
UP
qingth 2003-10-30
  • 打赏
  • 举报
回复
新的情况:
世界真是太奇妙了,就以jiezhi()的例子来说吧(环境为PL/SQL):
insert into t values('insert into t0 values(1,''James'')');
这句最后有分号的,可以运行。
insert into t0 values(1,'James');
这句也有分号,却不能运行,把分号去掉就可以运行了。如果仍要带分号运行
必须写成
begin
insert into t0 values(1,'James');
end;
绝吧。为什么要说这么多,因为我的问题最终就跟这个分号有关系。
我把jiezhi()的例子中的
insert into t values('insert into t0 values(1,''James'')');
insert into t values('insert into t0 values(2,''Mike'')');
改成了
insert into t values('begin insert into t0 values(1,''James'');end;');
insert into t values('begin insert into t0 values(2,''Mike''); end;');
请注意其中的变化!!!

而存储过程是这样的:
create or replace procedure test is
cursor c is select text from t;
s t.text%type;
begin
open c;
fetch c into s;
while c%found loop
execute immediate s;
commit;
fetch c into s;
end loop;
close c;
exception
when others then
dbms_output.put_line('error!!!');
rollback;
end test;

然后
declare
begin
test;
end;
测试通过,而且记录也增加进去了.
世界真是太奇妙了,这是为什么呢?


jiezhi 2003-10-29
  • 打赏
  • 举报
回复
open c;
fetch c into s;
while c%found loop
execute immediate s;
fetch c into s;
--------请注意这里

EXIT WHEN s='select';
---------------------
end loop;
close c;
exception
when others then
dbms_output.put_line('error!!!');
rollback;
beckhambobo 2003-10-29
  • 打赏
  • 举报
回复
if s='select' then
close c;
return;
end if;

第一个问题是否与oracle服务器内存不足,或其它问题,检查一下吧
newyu1127 2003-10-29
  • 打赏
  • 举报
回复
跟你写的一样啊,在SQL/PLUS 和PL/SQL里都不行,出现如下错误:
ERROR:
ORA-03114: 未连接到 ORALCE


declare
*
ERROR 位于第 1 行:
ORA-03113: end-of-file on communication channel
可是我其它的存储过程对表进行增加删除都可以啊?为什么会这样?!!!
还有一个问题就是:(与当前问题无关哦)
如果程序中这样写:
open c;
fetch c into s;
while c%found loop
execute immediate s;
fetch c into s;
--------请注意这里
if s='select' then
return;
---------------------
end loop;
close c;
exception
when others then
dbms_output.put_line('error!!!');
rollback;
。。。。。
也就是在中间出现return退出存储过程后,游标会不会自动关闭,
如果不会,应该怎么办?会有什么问题呢?
谢谢赐教。

beckhambobo 2003-10-29
  • 打赏
  • 举报
回复
declare
str varchar(2000);
type t_sor is ref cursor;
v_sor t_sor;
begin
str:='insert into test values(1,2)';
execute immediate str;
commit;
open v_sor for select * from test;
loop
fetch v_sor into ...;
exit when v_sor%notfound;
dbms_output.put_line(...);
end loop;
close v_sor;
end;
/

jiezhi 2003-10-29
  • 打赏
  • 举报
回复
SQL> create table t(text varchar2(1000));

Table created

SQL> create table t0(id integer,name varchar2(30));

Table created

SQL> insert into t values('insert into t0 values(1,''James'')');

1 row inserted

SQL> insert into t values('insert into t0 values(2,''Mike'')');

1 row inserted

SQL> select * from t;

TEXT
--------------------------------------------------------------------------------
insert into t0 values(2,'Mike')
insert into t0 values(1,'James')


create or replace procedure test is
cursor c is select text from t;
s t.text%type;
begin
open c;
fetch c into s;
while c%found loop
execute immediate s;
fetch c into s;
end loop;
close c;
exception
when others then
dbms_output.put_line('error!!!');
rollback;
end test;

SQL> set serveroutput on;
SQL> declare
2 begin
3 test;
4 end;
5 /

PL/SQL procedure successfully completed

SQL> select * from t0;

ID NAME
--------------------------------------- ------------------------------
2 Mike
1 James

SQL>

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧