Great Perl Monks,
after using this site as a valued reference for the last few months I finally have a question! Please excuse my question if it is silly; I've only been using Perl for six months.
I'm using the FreeTDS ODBC driver and DBI to connect to a Microsoft SQL Server 2000 database. When I attempt to insert an undefined value - which from everything I've read here and elsewhere should insert a NULL value into the database - I get the literal string 'null'. I've searched far and wide during the last few days and either I'm the only person who's ever had this problem or I'm very bad at querying the Google.
My code works as follows: I open a pipe delimited text file, split the line into an array, cycle through the array and set each blank element to undef, using the normal DBI prepare & execute I insert the data into the database.
Code that cycles through the text file and inserts into the database.
while (!eof(INPUT)) { $record = <INPUT>; my @records = split(/\|/, $record); for (my $i=0; $i<scalar(@records); $i++) { if ($records[$i] eq '' || $records[$i] eq "\n") {$records[$i] = +undef;} #Setting blank fields to undefined else {$records[$i] =~ s/\n//g;} #Clearing endline characters } $DB->doQuery($report->{insertSQL}, $reportDate, @records); }
Below is the method from my class which handles the DBI calls. (Note I've also tried this with without using my class, just straight DBI, and I have the same issue.)
sub doQuery { my $self = shift; my $sql = shift; my @values = @_; #Running the new query eval{ my $sth = $self->{"connection"}->prepare($sql); $sth->execute(@values); $sth->finish(); }; #If there are errors if ($@) { warn "Error!"; } return $self; }
Below is an example of the SQL I'm using.
INSERT INTO QID.dbo.ReportT52 (report_date, insert_date, loan_num, loa +n_id, date_paid, payment_num, escrow, principal, interest, service_fe +e, net_interest, deposit_remit, balance, late_chg, other_trust, pi_co +nstant, ann_ir, sf_rate, due_date) VALUES (?, GETDATE(), ?, ?, ?, ?, +?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
I hope I've provided everything you might need. Any assistance anyone is able to provide would be much appreciated.
Thanks!
In reply to DBI Inserting undef as literal string 'null' by marmanold
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |