2

我想检查光标是否只有一行。并返回这个,

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

    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;
4

2 回答 2

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;
于 2012-12-11T09:32:39.997 回答
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;
于 2012-12-11T08:17:52.090 回答