绝对数据库批量移动

时间:2012-06-05 23:15:06

标签: delphi

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

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

对于我打算使用两个datetimepickers的任务。所以查询会像参数一样:

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命令合并?我希望所有检索到的记录从我的源表移动到目标表。

1 个答案:

答案 0 :(得分:3)

绝对数据库的BatchMove似乎是在旧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语句中目标表的名称。

绝对数据库on-line manual

中的更多信息

我没有使用绝对数据库,但是如果他们的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;