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

sk9*_*861 4 sql perl ntext sqlbindparameter dbi

我有一个关于perl DBI的bind_param的问题.以下SQL工作原理:

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

虽然以下不是:

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

上次查询导致的错误是[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,将'string'强制转换为ntext.我该如何解决这个问题?

小智 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();
Run Code Online (Sandbox Code Playgroud)