my $dbh = DBI->connect("DBI:mysql:database=proyecto;host=localhost", "root", "xyz123", {'RaiseError' => 1}); my $sth = $dbh->prepare("SELECT DISTINCT PAT FROM patentes"); $sth->execute(); while ((my @row) = $sth->fetchrow_array()) { print "$row[0]\n"; #### my $dbh = DBI->connect("DBI:mysql:database=proyecto;host=localhost", "root", "xyz123", {'RaiseError' => 1}); my $sth = $dbh->prepare("SELECT DISTINCT PAT FROM patentes"); $sth->execute(); while ((my @row) = $sth->fetchrow_array()) { print "$row[0]\n"; my $a_dbh = DBI->connect("DBI:mysql:database=proyecto;host=localhost", "root", "xyz123", {'RaiseError' => 1}); my $a_sth = $a_dbh->prepare("SELECT * INTO $row[0] FROM final WHERE pat = $row[0]"); } #### DBD::mysql::st execute failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '0447 from final where pat = 0447' at line 1 at finaltest.pl line 19. DBD::mysql::st execute failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '0447 from final where pat = 0447' at line 1 at finaltest.pl line 19. #### Table : patente Table : Final Pat Name pat dept course 0001 John 0001 SCIFI MATH ADVANCE 0002 Jack 0001 SCIFI Quark study 0003 Peter 0001 SCIFI TIME STUDY 0002 WAR STRATEGIC PHILO. 0002 WAR COUNTERMEASURES 0003 POLITICS DIPLOMATIC RELATIONS 0003 POLITICS ADDRESS STUDY So for pat 0001: I need to create a table contains this: Table : 0001 0001 SCIFI MATH ADVANCE 0001 SCIFI Quark study 0001 SCIFI TIME STUDY and likewise with this Table : 0002 0002 WAR STRATEGIC PHILO. 0002 WAR COUNTERMEASURES and ofcourse Table : 0003 0003 POLITICS DIPLOMATIC RELATIONS 0003 POLITICS ADDRESS STUDY So at the end I have all these tables in the database Patente Final 0001 0002 0003