use DBI; #connection stuff #prepare your statement handles #assuming the values are unique enough... my $sth_ins = $dbh->prepare("INSERT INTO table_foo(col1,col2,col3) VALUES(?,?,?)"); #after the insert run the query on those same values. my $sth_sel = $dbh->prepare("SELECT id FROM table_foo WHERE col1=?, col2=?, col3=?"); #...later in the code... my $last_id; for(@some_data_of_yours){ $sth_ins->execute($_->{'col1'},$_->{'col2'},$_->{'col3'}); $sth_sel->execute($_->{'col1'},$_->{'col2'},$_->{'col3'}); $sth_sel->bind_columns(\$last_id); $sth_sel->fetch(); #do some stuff with your $last_id... }