龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 数据库类 > Oracle 技术 >

Oracle通过存储过程如何正确返回数据集?

时间:2011-04-12 23:18来源:未知 作者:admin 点击:
分享到:
文章主要教会你如何正确的使用 Oracle 存储过程使其返回相关的数据集的实际操作步骤,我们大家都知道在Oracle中存储过程的返回相关的数据集主要作用是通过相关ref cursor类型数据的实

文章主要教会你如何正确的使用Oracle存储过程使其返回相关的数据集的实际操作步骤,我们大家都知道在Oracle中存储过程的返回相关的数据集主要作用是通过相关ref cursor类型数据的实际应用参数返回的,而返回数据的参数应该是out或in out类型的。

由于在定义Oracle存储过程时无法直接指定参数的数据类型为:ref cursor,而是首先通过以下方法将ref cursor进行了重定义:

  1. create or replace package FuxjPackage is  
  2. type FuxjResultSet is ref cursor;  

还可以定义其他内容

  1. end FuxjPackage; 

再定义Oracle存储过程:

  1. create or replace procedure UpdatefuxjExample 
    (sDM in char,sMC in char, pRecCur in out FuxjPackage.FuxjResultSet)  
  2. as  
  3. begin  
  4. update fuxjExample set mc=sMC where dm=sDM;  
  5. if SQL%ROWCOUNT=0 then  
  6. rollback;  
  7. open pRecCur for  
  8. select '0' res from dual;  
  9. else  
  10. commit;  
  11. open pRecCur for  
  12. select '1' res from dual;  
  13. end if;  
  14. end;  

  1. create or replace procedure InsertfuxjExample 
    (sDM in char,sMC in char, pRecCur in out FuxjPackage.FuxjResultSet)  
  2. as  
  3. begin  
  4. insert into FuxjExample (dm,mc) values (sDM,sMC);  
  5. commit;  
  6. open pRecCur for  
  7. select * from FuxjExample;  
  8. end;  

二、在Delphi中调用返回数据集的Oracle存储过程

可以通过TstoredProc或TQuery控件来调用执行返回数据集的存储,数据集通过TstoredProc或TQuery控件的参数返回,注意参数的DataType类型为ftCursor,而参数的ParamType类型为ptInputOutput。

使用TstoredProc执行UpdatefuxjExample的相关设置为:

  1. object StoredProc1: TStoredProc  
  2. DatabaseName = 'UseProc' 
  3. StoredProcName = 'UPDATEFUXJEXAMPLE' 
  4. ParamData = < 
  5. item 
  6. DataType = ftString 
  7. Name = 'sDM' 
  8. ParamType = ptInput 
  9. end  
  10. item  
  11. DataType = ftString 
  12. Name = 'sMC' 
  13. ParamType = ptInput 
  14. end  
  15. item  
  16. DataType = ftCursor 
  17. Name = 'pRecCur' 
  18. ParamType = ptInputOutput 
  19. Value = Null 
  20. end> 
  21. end   

以上的相关内容就是对Oracle存储过程中返回数据集的介绍,望你能有所收获。

精彩图集

赞助商链接