Retrieve output parameter from FireDac stored procedure

浪子不回头ぞ 提交于 2019-12-12 18:07:18

问题


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.

回答1:


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.



来源:https://stackoverflow.com/questions/34651797/retrieve-output-parameter-from-firedac-stored-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!