简体   繁体   中英

SQL Return Auto_increment value from insert

I have a large data set(250,000 rows) that I need to upload to a SQL table. I am currently doing this from a Java application I created, and as expected, it is extremely slow. I have to insert each row from a text file into a table, with an auto_increment column that stores an id for that new row. Now I want to use that same id to insert a modified version of the string I entered to the first table, into a second table, where one of the columns holds that original incremented id, so that I can tie the two rows together. Obviously I can query the string I just inserted, and pull the id, but that is an extra query per row I'm trying to avoid. Is there a way to pull the incremented id value from the sql table to my java class at the time the insert statement is executed?

Thanks,

I guess you are using JDBC so use getGeneratedKeys() of java.sql.PreparedStatement . The returned Resulset contains the auto-generated key.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM