use strict; my @to_insert = ( { 'A' => 1, 'B' => 1 }, { 'B' => 2 }, { 'A' => 3 }, ); for my $row (@to_insert) { my $insertStmt; my $cols; my $vals; for my $col (keys %{$row} ) { $cols .= $col . ","; $vals .= "?,"; } $insertStmt = "insert into blah ($cols) values ($vals);"; print "$insertStmt\n"; }