3

我想使用批处理组件来归档表中的一些旧记录。我查看了 Ace components 网站上的示例,但不确定如何使用它。命令是:

DestinationTable.BatchMove(SourceTable,TABSBatchMoveType(bmtAppend));

对于我打算使用两个日期时间选择器的任务。所以查询会像参数一样:

SELECT * from MYTABLE where DATE BETWEEN :a1 and :a2
ABSTQuery1.Parameters.ParamByName ('a1').AsDate := DateTimePicker1.Date;
ABSTQuery1.Parameters.ParamByName ('a2').AsDate := DateTimePicker2.Date;
ABSQuery.ExecSql; 

如何将查询与 batchmove 命令合并?我希望所有检索到的记录都从我的源表移动到目标表。

4

1 回答 1

3

Absolute DatabaseBatchMove似乎是仿照旧的 BDE 建模的TBatchMove,它需要两个TTable组件;IIRC,它不适用于TQuery,但我可能记错了。(BDE 已经被弃用了十多年,从 Delphi 1 开始我就没有使用过它。)

不过,你不需要BatchMove。您可以使用单个查询完成所有操作(为简洁起见,省略了异常处理):

// Copy rows into destination
ABSTQuery1.SQL.Text := 'INSERT INTO DestTable'#32 +
  '(SELECT * from MYTABLE where DATE BETWEEN :a1 and :a2)';
ABSTQuery1.Parameters.ParamByName ('a1').AsDate := DateTimePicker1.Date;
ABSTQuery1.Parameters.ParamByName ('a2').AsDate := DateTimePicker2.Date;
ABSTQuery1.ExecSql; 
ABSTQuery1.Close;

// Remove them from source (you said "move", after all)
ABSTQuery1.SQL.Text := 'DELETE FROM MyTable'#32 +
  `WHERE Date BETWEEN :a1 and :a2';
ABSTQuery1.Parameters.ParamByName ('a1').AsDate := DateTimePicker1.Date;
ABSTQuery1.Parameters.ParamByName ('a2').AsDate := DateTimePicker2.Date;
ABSTQuery1.ExecSql; 
ABSTQuery1.Close;

替换DestTable为第一个 SQL 语句中的目标表的名称。

Absolute Database在线手册中的更多信息

我没有使用过绝对数据库,但如果他们的 SQL 支持包括脚本(我将把研究留给你 - 上面的文档链接)和多个语句,你可以一次性完成:

// Note addition of `;` at end of each SQL statement
// and change in param names for second statement.
// Some DBs will allow you to just use one pair, and
// set the value for each once. Some require setting
// each twice, and some require unique param names.
// Check the documentation for Absolute DB.
//
ABSTQuery1.SQL.Text := 'INSERT INTO DestTable'#32 +
  '(SELECT * from MYTABLE where DATE BETWEEN :a1 and :a2);'
  'DELETE FROM MyTable WHERE Date BETWEEN :d1 and :d2;';
ABSTQuery1.Parameters.ParamByName ('a1').AsDate := DateTimePicker1.Date;
ABSTQuery1.Parameters.ParamByName ('a2').AsDate := DateTimePicker2.Date;

// New param names for second pass
ABSTQuery1.Parameters.ParamByName ('d1').AsDate := DateTimePicker1.Date;
ABSTQuery1.Parameters.ParamByName ('d2').AsDate := DateTimePicker2.Date;
ABSTQuery1.ExecSQL;
ABSTQuery1.Close;
于 2012-06-06T02:35:56.630 回答