参考光标类型oracle

时间:2012-12-11 07:42:46

标签: sql oracle stored-procedures plsql

我想检查游标是否只有一行。  并返回此,

- 请在程序中查看我的问题?

    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;

2 个答案:

答案 0 :(得分:3)

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;

答案 1 :(得分: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;