一、使用存储过程返回数据集
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;
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;
附:创建返回数据集的存储过程 简单框架
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;