Search code examples
delphistored-proceduresfiredac

Retrieve output parameter from FireDac stored procedure


I've this Stored Procedure defined within a Firebird Database:

create or alter procedure GET_MSG (
    IDLNG smallint,
    IDMSG integer)
returns (
    MSG varchar(200) character set UTF8)
as
begin
 IF (:IDMSG > 40000) THEN
  BEGIN
   IF (:IDLNG = 1) THEN
    BEGIN
     SELECT NOMBRE01 FROM XMSG2 WHERE ID_XMSG2 = :IDMSG INTO :MSG;
     EXIT;
    END
   IF (:IDLNG = 2) THEN
    BEGIN
     SELECT NOMBRE02 FROM XMSG2 WHERE ID_XMSG2 = :IDMSG INTO :MSG;
     EXIT;
    END
  END ELSE
  BEGIN
   IF (:IDLNG = 1) THEN
    BEGIN
     SELECT NOMBRE01 FROM XMSG WHERE ID_XMSG = :IDMSG INTO :MSG;
     EXIT;
    END
   IF (:IDLNG = 2) THEN
    BEGIN
     SELECT NOMBRE02 FROM XMSG WHERE ID_XMSG = :IDMSG INTO :MSG;
     EXIT;
    END
  END
end

and I use this code to call this Stored Procedure from Firedac :

SPGeneric.StoredProcName:= 'GET_MSG';
SPGeneric.FetchOptions.Items:= SPGeneric.FetchOptions.Items - [fiMeta];
SPGeneric.Prepare;
with SPGeneric.Params do begin
  Clear;
  with Add do begin
    Name:= 'IDLNG';
    ParamType:= ptInput;
    DataType:= ftSmallint;
    Value:= IdLan;
  end;
  with Add do begin
    Name:= 'IDMSG';
    ParamType:= ptInput;
    DataType:= ftInteger;
    Value:= Id;
  end;
  with Add do begin
    Name:= 'MSG';
    ParamType:= ptOutput;
    DataType:= ftString;
    Size:= 200;
  end;
end;
SPGeneric.ExecProc;
result:= VarToStr(SPGeneric.Params[2].Value);

The problem is that when I call this code with correct parameters (checked within Firebird), the result is always null. Is there anything wrong with this code?. Thanks

This is the code that works ok:

SPGeneric.StoredProcName:= 'GET_MSG';
SPGeneric.FetchOptions.Items:= SPGeneric.FetchOptions.Items - [fiMeta];
SPGeneric.Params.Clear;
  with SPGeneric.Params.Add do begin
    Name:= 'IDLNG';
    ParamType:= ptInput;
    DataType:= ftSmallint;
  end;
  with SPGeneric.Params.Add do begin
    Name:= 'IDMSG';
    ParamType:= ptInput;
    DataType:= ftInteger;
  end;
  with SPGeneric.Params.Add do begin
    Name:= 'MSG';
    ParamType:= ptOutput;
    DataType:= ftWideString;
    Size:= 200;
  end;

SPGeneric.Prepare;
SPGeneric.Params[0].Value:= IdLan;
SPGeneric.Params[1].Value:= Id;
SPGeneric.ExecProc;
result:= VarToStr(SPGeneric.Params[2].Value);
  • call Prepare after filling the parameters.
  • assign the parameters values after call prepare.

Solution

  • From the documentation :

    After Prepare is called, the application cannot change command parameter data types and sizes. Otherwise, during the next Execute / ExecSQL / ExecProc / Open call, an exception will be raised. It is recommended to setup parameters before the Prepare call.

    Here you have elected to not autopopulate the parameter information with

    SPGeneric.FetchOptions.Items:= SPGeneric.FetchOptions.Items - [fiMeta];
    

    So, since you are manually defining the parameters you should do this before calling Prepare.