代码之家  ›  专栏  ›  技术社区  ›  Maya

如何使用FireDAC提取存储过程DDL

  •  2
  • Maya  · 技术社区  · 8 年前

    FireDAC有从数据库中提取存储过程DDL的方法吗?

    例如,看起来像:

    SET TERM ^ ;
    
    CREATE PROCEDURE MY_PROC RETURNS (aParam INTEGER) AS
    BEGIN
      aParam = 10;
    END^
    
    1 回复  |  直到 8 年前
        1
  •  3
  •   Victoria zac    8 年前

    FireDAC不支持(统一)获取存储过程的DDL定义(此时)。所以,你需要从 RDB$PROCEDURES 桌子,

    uses
      FireDAC.Stan.Util;
    
    type
      TFDConnectionHelper = class helper for TFDConnection
      public
        function GetStoredProcCode(const AName: string): string;
      end;
    
    implementation
    
    { TFDConnectionHelper }
    
    function TFDConnectionHelper.GetStoredProcCode(const AName: string): string;
    var
      Table: TFDDatSTable;
      Command: IFDPhysCommand;
    begin
      CheckActive;
      if RDBMSKind <> TFDRDBMSKinds.Firebird then
        raise ENotSupportedException.Create('This feature is supported only for Firebird');
    
      Result := '';
      ConnectionIntf.CreateCommand(Command);
    
      Command.CommandText := 'SELECT RDB$PROCEDURE_SOURCE FROM RDB$PROCEDURES WHERE RDB$PROCEDURE_NAME = :Name';
      Command.Params[0].DataType := ftString;
      Command.Params[0].Size := 31;
      Command.Params[0].AsString := UpperCase(AName);
    
      Table := TFDDatSTable.Create;
      try
        Command.Define(Table);
        Command.Open;
        Command.Fetch(Table);
    
        if Table.Rows.Count > 0 then
          Result := Table.Rows[0].GetData(0);
      finally
        FDFree(Table);
      end;
    end;
    

    然后使用(当您连接到Firebird DBMS时):

    procedure TForm1.Button1Click(Sender: TObject);
    var
      S: string;
    begin
      S := FDConnection1.GetStoredProcCode('MyProcedure');
      ...
    end;
    
    推荐文章