my $products_db_dsn = ""; my $products_db_user = ""; my $products_db_password = ""; my $customers_db_dsn = ""; my $customers_db_user = ""; my $customers_db_password = ""; DBIPool::SetLogin( alias => 'products_db', dsn => $products_db_dsn, user => $products_db_user, password => $products_db_password, ); DBIPool::SetLogin( alias => 'customers_db', dsn => $customers_db_dsn, user => $customers_db_user, password => $customers_db_password, ); ProcessAllProducts(); sub ProcessAllProducts { my $dbh = DBIPool::GetHandle('products_db'); my $sql = "select ProductID from Products"; my $sth = $dbh->prepare($sql); if ( $sth->execute ) { while (my $product = $sth->fetchrow_hashref ) { my $productID = $$product{ProductID}; ProcessProductRegions($productID); } } } sub ProcessProductRegions { my $productId = shift; my $dbh = DBIPool::GetHandle('products_db'); my $sql = "select RegionID from ProductRegions where ProductID = ?"; my $sth = $dbh->prepare($sql); if ( $sth->execute($productId) ) { while (my $region = $sth->fetchrow_hashref ) { my $regionID = $$region{RegionID}; ProcessRegionCustomers($regionID); } } } sub ProcessRegionCustomers { my $regionID = shift; my $dbh = DBIPool::GetHandle('customers_db'); my $sql = "select c.* from RegionCustomers rc, Customers c where rc.RegionID = ? and c.CustomerID = rc.CustomerID"; my $sth = $dbh->prepare($sql); if ( $sth->execute($regionID) ) { while (my $customer = $sth->fetchrow_hashref ) { # # do some stuff for the customer here # } } }