in reply to Re: Re: MSSQL/Perl
in thread MSSQL/Perl

There are a few things I found difficult with your code.

  • Firstly, Please use Code Tags around your code.
  • Next, if you had run perl -c yourscript.pl you would have seen a host of errors, for example the missing ; that has already been pointed out.
  • There also seem to be a fair number of typing errors in the code you posted.. this could be because you typed in a rush - if you tried the code you asked us to look at it would not have worked.
  • Also you have not declared $val0, $val1, $val2 or $count for that matter. I think you were trying to look at elements in the @vals array in which case you should have done something like $val[0], $val[1], $val[2]
  • You use fetchrow_hashref when reading in the records when you really want a fetchrow_array, although you could have used the fetchrow_hashref with something more along the following lines..
    while (my $vals = fetchrow_hashref) { print "name=$vals->{'name'}, wins=$vals->{'wins'}, losses=$vals->{ +'losses'}\n"; ++count; }

    So here is my untested re-write of your code with a few other changes - notice the connect string...

    use strict; use DBI; use DBD::ODBC; my ($data_source, $database, $userid, $password) =qw( server webdb web +dev webdevpass ); my $connect_string = "driver={SQL Server};Server=$data_source;Database +=$database"; #This is effectively your DSN my $dbh = DBI ->connect ( "DBI:ODBC:$connect_string","$userid","$passw +ord" )or die $DBI::errstr; # You need to check which version of the D +BD::ODBC module you are using my $sql = "select name, wins, losses from teams"; my $sth = $dbh->prepare ( $sql ); $sth->execute; my $count = 0; while (my @vals = $sth->fetchrow_array ) { printf "name =%s, wins =%d, losses = %d\n", $vals[0], $vals[1], $v +als[2]; ++$count; } print "$count rows total\n"; $sth->finish (); #$data ->close (); #Don't know what this is.. probably old code..? $dbh->disconnect (); exit (0);

    HTH.

    -----
    Of all the things I've lost in my life, its my mind I miss the most.
  • Replies are listed 'Best First'.