我正在使用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:
pdReturnValue
用于存储过程的结果。pdInput
用于存储过程的第一个参数。pdInputOutput
用于存储过程的第二个参数。我对以上列表中的项目3感到惊讶。我本来希望
pdOutput
。问题:为什么此参数是
pdInputOutput
而不是pdOutput
?在什么情况下将参数视为
pdOutput
? 最佳答案
为什么此参数是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中做任何事情。我不知道将参数定义为仅输出的任何方法。
关于sql-server - 当TStoredProcedure参数的方向是pdOutput时?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38910437/