Oracle中通过存储过程中返回数据集及在Delphi中使用

2016-02-19 19:56 27 1 收藏

今天图老师小编给大家介绍下Oracle中通过存储过程中返回数据集及在Delphi中使用,平时喜欢Oracle中通过存储过程中返回数据集及在Delphi中使用的朋友赶紧收藏起来吧!记得点赞哦~

【 tulaoshi.com - 编程语言 】

 

  一、使用存储过程返回数据集
  Oracle中存储过程返回数据集是通过ref cursor类型数据的参数返回的,而返回数据的参数应该是out或in out类型的。
  由于在定义存储过程时无法直接指定参数的数据类型为:ref cursor,而是首先通过以下方法将ref cursor进行了重定义:
  create or replace package FuxjPackage is
  type FuxjResultSet is ref cursor;
  --还可以定义其他内容
  end FuxjPackage;
  再定义存储过程:
  create or replace procedure UpdatefuxjExample (sDM in char,sMC in char, pRecCur in out FuxjPackage.FuxjResultSet)
  as
  begin
  update fuxjExample set mc=sMC where dm=sDM;

  if SQL%ROWCOUNT=0 then
  rollback;
  open pRecCur for
  select '0' res from dual;
  else
  commit;
  open pRecCur for
  select '1' res from dual;
  end if;
  end;
  和
  create or replace procedure InsertfuxjExample (sDM in char,sMC in char, pRecCur in out FuxjPackage.FuxjResultSet)
  as
  begin
  insert into FuxjExample (dm,mc) values (sDM,sMC);
  commit;
  open pRecCur for
  select * from FuxjExample;
  end;
  
  二、在Delphi中调用返回数据集的存储过程
  可以通过TstoredProc或TQuery控件来调用执行返回数据集的存储,数据集通过TstoredProc或TQuery控件的参数返回,注意参数的DataType类型为ftCursor,而参数的ParamType类型为ptInputOutput。
  使用TstoredProc执行UpdatefuxjExample的相关设置为:
  object StoredProc1: TStoredProc
  DatabaseName = 'UseProc'
  StoredProcName = 'UPDATEFUXJEXAMPLE'
  ParamData =
  item
  DataType = ftString
  Name = 'sDM'
  ParamType = ptInput
  end
  item
  DataType = ftString
  Name = 'sMC'
  ParamType = ptInput
  end
  item
  DataType = ftCursor
  Name = 'pRecCur'
  ParamType = ptInputOutput
  Value = Null
  end
  end
  执行方法为:
  StoredProc1.Params.Items[0].AsString:=Edit1.Text; //给参数赋值;
  StoredProc1.Params.Items[1].AsString:=Edit2.Text; //给参数赋值;
  StoredProc1.Active:=False;
  StoredProc1.Active:=True; //返回结果集
  使用TQuery执行InsertfuxjExample的相关设置为:
  object Query1: TQuery
  DatabaseName = 'UseProc'
  SQL.Strings = (
  'begin'
  ' InsertfuxjExample(sDM=大笑M,sMC=:mc,pRecCur=:RecCur);'
  'end;')
  ParamData =
  item
  DataType = ftString
  Name = 'DM'
  ParamType = ptInput
  end
  item
  DataType = ftString
  Name = 'mc'
  ParamType = ptInput
  end
  item
  DataType = ftCursor
  Name = 'RecCur'
  ParamType = ptInputOutput
  end
  end
  执行方法为:
  Query1.Params.Items[0].AsString:=Edit3.Text; //给参数赋值;
  Query1.Params.Items[1].AsString:=Edit4.Text; //给参数赋值;
  Query1.Active:=False;
  Query1.Active:=True;

  

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  
  if SQL%ROWCOUNT=0 then
  rollback;
  open pRecCur for
  select '0' res from dual;
  else
  commit;
  open pRecCur for
  select '1' res from dual;
  end if;
  end;
  和
  create or replace procedure InsertfuxjExample (sDM in char,sMC in char, pRecCur in out FuxjPackage.FuxjResultSet)
  as
  begin
  insert into FuxjExample (dm,mc) values (sDM,sMC);
  commit;
  open pRecCur for
  select * from FuxjExample;
  end;
  
  二、在Delphi中调用返回数据集的存储过程
  可以通过TstoredProc或TQuery控件来调用执行返回数据集的存储,数据集通过TstoredProc或TQuery控件的参数返回,注意参数的DataType类型为ftCursor,而参数的ParamType类型为ptInputOutput。
  使用TstoredProc执行UpdatefuxjExample的相关设置为:
  object StoredProc1: TStoredProc
  DatabaseName = 'UseProc'
  StoredProcName = 'UPDATEFUXJEXAMPLE'
  ParamData =
  item
  DataType = ftString
  Name = 'sDM'
  ParamType = ptInput
  end
  item
  DataType = ftString
  Name = 'sMC'
  ParamType = ptInput
  end
  item
  DataType = ftCursor
  Name = 'pRecCur'
  ParamType = ptInputOutput
  Value = Null
  end
  end
  执行方法为:
  StoredProc1.Params.Items[0].AsString:=Edit1.Text; //给参数赋值;
  StoredProc1.Params.Items[1].AsString:=Edit2.Text; //给参数赋值;
  StoredProc1.Active:=False;
  StoredProc1.Active:=True; //返回结果集
  使用TQuery执行InsertfuxjExample的相关设置为:
  object Query1: TQuery
  DatabaseName = 'UseProc'
  SQL.Strings = (
  'begin'
  ' InsertfuxjExample(sDM=大笑M,sMC=:mc,pRecCur=:RecCur);'
  'end;')
  ParamData =
  item
  DataType = ftString
  Name = 'DM'
  ParamType = ptInput
  end
  item
  DataType = ftString
  Name = 'mc'
  ParamType = ptInput
  end
  item
  DataType = ftCursor
  Name = 'RecCur'
  ParamType = ptInputOutput
  end
  end
  执行方法为:
  Query1.Params.Items[0].AsString:=Edit3.Text; //给参数赋值;
  Query1.Params.Items[1].AsString:=Edit4.Text; //给参数赋值;
  Query1.Active:=False;
  Query1.Active:=True;

  

  附:创建返回数据集的存储过程 简单框架

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  1.
  create or replace package TestPackage is
  type TestResultSet is ref cursor;
  end TestPackage ;
  
  2.
  create or replace procedure Test
  (
  pRecCur in out TestPackage .TestResultSet
  )
  as
  begin
  open pRecCur for
  select * from table;
  end;


