DBI:bind_param将字符串转换为ntext - > nvarchar(max)和ntext不兼容

时间:2012-10-26 16:35:25

标签: sql perl dbi sqlbindparameter ntext

我有关于perl DBI的bind_param的问题。以下SQL有效:

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)

似乎bind_paramexecute调用,将'string'强制转换为ntext。我该如何解决这个问题?

1 个答案:

答案 0 :(得分: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();