oracle函数中使用游标

liubaolin1 2012-04-13 10:18:26
create table issue(--问题表
is_id number(8) primary key,--问题ID
is_content varchar2(50) not null,--问题
is_amenddate date,
is_amenstaff varchar2(50)
);
create sequence seq_issue;

create table answer(--答案表
an_id number(8) primary key,
an_issueid number(8),--对应问题表ID
an_content varchar2(120),--答案
an_amenddate date,
an_amenstaff varchar2(50)
);

create sequence seq_answer;

alter table ANSWER
add foreign key (AN_ISSUEID)
references ISSUE (IS_ID);


insert into issue values(seq_issue.nextval,'zhangsan',sysdate,'张三');
insert into issue values(seq_issue.nextval,'张有多少画?',sysdate,'张三');
insert into issue values(seq_issue.nextval,'五加五等于多少?',sysdate,'张三');
insert into answer values(seq_answer.nextval,1,'create', sysdate,'李石');
insert into answer values(seq_answer.nextval,2,'7',sysdate,'王武');
insert into answer values(seq_answer.nextval,2,'不知道',sysdate,'王武');
-----------------------------------------------------------------------------------------
--自定义函数获得返回值
create or replace function ims_ing(content in varchar2)return varchar2
is
v_content varchar2(120);
BEGIN

select a.an_content into v_content from answer a where a.an_issueid in(select i.is_id from issue i where i.is_content=content);
RETURN v_content;
exception
when no_data_found then
raise_application_error(-20001,'你输入的问题无效或没有答案!');
END;


select ims_ing('张有多少画?') from dual;
-----------------------------------------------------------------------------------------------
有错误 函数不可以编译 因为有时候有多个返回值 但是我只想要一个返回值怎么办? 一定要在函数里面用到游标!!!
-------------:
create or replace function ims_ing(content in varchar2)return varchar2
is
v_content varchar2(120);
v_content varchar2(120);
declare cura is
select a.an_content into v_content from answer a where a.an_issueid in(select i.is_id from issue i where i.is_content=content);
BEGIN
open cura;
fetch cura into v_content;
if cura%notfound then
-- 未找到
else
-- 多个没有判断,只取第一个
null;
end if;
close cura;
RETURN v_content;
exception
when no_data_found then
raise_application_error(-20001,'你输入的问题无效或没有答案!');
END;
...全文
542 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
liubaolin1 2012-04-13
  • 打赏
  • 举报
回复
第一个就可以了! 虽然在 select a.an_content into v_content from answer a where a.an_issueid in(select i.is_id from issue i where i.is_content=content)and rownum=1;就可以取得第一个值 但是我想使用游标
xpingping 2012-04-13
  • 打赏
  • 举报
回复
那当返回多个值的时候,你要的是多个值中的哪个值啊……
liubaolin1 2012-04-13
  • 打赏
  • 举报
回复
嗯 是的 但是怎么改呢? 改了一个早上了 不会了 帮帮忙 谢谢
我心飞翔 2012-04-13
  • 打赏
  • 举报
回复
对啊,一个问题可能有多个答案,所以选择语句的可能返回多个结果。
必须使用游标,然后根据需要只要第1条记录。

3,491

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 高级技术相关讨论专区
社区管理员
  • 高级技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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