SQL> select * from test; T1 T2 T3 A1
---------- ---------- ---------- --------
1 2 3 123create or replace procedure myproc(flag in number)
as
type my_cursor is ref cursor;
select_cur my_cursor;
str1 varchar2(200);
str2 varchar2(50);
v1 test.t1%type;
v2 test.t2%type;
begin
case flag
when 0 then str1:='select t1,t2 from test';
when 1 then str1:='select t1,t3 from test';
else str1:='select t2,t3 from test';
end case;
open select_cur for str1;
loop
fetch select_cur into v1,v2;
exit when select_cur%notfound;
dbms_output.put_line(v1+v2);
end loop;
close select_cur;
end;
/SQL> exec myproc(1)
4PL/SQL 过程已成功完成。============================
你的游标fetch到哪个变量名都一样的,所有没有必要取不同的名字
只要你的t1,t2,t3的数据类型一致即可
---------- ---------- ---------- --------
1 2 3 123create or replace procedure myproc(flag in number)
as
type my_cursor is ref cursor;
select_cur my_cursor;
str1 varchar2(200);
str2 varchar2(50);
v1 test.t1%type;
v2 test.t2%type;
begin
case flag
when 0 then str1:='select t1,t2 from test';
when 1 then str1:='select t1,t3 from test';
else str1:='select t2,t3 from test';
end case;
open select_cur for str1;
loop
fetch select_cur into v1,v2;
exit when select_cur%notfound;
dbms_output.put_line(v1+v2);
end loop;
close select_cur;
end;
/SQL> exec myproc(1)
4PL/SQL 过程已成功完成。============================
你的游标fetch到哪个变量名都一样的,所有没有必要取不同的名字
只要你的t1,t2,t3的数据类型一致即可
解决方案 »
- expdp/impdp导出导入时日志文件不全的问题
- 求助系统表与数据表关联语句
- oracle10g用域用户安装后database control无法启动,怎么解决?
- plsql developer调用别的包时调试问题
- Oracle的面向对像流行吗,大侠们?
- Oracle触发器问题
- plsql 中有类似于哈希表的东西吗~
- 初学Oracle求个简单的触发器 在线等
- 问一下oracle锁行的问题
- 求助,oracle 11g r2(32位)安装卡在2%
- oracle中有没有计算日期的函数?
- 环境:windows2003+oracle9i+asp程序中使用odbc方式连接,程序运行一会后总容易连接中断??
as
type my_cursor is ref cursor;
select_cur my_cursor;
str1 varchar2(200);
my_record test%rowtype;
begin
case flag
when 0 then str1:='select t1,t2 from test';
when 1 then str1:='select t1,t3 from test';
else str1:='select t2,t3 from test';
end case;
open select_cur for str1;
loop
fetch select_cur into my_record;
exit when select_cur%notfound;
dbms_output.put_line(my_record.t1||' '||my_record.t2||' '||my_record.t3);
end loop;
close select_cur;
end;
/SQL> select * from test; T1 T2 T3 A1
---------- ---------- ---------- --------
1 2 3 123SQL> exec myproc(0)
1 2PL/SQL 过程已成功完成。SQL> exec myproc(1)
1 3PL/SQL 过程已成功完成。SQL> exec myproc(2)
2 3PL/SQL 过程已成功完成。
再请教:test表有三个字段,而str1每次只取两个不同的字段,fetch select_cur into my_record;语句可以自动对应t1、t2或t3吗?麻烦给解释解释,让我长长知识,谢谢!
fetch select_cur into my_record;
由于你的t1,t2,t3都是一样的
只要这样就可以了,因为每次你只是取出两个字段
dbms_output.put_line(my_record.t1||' '||my_record.t2);你说的没错。。每次fetch都会依次对应my_record每个字段
要注意的是每次fetch是两个字段,
我的my_record是你的表的一个记录类型
相当于一次性定义了下面的四次定义
test.t1%type
test.t2%type
test.t3%type
test.a1%type当你select t1,t3的时候,会把t3赋值给my_record.t2
但是由于你的t1,t2,t3是一样的,所以没有关系
如果你是select * from test
那么忽会依次填满my_record
case flag
when 0 then str1:='select t1,t2,'','' from test';
when 1 then str1:='select t1,'',t3,'' from test';
else str1:='select '',t2,t3,'' from test';
end case;那就一一对应了。。
case flag
when 0 then str1:='select t1,t2,t3 from test';
when 1 then str1:='select t2,t5 from test';
when 2 then str1:='select t1,t2,t8,t9 from test;
when 3 then str1:='select t2 from test';
end case;
又该如何?所以我才会想能否这样写:(写法肯定不对,只是想表达意思)
case flag
when 0 then str1:='select t1,t2,t3 from test';
v_str1:='v_t1,v_t2,v_t3';
when 1 then str1:='select t2,t5 from test';
v_str1:='v_t2,v_t5';
when 2 then str1:='select t1,t2,t8,t9 from test;
v_str1:='v_t1,v_t2,v_t8,v_t9';
when 3 then str1:='select t2 from test';
v_str1:='v_t2';
end case;
.....
while
......
fetch str1 into v_str1;
.....
loop
as
type my_cursor is ref cursor;
select_cur my_cursor;
str1 varchar2(200);
str2 varchar2(30000);
begin
case flag
when 0 then str1:='select t1||'' ''||t2 from test';
when 1 then str1:='select t1||'' ''||t3 from test';
else str1:='select t2||'' ''||t3 from test';
end case;
open select_cur for str1;
loop
fetch select_cur into str2;
exit when select_cur%notfound;
dbms_output.put_line(str2);
end loop;
close select_cur;
end;
/SQL> select * from test;T1 T2 T3 A1
---------- ---------- ---------- --------
30-11月-04 1 10 aaaSQL> edit car.sqlSQL> exec myproc(0)
30-11月-04 1PL/SQL 过程已成功完成。SQL> exec myproc(1)
30-11月-04 10PL/SQL 过程已成功完成。SQL> exec myproc(2)
1 10PL/SQL 过程已成功完成。
这本来就是一个数据集
只不过现在是把所有字段合并在一起。。用空格分隔每个字段的内容
其实我不知道你具体使用时要达到怎样的效果
我也没有时间帮你想了。。你如果要在应用程序端返回结果集,那只要一句SQL语句即可。。无需这样复杂。。