2012-10-26 5 views
4

perl DBI의 bind_param과 관련된 문제가 있습니다. 다음 SQL 작동 : 다음하지 않는DBI : bind_param이 문자열을 ntext로 캐스팅합니다. -> nvarchar (max)와 ntext가 호환되지 않습니다.

my $sth = $dbh->prepare("SELECT id FROM table WHERE id = 'string'"); 
$sth->execute(); 

동안 :

my $sth = $dbh->prepare("SELECT id FROM table WHERE id = ?"); 
$sth->execute('string'); 

마지막 쿼리가 원인이 오류는 [ODBC SQL Server Driver][SQL Server]The data types nvarchar(max) and ntext are incompatible in the equal to operator. (SQL-42000)입니다.

execute에 의해 호출되는 bind_param이 ntext로 '문자열'을 전송합니다. 어떻게 해결할 수 있습니까?

+1

당신이 다음 SQLBindParameter를이 SQL_C_CHAR의 C 형과의 매개 변수 유형이라고합니다 (이 모양) DBD :: ODBC를 사용하는 가정을 어떤 드라이버는 매개 변수가 SQLDescribeParam에 있다고 말했습니다. 최신 DBI 및 DBD :: ODBC가있는 경우 DBI_TRACE = DBD = x.log를 설정하고 코드를 실행하면 x.log에서 가져 와서 수행 할 작업을 볼 수 있습니다. – bohica

답변

5

전에 SQL 호출 값 유형을 결합 고려 :

use DBI qw(:sql_types); 

my $sth = $dbh->prepare("SELECT id FROM table WHERE id = ?"); 

my $key = 'string'; 
my $sth->bind_param(1, $key, SQL_VARCHAR); 

$sth->execute();