当TStoredProcedure参数的方向是pdOutput时?

时间:2016-08-12 05:19:10

标签: sql-server delphi stored-procedures ado delphi-xe8

我正在使用Delphi XE8和SQL Server 2014。

考虑以下存储过程:

CREATE PROCEDURE [dbo].[usp2_BaseShow_By_Id](
    @ShowId int,
    @Result int OUT
)

我使用这个Delphi代码来枚举其参数:

function TDBHelper<T>.OpenSingle(ProcedureName:string; Args: TArray<Variant>;
  Transform: TTransformFunc<T>): TDBResult<T>;
var
  Con:TADOConnection;
  Proc:TADOStoredProc;
  ArgIndex,ParamIndex: Integer;
begin
  Result:=TDBResult<T>.Create;
  Con:=GetConnection;
  Proc:=TADOStoredProc.Create(nil);
  try
    try
      Proc.Connection:=Con;
      Proc.ProcedureName:=ProcedureName;
      Proc.Parameters.Clear;
      Proc.Parameters.Refresh;
      ArgIndex:=0;
      for ParamIndex := 0 to Proc.Parameters.Count-1 do begin
        if(Proc.Parameters[ParamIndex].Direction in [pdInput,pdInputOutput])then begin
          Proc.Parameters[ParamIndex].Value:=Args[ArgIndex];
          Inc(ArgIndex);
        end;
      end;
      Proc.Open;
      Result.Data:=Transform(Proc);
      Result.Success:=True;
      Proc.Close;
      Con.Close;
    except
      on E:Exception do begin
        Result.Success:=False;
        Result.E:=E;
      end;
    end;
  finally
    Proc.Free;
    Con.Free;
  end;
end;

这告诉我参数具有以下parameter direction

  1. pdReturnValue获取存储过程的结果。
  2. pdInput获取存储过程的第一个参数。
  3. pdInputOutput表示存储过程的第二个参数。
  4. 我对上面列表中的第3项感到惊讶。我原以为pdOutput。问题:

    • 为什么此参数pdInputOutput而不是pdOutput
    • 在什么情况下参数会被视为pdOutput

1 个答案:

答案 0 :(得分:1)

  

为什么此参数pdInputOutput而不是pdOutput

因为尽管OUT关键字,该参数仍用于输入和输出。请尝试以下方法:

CREATE PROC DoubleIt (@Value int OUT)
AS
SET @Value = @Value * 2
GO

DECLARE @Val int = 3
exec DoubleIt @Val OUTPUT
PRINT @Val

您会注意到@Value用作输入和输出。

  

在什么情况下参数会被视为pdOutput

您可以手动创建参数。不幸的是,我现在无法轻易测试这一点。还有一些其他数据库平台可以支持它。

  

在SQL Server的存储过程中,如何在Delphi中使用pdOutput参数?

我认为你无法在SQL Server中做任何事情。我不知道有任何方法可以将参数定义为仅输出