RE: Calling DB2 Stored Procedures through DBD::ODBC

From: Jeff Urlwin (jurlwin_at_esoftmatic.com)
Date: 08/07/04


To: <samarth_kumar@vanguard.com>, <dbi-users@perl.org>
Date: Fri, 6 Aug 2004 18:21:36 -0400


>
> Folks,
>
> I am not sure if this has been answered before or not but on
> searching the
> list archives I haven't been able to get an answer. I am
> connecting to DB2
> using DBD::ODBC and am also able to run select statements.
> Now I have to
> run a stored procedure through the connection and was
> wondering if this is
> possible through the ODBC driver or do I have to install the
> native DB2
> driver.

Please see the DBD-ODBC distribution tarballs for examples in the t\*.t and
mytest\*.pl directories. t\20SqlServer.t and t\30Oracle.t have examples for
those systems. Testproc*.pl in the mytest should have some samples, too,
but the "mytest" directory is to handle specific things above and beyond
normal and a play area. Since some of the items have use as examples, I
publish them there.

Also, if you would like to contribute syntax for creating simple stored
procs for DB2, I am *happy* to include them in the tests in the same manner
that the SQLServer and Oracle tests are conducted. The more RDBMSs the
better, IMHO.

The short answer is you need the ODBC syntax wrapped around it.
        {? = call func_with_no_params() } # note your system may not like
the extra ()
                                                        # see
bind_param_inout
        { call proc() } # procedure, not function,
no expected return
        { ? = call func_with_params(?, ?, ?) }
Etc

>
> The second thing is if I can run the stored procedure through
> ODBC then
> can anyone please forward me sample code that shows how to do
> this as I
> have written snippets of code which are able to connect to
> DB2 but do not
> run the stored procedure. They are attached with this email.

The {} are important, I think. Try those in your code below.

Jeff

>
> Any help in this matter would be greatly welcome!!
>
> Thanks in advance,
>
> Samarth
>
>
> #!j:\perl\bin\perl
>
> use DBI;
> use CGI qw( :standard );
> use CGI::Carp qw( warningsToBrowser fatalsToBrowser );
>
> my $subsystem = "DQ0G";
> my $collectionID = "AVGI00";
> my $username = "";
> my $password = "";
>
> print( "Content-type: text/html\n\n" );
> print( "<html>\n" );
> print( "\t<head>\n" );
> print( "\t\t<title>DBI Connection Test</title>\n" );
> print( "\t</head>\n" );
> print( "<body>\n" );
>
> my $dbh = DBI -> connect( "DBI:ODBC:$subsystem", "$username",
> "$password"
> );
>
> if( !$dbh ) {
> print( "Unable to connect to database!!" . DBI -> errstr );
> exit;
> }
> else {
> print( "Connected to DB2!!\n" );
> }
> my $rv = $dbh -> prepare( 'call
> dq0gwcat.eposp522(\'AVGI00\',\'CIA\',\'WECIACAT\',\'IIOW\',\'2
> 004-08-05-12.10.23.798322\',\'N\',0,0,0,\'
> \',\' \',\'Y\',\' \',115436746,364285835)' );
>
> if( !rv ) {
> print( "Unable to prepare select statement: " . $dbh -> errstr );
> $dbh -> disconnect;
> exit;
> }
>
> $rv -> execute();
>
> print( "Executed stored procedure" );
>
> $rv -> finish;
> $dbh -> disconnect;
>
> print( "</body>" );
> print( "</html>\n" );
>



Relevant Pages

  • Calling DB2 Stored Procedures through DBD::ODBC
    ... I am connecting to DB2 ... The second thing is if I can run the stored procedure through ODBC then ... print("Executed stored procedure"); ...
    (perl.dbi.users)
  • Re: Simple query executes fast but renders slow...
    ... So, ODBC, right? ... no database engine can perform ... I've spent a lot of time on a PC running Access Queries against a DB2 ...
    (microsoft.public.access.queries)
  • Re: odbc command not returning results
    ... Have you turned on SQL Profiler to determine that the stored procedure is ... odbc call syntax to invoke a stored procedure. ... How To Execute SQL Parameterized Stored Procedures by Using the ODBC .NET ...
    (microsoft.public.dotnet.distributed_apps)
  • Re: Mailmerge from SQL Server 2000 Stored Procedure
    ... gather data for a mailmerge on Word 2003? ... The only way I know how to do this is to use Word VBA OpenDataSource to open a ODBC connection that issues an ODBC call "escape" rather than a SQL SELECT. ... One problem with this is that Word does not "see" the data in columns that have the Unicode data types in SQL Server when it connects via ODBC. ... In Word 2002 and later, you really ought to be able to use a stored procedure via OLE DB, but I have never found any syntax that works in the SQLStatement parameter. ...
    (microsoft.public.word.mailmerge.fields)
  • RE: Data Transfer b/w SQL Server 2005 and DB2 UDB.
    ... Your IBM guy may like the ODBC method because it's easy for him, ... install the Microsoft OLE DB for DB2 Provider (you will need ... colleagues, a DB2 DBA, has helped me in setting up the ODBC DSN for DB2 and I ...
    (microsoft.public.sqlserver.dts)