4

私のストアド プロシージャは、1 つの出力パラメータを取り、レコードセットを返します。

CREATE PROCEDURE MyProc
    @EntityCode BIGINT, @ResValue INT OUTPUT
AS
BEGIN
  SELECT .... WHERE Code=@EntityCode
  SET @ResValue = ...
END

出力パラメーターとレコードセットの両方の値を受け取る必要があります。だから私はこれを行います:

function GetData(const EntityCode: Int64): Integer;
var
  Proc: TADOStoredProc;
  PEntityCode: ADODB.TParameter;
  PResValue: ADODB.TParameter;
begin
  Proc := TADOStoredProc.Create(nil);
  try
    Proc.Connection := ADOConnection1;
    Proc.CursorLocation := clUseServer;
    Proc.ProcedureName := 'MyProc';

    PEntityCode := Proc.Parameters.AddParameter;
    PEntityCode.Name := '@EntityCode';
    PEntityCode.DataType := ftLargeint;
    PEntityCode.Value := EntityCode;

    PResValue := Proc.Parameters.AddParameter;
    PResValue.Name := '@ResValue';
    PResValue.DataType := ftInteger;
    PResValue.Direction := pdOutput;

    //Proc.Prepared := True;
    Proc.Open;
    Result := PResValue.Value;
    while not Proc.Eof do
    begin
      Proc.Next;
    end;
  finally
    Proc.Free;
  end;
end;

レコードセットは空ではありませんが、PResValue.Value は 0 です。Proc.ExecProc を呼び出すと、レコードセットは空ですが、PResValue.Value が割り当てられます。レコードセットと出力パラメーターの値の両方を受け取ることは可能ですか?

MyProc のレコードセットが 1 つのレコードのみで構成されている場合、OUTPUT パラメータの値が割り当てられることがわかりました。どういう意味ですか?

ありがとう!

4

1 に答える 1

5

The solution is to close recordset before accessing the output variables:

Proc.Open;
while not Proc.Eof do
begin
  Proc.Next;
end;
Proc.Close;
Result := PResValue.Value;
于 2013-01-16T15:32:33.047 に答える