如何从 Delphi 调用 Oracle 函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5127669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I call an Oracle function from Delphi?
提问by maher
I created a function in Oracle that inserts records in specific tables and return an output according to what occurs within the function. e.g (ins_rec return number)
我在 Oracle 中创建了一个函数,该函数在特定表中插入记录并根据函数内发生的情况返回输出。例如(ins_rec 返回编号)
How do I call this function and see its output in Delphi?
如何调用此函数并在 Delphi 中查看其输出?
I got a reply (with all my thanks) for sql plus but I need to know how can I do this in Delphi
我得到了 sql plus 的回复(非常感谢),但我需要知道如何在 Delphi 中做到这一点
回答by Bharat
Just pass the user defined function as column name in the query and it will work.
只需将用户定义的函数作为查询中的列名传递,它就会起作用。
Example:
例子:
Var
RetValue: Integer;
begin
Query1.Clear;
Query1.Sql.Text := 'Select MyFunction(Param1) FunRetValue from dual';
Query1.Open;
if not Query1.Eof then
begin
RetValue := Query1.FieldByName('FunRetValue').AsInteger;
end;
end;
回答by Bharat
How to accomplish it may depend on what DB access library you use (BDE? dbExpress? ADO? others), some may offer a "stored procedure" component that may work with functions as well.
如何完成它可能取决于您使用的 DB 访问库(BDE?dbExpress?ADO?其他),有些可能提供一个“存储过程”组件,也可以与函数一起使用。
A general approach it to use an anonymous PL/SQL block to call the function (and a parameter to read the return value), PL/SQL resembles Pascal a lot...:
使用匿名 PL/SQL 块调用函数(以及读取返回值的参数)的一般方法,PL/SQL 与 Pascal 非常相似……:
Qry.SQL.Clear;
Qry.SQL.Add('BEGIN');
Qry.SQL.Add(' :Rez := ins_rec;');
Qry.SQL.Add('END;');
// Set the parameter type here...
...
Qry.ExecSQL;
...
ReturnValue := Qry.ParamByName('Rez').Value;
I would not have used a function, though, but a stored procedure with an OUT value. Moreover, Oracle offers packagesthat are a very nice way to organize procedure and functions, and they also offer useful features like session variables and initialization/finalization sections... very much alike a Delphi unit.
不过,我不会使用函数,而是使用带有 OUT 值的存储过程。此外,Oracle 提供的包是组织过程和函数的一种非常好的方式,它们还提供有用的功能,如会话变量和初始化/结束部分……非常类似于 Delphi 单元。
回答by Leslie
we run an Oracle stored procedure using this code that utilizes the BDE (I know please don't bash because we used the BDE!)
我们使用使用 BDE 的代码运行 Oracle 存储过程(我知道请不要抨击,因为我们使用了 BDE!)
Try
DMod.Database1.Connected:= False;
DMod.Database1.Connected:= True;
with DMod.StoredProc1 do
begin
Active:= False;
ParamByName('V_CASE_IN').AsString:= Trim(strCaseNum);
ParamByName('V_WARRANT_IN').AsString:= strWarrantNum;
ParamByName('V_METRO_COMMENTS').AsString:= strComment;
Prepare;
ExecProc;
Result:= ParamByName('Result').AsString;
end;
Except