my @line; foreach (my $top = $top_sth->fetchrow_hashref) { $line[0]= $top->{guid}; push @line,$top->{title}; $exchange->stdxls_write($exchange->stdxls_row(), \@line); print STDERR "Level1 ".$top->{title}."\n"; child_levels($top->{asset_id},@line); } $workbook->close(); sub child_levels { my ($asset_id,@line) = @_; my $child_sth = $dbh->prepare(' SELECT asset_id ,title ,seq ,guid ,parent_id FROM asset WHERE parent_id = ? ORDER BY seq ') or die $dbh->errstr; $child_sth->execute($asset_id) or die $child_sth->errstr; foreach (my $child = $child_sth->fetchrow_hashref) { $line[0]= $child->{guid}; push @line,$child->{title}; $exchange->stdxls_write($exchange->stdxls_row(), \@line); child_levels($child->{asset_id},@line); }