我想使用batch组件来归档表中的一些旧记录。我看过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命令合并?我希望所有检索到的记录都从我的源表移动到目标表。
发布于 2012-06-06 10:35:57
绝对数据库的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;https://stackoverflow.com/questions/10906274
复制相似问题