gpt4 book ai didi

delphi - 从 FireDac 存储过程检索输出参数

转载 作者:行者123 更新时间:2023-12-02 08:51:30 28 4
gpt4 key购买 nike

我在 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 中检查)时,结果始终为 null。这段代码有什么问题吗?谢谢

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

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。
  • 调用准备后分配参数值。

最佳答案

来自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.

此处您选择不使用

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

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

关于delphi - 从 FireDac 存储过程检索输出参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34651797/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com