How can I call an Oracle function from Delphi?
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;
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.
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...:
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 packages that 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.
we run an Oracle stored procedure using this code that utilizes the BDE (I know please don't bash because we used the 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