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.

    In reply to Re: Re: Re: MSSQL/Perl by AcidHawk
    in thread MSSQL/Perl by Anonymous Monk

    Title:
    Use:  <p> text here (a paragraph) </p>
    and:  <code> code here </code>
    to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.