我想使用批处理组件在表中归档一些旧记录。我在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命令合并?我希望所有检索到的记录从我的源表移动到目标表。
最佳答案
绝对数据库的BatchMove
似乎是按照旧的BDE TBatchMove
建模的,而旧的BDE TTable
需要两个TQuery
组件。 IIRC,它不适用于BatchMove
,但我可能会记错了。 (已弃用BDE十多年了,自Delphi 1起我就没有使用过。)
不过,您不需要DestTable
。您可以使用单个查询来完成所有操作(为简洁起见,省略了异常处理):
// 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;
在第一个SQL语句中,将替换为目标表的名称。
绝对数据库on-line manual中的更多信息
我没有使用过Absolute Database,但是如果他们的SQL支持包括脚本(我将把研究工作留给您-上面的docs链接)和多个语句,则可以一次完成:
// 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;