【问题标题】:Retrieve output parameter from FireDac stored procedure从 FireDac 存储过程中检索输出参数
【发布时间】:2016-01-07 09:40:50
【问题描述】:

我在 Firebird 数据库中定义了这个存储过程:

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

我使用此代码从 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);

问题在于,当我使用正确的参数(在 Firebird 中检查)调用此代码时,结果始终为空。这段代码有什么问题吗?谢谢

这是可以正常工作的代码:

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);
  • 填写参数后调用Prepare。
  • 在调用prepare后分配参数值。

【问题讨论】:

    标签: delphi stored-procedures firedac


    【解决方案1】:

    来自documentation

    调用 Prepare 后,应用程序无法更改命令参数数据类型和大小。否则,在下一次 Execute / ExecSQL / ExecProc / Open 调用期间,将引发异常。 建议在 Prepare 调用之前设置参数

    在这里您选择不使用

    自动填充参数信息
    SPGeneric.FetchOptions.Items:= SPGeneric.FetchOptions.Items - [fiMeta];
    

    因此,由于您是手动定义参数,因此您应该在调用Prepare之前执行此操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-24
      • 1970-01-01
      • 1970-01-01
      • 2020-08-29
      • 2019-06-27
      • 1970-01-01
      • 1970-01-01
      • 2014-08-19
      相关资源
      最近更新 更多