DB2 supports both stored procedures (SP) and user-defined functions (UDF). The former allows in, out and inout parameters that can be bound (see below) as can be expected. For the latter I cannot get bind variables to work.
I have a DB2 UDF (function) defined as follows (the simplest I could think of)
I can call this function with a statement like this:CREATE FUNCTION FUNC_JUST_RETURN_IT(i int) RETURNS int NO EXTERNAL ACTION DETERMINISTIC RETURN i @
I would also like to prepare the statement with the input variable as a bind variable in order to re-user the statement, e.g.-- execute directly SELECT DB2INST1.FUNC_JUST_RETURN_IT(42) FROM SYSIBM.SYSDUMMY1
Does the DB2, DBD::DB2 and DBI trio support bind variables to user defined function (UDF) as they do with stored procedures?-- prepare, then execute later SELECT DB2INST1.FUNC_JUST_RETURN_IT(?) FROM SYSIBM.SYSDUMMY1
use strict; use warnings; use DBD::DB2; use DBD::DB2::Constants; use Test::More; plan q/no_plan/; # I'm the man with no plan .. sub DBSETUP_PARAMS { return ('DBI:DB2:viper', 'db2inst1', '*****'); } # Call function with unbound value sub call_func_nobind { my $cardno = shift; my $rv = undef; my $dbh = DBI->connect(DBSETUP_PARAMS()); die(q{Connect failed!}) if (!defined($dbh)); my $sth = $dbh->prepare( qq{SELECT DB2INST1.FUNC_JUST_RETURN_IT($cardno) FROM SYSIBM.SYSDUM +MY1}); die(q{Prepare failed!}) if (!defined($sth)); $sth->execute() or die (q{execute failed}); die(q{Execute failed!}) if (!defined($sth)); my $row = $sth->fetchrow_arrayref(); ($row) ? $row->[0] : undef; } # Failing attempt to call function with bound value sub call_func_bind { my $cardno = shift; my $rv = undef; my $dbh = DBI->connect(DBSETUP_PARAMS()); die(q{Connect failed!}) if (!defined($dbh)); my $sth = $dbh->prepare(qq{SELECT DB2INST1.FUNC_JUST_RETURN_IT(?) FROM SYSIB +M.SYSDUMMY1}); die(q{Prepare failed!}) if (!defined($sth)); $sth->bind_param(1, $cardno); die(q{bind_param failed!}) if (!defined($sth)); $sth->execute() or die (q{execute failed}); die(q{Execute failed!}) if (!defined($sth)); my $row = $sth->fetchrow_arrayref(); ($row) ? $row->[0] : undef; } # ------ main ------ cmp_ok(call_func_nobind(q{123456}), q{==}, 123456, q{Expect 123456 ret +urned}); cmp_ok(call_func_bind(q{123456}), q{==}, 123456, q{Expect 123456 ret +urned}); 1; __END__
$ /usr/bin/perl -w call.function.pl ok 1 - Expect 123456 returned DBD::DB2::db prepare failed: [IBM][CLI Driver][DB2/LINUX] SQL0418N A +statement contains a use of a parameter marker that is not valid. SQ +LSTATE=42610 DBD::DB2::st execute failed: [IBM][CLI Driver][DB2/LINUX] SQL0418N A +statement contains a use of a parameter marker that is not valid. SQ +LSTATE=42610 execute failed at call.function.pl line 48. 1..1 # Looks like your test died just after 1. $
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |