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

Hi Monks, I would like your assistance to write a program that searches for files in a directory and grabs file's name to store it on a variable, then connect to SQL Server 2000 Database and select records out of a table base on the values store in an array variable; then format the records with a padded zeros to meet standards. Finally it will write the output to a file. I'm not familiar using Perl to Connect to SQL Server 2000 or formatting a variable to pad it with zeros to meet a certain length. Does anyone know any samples on how to a connect to a SQL Server database and format variables. Could this program best accomplish by writing a store procedure then connecting to sql server 2000 to run the store procedure and grab the output fields to a file?? I haven't seen that much DBI programming. I'll appreciate your assistance on how to accomplish this task.

Replies are listed 'Best First'.
Re: Accessing SQL Server 2000
by tcf22 (Priest) on Jul 06, 2003 at 19:03 UTC
    I normally use Win32::ODBC, not by choice, but it does the job.
    use strict; use Win32::ODBC; my ($db); if (!ref($db = new Win32::ODBC("DSN=$dbname;UID=$username;PWD=$passwor +d"))) { my ($ErrNum, $ErrText, $ErrConn) = Win32::ODBC::Error(); #...IT FAILED - DO SOMETHING... } my $sql = "SELECT * FROM Table"; if($db->Sql($sql)){ #...FAILED... my ($ErrNum, $ErrText, $ErrConn) = $db->Error(); } while($db->FetchRow()){ my %data = $db->DataHash(); #...Processing Data... } $db->Close();

    You could(my preference) use DBI::ODBC

    As for padding with 0s, you could use my $padded_var = sprintf('%.2d',$var); replacing 2 with the total length of the value.
Re: Accessing SQL Server 2000
by artist (Parson) on Jul 06, 2003 at 19:09 UTC
Re: Accessing SQL Server 2000
by Anonymous Monk on Jul 07, 2003 at 13:49 UTC

    I believe that MS advise that you now use ADO to access data. They say it is the most efficient way to do it. You'll need MDAC installed, though (Assuming a Win32 platform):

    #!C:/ActivePerl/bin/perl.exe -w use strict; use Win32::OLE; my ($ADO_Connection, $ADO_Recordset, $SQL_statement); my $conn_string="your DSN (see MSDN for syntax)"; $ADO_Connection = Win32::OLE->new("ADODB.Connection") or die("Couldn't create Connection object.\n"); $ADO_Recordset = Win32::OLE->new("ADODB.Recordset") or die("Couldn't create RecordSet object.\n"); $ADO_Connection->Open $conn_string; $ADO_RecordSet-{'ActiveConnection'} = $ADO_Connection $ADO_Recordset->{'Cursortype'} = 2 'adOpenKeySet $ADO_Recordset->{'LockType'} = 3 'adLockPessimistic $ADO_Recordset->Open $SQL_statement;

Re: Accessing SQL Server 2000
by bobn (Chaplain) on Jul 06, 2003 at 21:58 UTC
    I assess SQL Server 200 as buggy, expensive, insecure, proprietary, crappy ... oh, wait, you said accessing...

    --Bob Niederman, http://bob-n.com

      I find your assesment quite interesting. Besides the no brainers (its expensive and proprietary) the others are crap. SQL Server 2000, performs very well against the competitors according to independent bechmarks and tests. And is far easier to manage than most anything else (all the Sybase admins I know hate SQL server because it essentially puts them out of a job.)

      Perhaps there is a touch of anti MS venom in what you say and it really has nothing to do with an objective analysis of the program itself?

      Furthermore are you aware that you can use PerlScript from AS inside of SQL Server? IIRC Its the primary reason MS pumped so much money into AS and into perl itself, they were partially funding Sarathys work on 5.6 and the OnePerl project.

      As a last comment, its probably not the best idea to be an anti-ms troll here. Its not really in the spirit of Perl or the Monastery itself. :-)


      ---
      demerphq

      <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...

        Perhaps there is a touch of anti MS venom

        'Perhaps?' 'Touch?' I must be getting subtle in my old age.

        It was a joke. I do think their business practices are 100% despicable, but I wasn't really trying to get into that here.



        --Bob Niederman, http://bob-n.com
        all the Sybase admins I know hate SQL server because it essentially puts them out of a job
        Well.... MS-SQL may be easier to administer, but it is platform-specific, which limits scalability (although admittedly high-end Intel boxes are pretty fast these days), and with the ease of administration comes a reduction in tuning flexibility.

        Which of course has nothing whatsoever to do with perl, so I'll shut up now :-)

        Michael