我有关于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
调用,将'string'强制转换为ntext。我该如何解决这个问题?
答案 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();