【发布时间】:2011-02-17 07:53:20
【问题描述】:
来自 SQL::Statement::Functions 文档:
创建用户定义的函数
...
更复杂的函数可以使用一些总是自动传递给函数的参数。函数总是在 @_:
中接收这些值 sub FOO { my( $self, $sth, $rowhash, @params ); }
#!/usr/bin/env perl
use 5.012;
use warnings; use strict;
use DBI;
my $dbh = DBI->connect( "DBI:CSV:", undef, undef, { RaiseError => 1, } );
my $table = 'wages';
my $array_ref = [ [ 'id', 'number' ],
[ 0, 6900 ],
[ 1, 3200 ],
[ 2, 1800 ], ];
$dbh->do( "CREATE TEMP TABLE $table AS import( ? )", {}, $array_ref );
sub routine {
my $self = shift;
my $sth = shift;
my $rowhash = shift; #
return $_[0] / 30;
};
$dbh->do( "CREATE FUNCTION routine" );
my $sth = $dbh->prepare( "SELECT id, routine( number ) AS result FROM $table" );
$sth->execute();
$sth->dump_results();
当我尝试这个时,我收到一条错误消息:
DBD::CSV::st 执行失败:在 ./so.pl 第 27 行的除法 (/) 中使用未初始化的值 $_[0]。
[for Statement "SELECT id, routine(number) AS result FROM "wages""] at ./so.pl 第 34 行。
当我注释掉第三个参数时,我按预期工作(因为它看起来好像缺少第三个参数):
#!/usr/bin/env perl
...
sub routine {
my $self = shift;
my $sth = shift;
#my $rowhash = shift;
return $_[0] / 30;
};
...
0, 230
1、106.667
2、60
3行
这是一个错误吗?
【问题讨论】:
标签: sql perl csv dbi user-defined-functions