Delphi 调用 Oracle 存储过程并返回数据集的例子.
一、先在 Oracle 建好
CREATE OR REPLACE PACKAGE pkg_test
AS
TYPE myrctype IS REF CURSOR;
PROCEDURE get(i_test INTEGER,p_rc OUT myrctype);
END pkg_test;
CREATE OR REPLACE PACKAGE BODY pkg_test
AS
PROCEDURE get(i_test INTEGER,p_rc OUT myrctype) IS
BEGIN
IF i_test = 0 THEN
OPEN p_rc FOR SELECT SYSDATE FROM dual;
ELSE
OPEN p_rc FOR SELECT * FROM tab;
END IF;
END get;
END pkg_test;
二、用 Delphi 调用
建一个窗体,拖动控件 AdoConnection1 , ADOStoredProc1 和 Button1.
procedure TForm1.Button1Click(Sender: TObject);
begin
try
with ADOConnection1 do
begin
ConnectionString:=
'Provider=OraOLEDB.Oracle.1;'
+ 'Password=密码;'
+ 'Persist Security Info=True;'
+ 'User ID=用户名;'
+ 'Data Source=数据库名;'
+ 'Extended Properties="PLSQLRSet=1;"';
Open;
end;
except
showMessage('连接不成功');
exit;
end;
try
with ADOStoredProc1 do
begin
Connection := ADOConnection1;
Parameters.Clear;
ProcedureName:= 'pkg_test.get';
Parameters.CreateParameter('p1',ftInteger,pdInput,10,1);
Open;
end;
except
showMessage('无法执行过程.');
end;
end;
一、先在 Oracle 建好
CREATE OR REPLACE PACKAGE pkg_test
AS
TYPE myrctype IS REF CURSOR;
PROCEDURE get(i_test INTEGER,p_rc OUT myrctype);
END pkg_test;
CREATE OR REPLACE PACKAGE BODY pkg_test
AS
PROCEDURE get(i_test INTEGER,p_rc OUT myrctype) IS
BEGIN
IF i_test = 0 THEN
OPEN p_rc FOR SELECT SYSDATE FROM dual;
ELSE
OPEN p_rc FOR SELECT * FROM tab;
END IF;
END get;
END pkg_test;
二、用 Delphi 调用
建一个窗体,拖动控件 AdoConnection1 , ADOStoredProc1 和 Button1.
procedure TForm1.Button1Click(Sender: TObject);
begin
try
with ADOConnection1 do
begin
ConnectionString:=
'Provider=OraOLEDB.Oracle.1;'
+ 'Password=密码;'
+ 'Persist Security Info=True;'
+ 'User ID=用户名;'
+ 'Data Source=数据库名;'
+ 'Extended Properties="PLSQLRSet=1;"';
Open;
end;
except
showMessage('连接不成功');
exit;
end;
try
with ADOStoredProc1 do
begin
Connection := ADOConnection1;
Parameters.Clear;
ProcedureName:= 'pkg_test.get';
Parameters.CreateParameter('p1',ftInteger,pdInput,10,1);
Open;
end;
except
showMessage('无法执行过程.');
end;
end;