Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Invalid Precision error

by Anonymous Monk
on Apr 28, 2010 at 04:27 UTC ( [id://837219]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks i am getting invalid precision error while executing the following script

Is there any solution

use strict; use File::Copy; use Cwd; use DBI; #use DBD::Oracle; # Oracle use DBD::ODBC; use Time::Local; use warnings; use Carp; my $dbh; my %hdr_field_vars = ( tran_seq_nbr=>[167,"N12_0"], org_number=>[28,"I"], store_number=>[167,"I"], tran_date=>['2006-12-08 00:00:00',"D"], terminal_number=>[2, "I"], tran_number=>[801,"I"], # tran_time=>['2006-12-08 15:41:00',"D"], # tran_type=>['02',"V4"], sub_tran_type=>['C',"V4"], clerk_code=>['116704444',"V15"], tran_taxable_amount=>[-35.10,"N12_2"], tran_tax_total=>[0,"N12_2"], tran_gross_total=>[-35.10,"N12_2"], coup_total=>[3.90,"N9_2"], mkd_total=>[0,"N9_2"], employee_number=>['NULL',"V15"], tran_alttax_total=>[0,"N12_2"], sale_amount=>[0, "N9_2"], return_amount=>[39.00, "N9_2"], tender_code=>['01',"V4"], split_tender=>['N',"V2"], rtn_with_rcpt=>['N',"V2"], pv_time_lapse=>[0,"I"], orig_store_nbr=>[0,"I"], #orig_tran_date=>['',"D"], orig_term_nbr=>[0,"I"], orig_tran_nbr=>[0,"I"], orig_tender_code=>['00',"V4"], prev_tran_type=>['NULL',"V4"], prev_sub_tran_type=>['NULL',"V4"], phone_no=>[0,"N11_0"] # kcpos_kth_user_403=>['2010-04-27 11:05:51',"D"] ); my $JBM; open( $JBM, ">>C:\\Installer\\ETL\\SQL\\output.txt"); # Correction $dbh = DBI->connect( "DBI:ODBC:Navi_s7200_IndiaCOE", "sa", "ABCabc123", { RaiseError => 1, AutoCommit => 0, PrintError => 0 } ) or die "Couldn't connect to database: $DBI::errstr"; $dbh->{odbc_ignore_named_placeholders} = 1; # $dbh->{odbc_default_bind_type}=1; print $JBM "connected\n"; print $JBM $dbh."\n"; my $first_key = 1; my $field_var_hash_size; my $tail = ") VALUES (" ; ################################# # Creating the insertion statement ################################# my $HEADER = "INSERT INTO kcpos_tran_header("; foreach my $key (keys %hdr_field_vars) { if ( !$first_key ) { $HEADER .= ","; $tail .= ","; } $tail .= prepareSQLDate( $hdr_field_vars{$key}[1]) ; $HEADER .= "$key"; $first_key = 0; } $HEADER .= $tail . ")"; print $JBM $HEADER."\n"; my $sth_header = $dbh->prepare( $HEADER ) or die "Couldn't prepare statement: " . $dbh->errstr; print $JBM $sth_header."\n"; my $counter = 1; foreach my $key (keys %hdr_field_vars) { my $data_ref = $hdr_field_vars{$key}[0]; my $form=$hdr_field_vars{$key}[1]; my $data; if(($form=~/^V1$/ || $form=~/[DC]/ || $form=~/^V4$/ || $form=~ +/^V2$/ || $form=~/^V15$/) && defined($data_ref)){ if($data_ref ne 'NULL'){ $data = "'".$data_ref."'"; } else { $data = $data_ref; } } else{ $data = $data_ref; } print $JBM $data."\n"; # use Data::Dumper; # print $JBM "<--------------------"; # print $JBM Dumper($data); # print $JBM "-------------------->\n"; $sth_header->bind_param( $counter, $data); $counter++; } print $JBM $HEADER; $sth_header->execute(); $sth_header->finish(); $dbh->commit; $dbh->disconnect; ############################################ # Function: prepareSQLDate ############################################ # add the convert function to the insert statements data type Date sub prepareSQLDate { my $format = $_[0]; # if(defined($format)|| !defined($format)) # { chomp($format); if ( $format =~ /D/ ) { return ( " CAST(?,datetime,120)"); } elsif ($format =~ /C/) { return ( " CAST(?,datetime,112)"); } else{ return (" ? "); } # } # else { # return (" ? "); # } }

Thanks

Replies are listed 'Best First'.
Re: Invalid Precision error
by ikegami (Patriarch) on Apr 28, 2010 at 05:18 UTC
    Three things I've spotted
    • $data = "'".$data_ref."'"; invalidates your dates for sure, probably everything else too. Instead of passing a date, you're passing an SQL string literal of a date. Just pass the date. That's the whole point of placeholders. Well ok, one major point of placeholders.

    • You call keys %hdr_field_vars twice, expecting the keys to be returned in the same order both times. I think it actually will do so in this very specific program, but it's a very fragile assumption. You'd do well to avoid that assumption. (Save the result of keys %hdr_field_vars into an array, and iterate over the array.)

    • DBI might be guessing the type of your data incorrectly. (number instead of string, string instead of number) You might need to tell it the proper type (via bind_param).

      i did this and executed then even though the same problem

      <code> my $counter = 1; foreach my $key (keys %hdr_field_vars) { my $data_ref = $hdr_field_vars{$key}[0]; # my $form=$hdr_field_vars{$key}1; my $data; # if(($form=~/^V1$/ || $form=~/^V4$/ || $form=~/^V2$/ || $form=~/^V15$/) && defined($data_ref)){ # if($data_ref ne 'NULL'){ # $data = "'".$data_ref."'"; # } # else # { $data = $data_ref; # } # } # else{ # $data = $data_ref; # } print $JBM $data."\n"; # use Data::Dumper; # print $JBM "<--------------------"; # print $JBM Dumper($data); # print $JBM "-------------------->\n"; $sth_header->bind_param( $counter, $data); $counter++; }

      ok about second point i know that, the hash will return the values in random order

      for date its giving error

Re: Invalid Precision error
by GrandFather (Saint) on Apr 28, 2010 at 05:01 UTC

    Show us the actual error text, or even better provide a stand alone sample script the reproduces the error (I realise this may not be possible given the database involvement).

    True laziness is hard work

      DBD::ODBC::st execute failed: MicrosoftODBC SQL Server DriverInvalid precisi on value (SQL-HY104) at ODBC_error.pl line 119. Issuing rollback() due to DESTROY without explicit disconnect() of DBD::ODBC::db handle NaviStor_s7200_IndiaCOE at ODBC_error.pl line 119.

      This is the error

        So it is a data base error. You need to examine the SQL and values that you used at the point that you get the error then clean up the data you are feeding to the data base.

        True laziness is hard work

        I bet it is a problem with one of your dates. Show us the SQL and any parameters at error time. You can do this with Statement and ParamValues and ParamTypes.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://837219]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-19 02:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found