zoukankan      html  css  js  c++  java
  • firedac的TFDStoredProc动态创建并调用存储过程

    1)中间件执行存储过程

    sp.Close;
    sp.StoredProcName := procName;
    sp.Prepare;  // 生成存储过程的参数列表,无任何OUTPUT的存储过程,也会自动生成一个@ReturnValue的返回值参数
    params := TFDParams.Create;
    try
      UnpackFDParams(inParams, params);  // 还原客户端上传的参数
      for i:=0 to params.Count -1 do
        for h:=0 to d.procOpen.ParamCount-1 do
          if sp.Params[h].Name = params[i].Name then
          begin
             sp.Params[h].Value := params[i].Value;  // 给存储过程的参数赋值
             Break;
          end;
    finally
      params.Free;
    end;
    sp.open;
    2)客户端调用远程方法执行存储过程

    procedure TfrmMain.Button2Click(Sender: TObject);
    begin
      cds2.Params.Clear;
      cds2.Params.CreateParam(ftString,'@workno',ptInput).Value :='1';  // 参数前面记得要加@
      cds2.Data := ProcOpen('9999','3',PackageParams(cds2.Params));
    end;

    3)带OUTPUT的存储过程

    function TServerMethods1.spOpenOut(const accountNo, spName: WideString; inParams: OleVariant): OleVariant;
    var
    d: TfrmDB;
    params: TFDParams;
    i: Integer;
    param: TFDParam;
    begin
    Result := null;
    if (accountNo = '') or (spName = '') then
    Exit;
    d := GetDBPool(accountNo).Lock;
    if not Assigned(d) then
    Exit;
    try
    try
    d.procOpen.Close;
    d.procOpen.StoredProcName := spName;
    d.procOpen.Prepare;
    params := TFDParams.Create;
    try
    UnpackFDParams(inParams, params);
    for i := 0 to params.Count - 1 do
    begin
    param := d.procOpen.FindParam(params[i].Name);
    if not Assigned(param) then
    Continue;
    param.value := params[i].value;
    end;
    finally
    params.Free;
    end;

    Result := VarArrayCreate([0, 1], varVariant);
    Result[0] := d.dspProcOpen.Data; // 查询数据集
    Result[1] := PackageFDParams(d.procOpen.params); // OUTPUT参数

    except
    on e: Exception do
    begin
    Result := null;
    Log.WriteLog('TServerMethods1.spOpenOut ' + e.Message);
    Exit;
    end;
    end;
    finally
    d.procOpen.Close;
    GetDBPool(accountNo).Unlock(d);
    end;
    end;

  • 相关阅读:
    UVa 1643 Angle and Squares
    UVa 1210 (高效算法设计) Sum of Consecutive Prime Numbers
    UVa 1213 (01背包变形) Sum of Different Primes
    UVa 1644 (筛素数 + 二分) Prime Gap
    UVa 10048 (Floyd变形) Audiophobia
    UVa 247 (传递闭包) Calling Circles
    UVa 808 (建坐标系、找规律) Bee Breeding
    UVa 1151 (枚举 + MST) Buy or Build
    UVa 1395 (最小生成树) Slim Span
    UVa 11040 (水题) Add bricks in the wall
  • 原文地址:https://www.cnblogs.com/hnxxcxg/p/4917335.html
Copyright © 2011-2022 走看看