use Text::xSV; my $csv = new Text::xSV; $csv->open_file("foo.csv"); $csv->read_header(); my $SQL = "CREATE TABLE $table_name("; foreach my $field ($csv->get_fields) { $SQL .= $field . " varchar(255),"; } $SQL .= "); # then connect to the database and run the query