一、使用存儲過程返回數(shù)據(jù)集
Oracle中存儲過程返回數(shù)據(jù)集是通過ref cursor類型數(shù)據(jù)的參數(shù)返回的,而返回數(shù)據(jù)的參數(shù)應該是out或in out類型的。
由于在定義存儲過程時無法直接指定參數(shù)的數(shù)據(jù)類型為:ref cursor,而是首先通過以下方法將ref cursor進行了重定義:
create or replace package FuxjPackage is
type FuxjResultSet is ref cursor;
--還可以定義其他內(nèi)容
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中調(diào)用返回數(shù)據(jù)集的存儲過程
可以通過TstoredProc或TQuery控件來調(diào)用執(zhí)行返回數(shù)據(jù)集的存儲,數(shù)據(jù)集通過TstoredProc或TQuery控件的參數(shù)返回,注意參數(shù)的DataType類型為ftCursor,而參數(shù)的ParamType類型為ptInputOutput。
使用TstoredProc執(zhí)行UpdatefuxjExample的相關(guān)設置為:
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
執(zhí)行方法為:
StoredProc1.Params.Items[0].AsString:=Edit1.Text; //給參數(shù)賦值;
StoredProc1.Params.Items[1].AsString:=Edit2.Text; //給參數(shù)賦值;
StoredProc1.Active:=False;
StoredProc1.Active:=True; //返回結(jié)果集
使用TQuery執(zhí)行InsertfuxjExample的相關(guān)設置為:
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
執(zhí)行方法為:
Query1.Params.Items[0].AsString:=Edit3.Text; //給參數(shù)賦值;
Query1.Params.Items[1].AsString:=Edit4.Text; //給參數(shù)賦值;
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中調(diào)用返回數(shù)據(jù)集的存儲過程
可以通過TstoredProc或TQuery控件來調(diào)用執(zhí)行返回數(shù)據(jù)集的存儲,數(shù)據(jù)集通過TstoredProc或TQuery控件的參數(shù)返回,注意參數(shù)的DataType類型為ftCursor,而參數(shù)的ParamType類型為ptInputOutput。
使用TstoredProc執(zhí)行UpdatefuxjExample的相關(guān)設置為:
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
執(zhí)行方法為:
StoredProc1.Params.Items[0].AsString:=Edit1.Text; //給參數(shù)賦值;
StoredProc1.Params.Items[1].AsString:=Edit2.Text; //給參數(shù)賦值;
StoredProc1.Active:=False;
StoredProc1.Active:=True; //返回結(jié)果集
使用TQuery執(zhí)行InsertfuxjExample的相關(guān)設置為:
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
執(zhí)行方法為:
Query1.Params.Items[0].AsString:=Edit3.Text; //給參數(shù)賦值;
Query1.Params.Items[1].AsString:=Edit4.Text; //給參數(shù)賦值;
Query1.Active:=False;
Query1.Active:=True;
附:創(chuàng)建返回數(shù)據(jù)集的存儲過程 簡單框架
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;
新聞熱點
疑難解答
圖片精選