4

我有一个关于 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_param,它被 调用execute,将“字符串”转换为 ntext。我该如何解决这个问题?

4

1 回答 1

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();
于 2012-10-27T07:54:20.227 回答