2008-09-15 5 views

답변

2

당신은 아마 DBI 라이브러리를 통해 연결되어야한다 먼저 한 다음 바인드 변수를 사용해야합니다. 예 : 다음과 같이 입력하십시오 :

#!/usr/bin/perl 
# 
use strict; 
use DBI qw(:sql_types); 

my $dbh = DBI->connect(
      $ConnStr, 
      $User, 
      $Password, 
      {RaiseError => 1, AutoCommit => 0} 
     ) || die "Database connection not made: $DBI::errstr"; 
my $sql = qq {CALL someProcedure(1);} } 

my $sth = $dbh->prepare($sql); 
eval { 
    $sth->bind_param(1, $argument, SQL_VARCHAR); 
}; 
if ([email protected]) { 
warn "Database error: $DBI::errstr\n"; 
$dbh->rollback(); #just die if rollback is failing 
} 

$dbh->commit(); 

이 테스트를 통과하지 못 했으므로 CPAN에서 정확한 구문을 찾아야합니다.

+0

eval {} 다음에 세미콜론을 깜박입니다. 일반적인 버그입니다. –

7

MySQL을 저장 프로 시저는 펄 DBD :: 4.001 이상 MySQL을 사용합니다. (http://www.perlmonks.org/?node_id=609098)

다음

는 최신 버전에서 작동 테스트 프로그램입니다 :

mysql> delimiter // 
mysql> create procedure Foo(x int) 
    -> begin 
    -> select x*2; 
    -> end 
    -> // 

perl -e 'use DBI; DBI->connect("dbi:mysql:database=bonk", "root", "")->prepare("call Foo(?)")->execute(21)' 

을하지만 너무 오래된 DBD의 버전 :: mysql을이있는 경우, 당신은이 같은 결과를 얻을 :

DBD::mysql::st execute failed: PROCEDURE bonk.Foo can't return a result set in the given context at -e line 1. 

CPAN을 사용하여 최신 DBD를 설치할 수 있습니다.

2
#!/usr/bin/perl 
# Stored Proc - Multiple Values In, Multiple Out 
use strict; 
use Data::Dumper; 
use DBI; 
my $dbh = DBI->connect('DBI:mysql:RTPC;host=db.server.com', 
    'user','password',{ RaiseError => 1 }) || die "$!\n"; 
my $sth = $dbh->prepare('CALL storedProcedure(?,?,?,?,@a,@b);'); 
$sth->bind_param(1, 2); 
$sth->bind_param(2, 1003); 
$sth->bind_param(3, 5000); 
$sth->bind_param(4, 100); 
$sth->execute(); 
my $response = $sth->fetchrow_hashref(); 
print Dumper $response . "\n"; 

나는 그것을 이해하는 데 다소 시간이 걸렸지 만, 위와 함께 필요한 것을 얻을 수있었습니다. 여러 개의 "회선"을 가져와야 할 필요가있는 경우, 나는 당신을 추측합니다. ...

while(my $response = $sth->fetchrow_hashref()) { 
    print Dumper $response . "\n"; 
} 

나는 그것이 도움이되기를 바랍니다. 위와 같지만 사용하여 SQL 간부와 유사한

1

안녕하세요. 나는 CALL 명령을 작동시킬 수 없었다. 대괄호 안에있는 내용을 모두 채우고 대괄호를 제거해야합니다.

 
    use DBI; 
    #START: SET UP DATABASE AND CONNECT 
    my $host  = '*[server]*\\*[database]*'; 
    my $database = '*[table]*'; 
    my $user  = '*[user]*'; 
    my $auth  = '*[password]*'; 

    my $dsn = "dbi:ODBC:Driver={SQL Server};Server=$host;Database=$database"; 
    my $dbh = DBI->connect($dsn, $user, $auth, { RaiseError => 1 }); 

    #END : SET UP DATABASE AND CONNECT 
    $sql = "exec *[stored procedure name]* *[param1]*,*[param2]*,*[param3]*;"; 
    $sth = $dbh->prepare($sql); 
    $sth->execute or die "SQL Error: $DBI::errstr\n"; 
관련 문제