This code can't compile.

You are missing a semicolon after use DBD::ODBC and after my $sql ... and some variables don't match with their declaration ($user $userid, $connect_string $conn_string). You didn't declare $sth and $count.

Moreover, you are assigning the result of fetchrow_hashref to an array.

You should read Reading from a database to get started with the DBI, and maybe some of the more basic Tutorials to use the language more proficiently.

And, please, use CODE tags when you post your code.

Update

Here is your code a little bit better formatted. I won't guarantee that it does what you want, but at least it compiles without errors.

use strict; use DBI; #use DBD::ODBC ; my ($data_source, $database, $userid, $password) = qw( server webdb webdev webdevpass ); my $connect_string = "driver={SQL Server};Server=$data_source;Database=$database;UID=$useri +d;PWD=$password"; my $dbh = DBI ->connect ( "DBI:ODBC:host=localhost;$connect_string" ) or die $DBI::errstr; my $sql = "select name, wins, losses from teams"; my $sth = $dbh->prepare ( $sql ); $sth->execute; my $count = 0; while (my @val = $sth->fetchrow_array ) { printf "name =%s, wins =%d, losses = %d\n", $val[0], $val[1], $val[2]; ++$count; } print "$count rows total\n"; $dbh->disconnect ();

I changed your code to use fetchrow_array instead of fetchrow_hashref.

You really need to read some docs. The DBI documentation (perldoc DBI) will give you good references on how to use things. The tutorial I have mentioned above is a good start. It is true that it uses MySQL, but using DBI, the only thing that is (in principle) different is only the connection statement. After that, the methods are the same.

Good luck.


In reply to Re: Re: Re: MSSQL/Perl by rnahi
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.