从 FireDac 存储过程中检索输出参数
Retrieve output parameter from FireDac stored procedure
我在 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。
- 调用准备后分配参数值。
来自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
.
之前执行此操作
我在 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。
- 调用准备后分配参数值。
来自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
.