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

I have created a stored procedure in MS SQL 2005 that I want to call in perl. I am using the dbi:ODBC: connection of the DBI module to interact with perl and when I was looking at the info for DBI I ran accross this statment: "Calling stored procedures is currently not defined by the DBI. Some drivers, such as DBD::Oracle, support it in non-portable ways" So does anyone know how I might be able to call a proc, send it varibles to use, and retreave info from it?

Replies are listed 'Best First'.
Re: Perl and MS SQL 2k5 procs
by roboticus (Chancellor) on Apr 04, 2008 at 01:04 UTC

    Just call it as an SQL statement. I do it all the time. Instead of select * from foo use exec sp_foo. If you want to use in/out parameters, bind them up using a prepared statement.

    I'll try to dig up an example around here and put it up.

    ...roboticus

    Update: While delving through my hard drive looking for one of my bits of code that calls a proc, I tripped across proctest1.pl, which comes straight from the DBD-ODBC-1.14 distribution. That's a fine example. There are a couple of others in there, as well.

      Thanks I will look at proctest1.pl for more details.
      I finaly got around to looking for the proctest.pl but can't find it on cpan. I run off a shard server so I am not sure of exactly where to find the .pl file your talking about.
        You can download the tarball from CPAN, then extract the archive. You'll find the code in the mytest directory.

        ...roboticus

Re: Perl and MS SQL 2k5 procs
by derby (Abbot) on Apr 04, 2008 at 13:17 UTC

    Hmmm ... for MSSQL 2005, I cannot specifically say but for Sybase (which for our purposes is pretty much the same thing), you just call it like any other piece of SQL. The thing(s) to worry about:

    • Parameters need to be escaped: $sth->prepare( "exec foo \@name = 'baz'" )
    • Output parameters need to be handled differently (not sure if DBD::ODBC can handle that)
    • Stored procs may return multiple (differing) resultsets (not sure if DBD::ODBC can handle that)
    • Some stored procs have a tendency to not return resultsets (nor paramaters) but print to STDOUT - normally I catch these with a custom error handler (not sure if DBD::ODBC can handle that either)

    that being said, if your stored proc does the normal case (returns a single resultset), you should be fine.

    -derby