参考光标类型 oracle
Posted
技术标签:
【中文标题】参考光标类型 oracle【英文标题】:Ref Cursor Type oracle 【发布时间】:2012-12-11 07:42:46 【问题描述】:我想检查光标是否只有一行。 并返回这个,
--请看我的问题在程序里面吗?
CREATE OR REPLACE PROCEDURE GetInterestRate( p_id IN NUMBER DEFAULT NULL,
RC1 IN OUT SYS_REFCURSOR
)
....
begin
open rc1 for select * from interestRatesTable i join parametersInterest p on
i.interestName = p.Name
where i.idinterest = p_id
and p.Active ='A';
-- I would like to check if cursor have only one row.
--- if it has zero row, no result I got to raise an error
--- if it has more than one row, I got to raise to many interest rates!
-- how can I do this?
end;
【问题讨论】:
和java有什么关系? 我认为java开发人员应该熟悉这个案例。我只是为了礼貌而删除...... 【参考方案1】:Oracle 会为您提出错误,只需在一行中选择一个值 我想你也应该在异常块中捕获异常。
CREATE OR REPLACE PROCEDURE GetInterestRate( p_id IN NUMBER DEFAULT NULL,
RC1 IN OUT SYS_REFCURSOR
)
....
begin
select 1
into a_value
from interestRatesTable i
join parametersInterest p ...
open rc1 for ......
EXCEPTION
WHEN NO_DATA_FOUND THEN
.....
WHEN TOO_MANY_ROWS THEN
.....
WHEN OTHERS THEN
.....
end;
【讨论】:
【参考方案2】:我认为您必须先运行查询才能打开光标。
CREATE OR REPLACE PROCEDURE ...
begin
select count(*)
into row_found
from interestRatesTable i join parametersInterest p ...
and rownum = 1;
if row_found = 0 then raise ...
end if
open ...
end;
【讨论】:
以上是关于参考光标类型 oracle的主要内容,如果未能解决你的问题,请参考以下文章