sub importPublisher { my $file = shift(); my $time_started = time(); Log "\nImporting Publishers"; my ($new,$updated,$incorrect) = (0,0,0); my $PublisherId; my $check = $db->prepare('SELECT PublisherID FROM Publishers WHERE MediaNumber = ? and CountryID = (SELECT CountryID FROM Country WHERE Cname_advision = ?)'); db_error("compiling check Media number",$db) if !$check; my $insert = $db->prepare('EXEC InsertPublisher ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?'); db_error("compiling InsertPublisher",$db) if !$insert; my $update = $db->prepare('EXEC UpdatePublisher ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?'); db_error("compiling UpdatePublisher",$db) if !$update; my @fields = qw(publisher_name media_number address city state zip phone contact fax email_id notes country); fixXML($file); my $reader = new XML::RAX; $reader->openfile($file) or die "Can't open $file: $!\n"; $reader->setRecord('PublisherData'); while (my $rec = $reader->readRecord()) { foreach (values %{$rec->{fields}}) {s/\n/ /g;s/^\s+//;s/\s+$//;convText;undef $_ if $_ eq 'NULL'}; $rec->{fields}->{zip} =~ tr/-//d; unless (testFields($rec->{fields},$fields,"Publisher name '$rec->{fields}->{publisher_name}'")) { $incorrect++; next; } my $res = $check->execute($rec->{fields}->{media_number}, $rec->{fields}->{country}); if (!$res) { db_error("checking for Publisher '$rec->{fields}->{publisher_name}', '$rec->{fields}->{country}'",$db,1); } elsif (abs($res) == 1) { $check->finish(); if ($update->execute(@{$rec->{fields}}{@fields})) { $updated++; } else { db_error("updating Publisher '$rec->{fields}->{publisher_name}'",$db,1); $incorrect++; } } else { $check->finish(); if ($insert->execute(@{$rec->{fields}}{@fields})) { $new++; } else { db_error("inserting Publisher '$rec->{fields}->{publisher_name}'",$db,1); $incorrect++; } } } LogNT "Imported $file with $new new, $updated updated and $incorrect incorrect records."; my $time_run = time() - $time_started; Log( sprintf "\tThe import took %02d:%02d:%02d HH:MM:SS",int($time_run/3600),int($time_run/60) % 60, $time_run % 60); } #### CREATE PROCEDURE InsertPublisherDetails( @publishername NAME, @medianumber varchar(50), @address ADDRESS, @city NAME, @state NAME, @zip ZIP, @phone PHONE, @contact NAME, @fax PHONE, @emailid EMAIL, @notes NOTES, @cname_advision varchar(2) ) AS BEGIN SET NOCOUNT ON -- this is what I had to add Declare @Country JdtID; SELECT @Country = CountryID FROM Country WHERE cname_advision = @cname_advision if @Country is NULL BEGIN raiserror('Cannot find the Country ''%s''!',11,1,@cname_advision); RETURN; END INSERT INTO Publishers ( publishername, medianumber, address, city, state, zip, phone, contact, fax, emailid, notes ,CountryID ) values ( @publishername, @medianumber, @address, @city, @state, @zip, @phone, @contact, @fax, @emailid, @notes, @Country ) RETURN @@identity END