my $sql; foreach my $table (sort keys %$config) { my $sql = qq{ insert into $table ( id, balance ) values ( ?, ? ) }; my $sth = $dbh->prepare( $sql ); while (my ($col, $val) = each %{$config->{$table}}) { $sth->execute( $col, $val ); } }