Batch Insert: How to get id's of inserted records?

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
tommy
Posts: 11
Joined: Thu 21 Jul 2016 21:52

Batch Insert: How to get id's of inserted records?

Post by tommy » Mon 17 Jul 2017 14:43

For performance, I'd like to use either TUniLoader or a batch operation as described in 4.9 of the manual (array parameters).

However, for each record inserted I need its autoincrement ID as it will be used for reference in another table. Currently I iterate over setting parameters, executing and memorizing the LastInsertID which is not really performant.

How can I get the IDs of the inserted records with either TUniLoader or after an Execute(1000) statement?

Kinds regards,
Thomas

MaximG
Devart Team
Posts: 1822
Joined: Mon 06 Jul 2015 11:34

Re: Batch Insert: How to get id's of inserted records?

Post by MaximG » Tue 18 Jul 2017 12:20

Please specify what database you select when using our product. What do you think what result the Execute(1000) method should return?

tommy
Posts: 11
Joined: Thu 21 Jul 2016 21:52

Re: Batch Insert: How to get id's of inserted records?

Post by tommy » Tue 18 Jul 2017 17:35

Currently, I'm using an sqlite database, but I think this also applies to any other provider supporting the lastinsertid property.

After running Execute(1000) (or whatever number), I would expect to gain access to an array of all inserted IDs, analog to the ParamByName('xxx')[n] array which provides the data to be inserted. In other words, when using the Batch function, LastInsertID should be an array of INT64 instead of just an INT64.

MaximG
Devart Team
Posts: 1822
Joined: Mon 06 Jul 2015 11:34

Re: Batch Insert: How to get id's of inserted records?

Post by MaximG » Fri 21 Jul 2017 09:43

Unfortunately, in this question, we are limited by the possibilities of SQLite, which provides only the last_insert_rowid function (http://www.sqlite.org/c3ref/last_insert_rowid.html) to return values of interest to you. This function allows you to return a value for only one inserted record. If there is the necessary support from the server in the database (for example, Oracle DB), then we return the autoincrement values as an array (this is the behavior that is implemented in our ODAC product).

tommy
Posts: 11
Joined: Thu 21 Jul 2016 21:52

Re: Batch Insert: How to get id's of inserted records?

Post by tommy » Tue 25 Jul 2017 15:36

Ok, I understand. Thanks for your explanation.

MaximG
Devart Team
Posts: 1822
Joined: Mon 06 Jul 2015 11:34

Re: Batch Insert: How to get id's of inserted records?

Post by MaximG » Thu 27 Jul 2017 12:07

Thank you for your interest to our products. Feel free to contact us if you have any further
questions about UniDAC.

Post Reply