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

Hi All, I am using the MSSQL:Sqllib module to connect to MS SQL 2000.
After searching through the forums, I cannot find an answer to my question ---

If there are multiple rows retrieved on a SELECT statement, how do you cycle through each row ??

Thanks
  • Comment on Reviewing Rows from MSSQL SELECT statement

Replies are listed 'Best First'.
Re: Reviewing Rows from MSSQL SELECT statement
by marto (Cardinal) on Jun 08, 2005 at 08:51 UTC
    Hi,

    Have you read this page?
    I think you will find the answer here.

    Hope this helps,

    Cheers,

    Martin
Re: Reviewing Rows from MSSQL SELECT statement
by Grygonos (Chaplain) on Jun 08, 2005 at 12:38 UTC

    Not trying to deflect your question, but do you gain anything from using the MSSQL:SQLLib over say... DBI? DBI is database independant, so if you ever had to transition this code to another platform, the work would be minimal, compared to re-writing it from a platform specific module like you're using. I'm not trying to be a DBI zealot at all. DBI has just worked really well for me in MySQL,Oracle, and MSSQL.

    <edit> Since I was babbling about it I thought I would expand on the fact that DBI is an interface, and you need the database driver DBD::ODBC to use it with DBI. Check the POD on them, and there are some good examples of how to get something up and running. </edit>

      Since I posted this, I have started experimenting with DBI and have found that it would make sense to use this method.
      Once again, thanks to all Monks that replied..
      Danny
      Grygonos,
      Do I have to specify use DBD::ODBC after the use DBI
      statement ?
      After reading various help pages on DBI/DBD, I thought
      that the DBI module loaded the appropriate DBD part on
      connect.
      Thanks
        No you don't.
        use DBI; my $dbh = DBI->connect('DBI:ODBC:driver=SQL Server;database=mydb;serve +r=myserver;app=myapp',$username,$password);
Re: Reviewing Rows from MSSQL SELECT statement
by Nevtlathiel (Friar) on Jun 08, 2005 at 08:50 UTC
    Update: Whoops, my bad, that'll teach me not to read the question properly. VSarkiss is correct and thanks for pointing it out :)

    my $sth = $dbh->prepare("SELECT * FROM $table"); $sth->execute; while (my $array_ref = $sth->fetch) { # #do something # }

    And if you're not you should probably check that your query executes too :)

    ----------
    My cow-orkers were talking in punctuation the other day. What disturbed me most was that I understood it.