来源:https://www.tulaoshi.com/n/20160219/1622295.html

延伸阅读
我们已经熟悉在 ASP 中通过调用 SQL Server 存储过程来执行数据库操作,不过大家是否知道,在桌面级数据库 Access 中,我们也能够创建并使用“存储过程”? Access + ASP 是开发轻量级 Web 应用程序的绝佳组合:简单,快速,兼容性好,但是性能通常不高。并且,用 ADODB.Connection 和 Recordset 对象来执行 SQL 语句的方式,也有一些...
标签: SQLServer
SQL server 的 T-Sql 语言的功能是 非常的强大,但是有个时候 也确实是有些限制和不方便,为什么不象 asp 一样 大量的借用组件呢?开始在 Sql online book 中查找,终于找到了 一个 Sql 的 系统存储过程 sp_OACreate,下面大家就一起去 看看这个 存储过程的神气之处吧 s首先我们先用VB 作一个最简单的组件 ,因为是介绍性的文章,所以这个组件是...
怎样才能将在表A取得的数据插入另一个表B中? (1)对于表A和表B两个表结构完全相同的话〔字段个数,相应字段的类型等等〕,可以使用 insert INTO B select * FROM A; insert INTO B(field1,field2,field3) select A.field1,A.field2,A.field3 from A; (2) 对于两个表如果字段数不一样,但是有几个字段的结构一样时...
SQL_PLUS中 spool ExecCompProc.sql select 'alter procedure 'object_name' compile;' From all_objects where status = 'INVALID' and object_type = 'PROCEDURE'; spool off @ExecCompProc.Sql; 整理成一个存储过程 Create Or Replace Procedure Zl_Compile_Invalid_Procedure As  Strsql V...
存储过程中的TOP后跟一个变量会如何? 代码如下: Create proc getWorkPlan2 (@intCounter int ,@lngUserID int) as select Top 5 lngWorkID,strWorkName,strExecHumanName,strBeginDate from worklist where lngExecHumanID= @lngUserID order by lngWorkID desc 现在想将这里的Top 5 改为变量· Top @intCounter 如下 代码如下...

经验教程

505

收藏

91
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部