i have an statement like below for sql server,

insert into student(name,age) values(N'张三',10)

it seems we can not use N'#{name}' in mapper file.

Comment From: harawata

Hello @sharpboy2008 ,

I don't think N is necessary in normal situation. What happens if you just write #{name} instead of N'#{name}'?

Comment From: sharpboy2008

non-English will become question marks,such as below, one is with N. the other is not. can this be fixed? MyBatis mybatis does not support N' for sql server

Comment From: harawata

That probably means that you set sendStringParametersAsUnicode=false in the JDBC URL (the default is true). Removing this option may fix the problem, but it could have negative impact on performance.

Another solution is to specify jdbcType or typeHandler explicitly in each parameter reference.

  • #{name,jdbcType=NVARCHAR}
  • #{name,typeHandler=org.apache.ibatis.type.NStringTypeHandler}

It is also possible to override the default type handler for String in the config, but it may cause error when querying data if the columns are defined as VARCHAR, not NVARCHAR.

Comment From: sharpboy2008

yes.we can not do this with 'sendStringParametersAsUnicode=false' as you said it huge impact on the performance. i will try either of them, is there any impact?

{name,jdbcType=NVARCHAR}

{name,typeHandler=org.apache.ibatis.type.NStringTypeHandler}

Comment From: sharpboy2008

how about nvarchar(max)? we have some columns with nvarchar(max).still #{name,jdbcType=NVARCHAR}

Comment From: harawata

@sharpboy2008 , The size of the column should not matter. If you have any difficulty, please provide a small demo project like these so that we can reproduce the same issue on our end.

Comment From: qlwszl

我有如下针对 sql server 的语句, insert into student(name,age) values('N'||'张三',10)

Comment From: awxiaoxian2020

我有如下针对 sql server 的语句, insert into student(name,age) values('N'||'张三',10)

I'm learning sql server, I'm not familiar with N sql,but the statement may have an error (in SQL Server 2008R2). issue_sql

Comment From: harawata

As there is no reply, I assume the problem has been resolved.

Comment From: ettingshausen

That probably means that you set sendStringParametersAsUnicode=false in the JDBC URL (the default is true). Removing this option may fix the problem, but it could have negative impact on performance.

Another solution is to specify jdbcType or typeHandler explicitly in each parameter reference.

  • #{name,jdbcType=NVARCHAR}
  • #{name,typeHandler=org.apache.ibatis.type.NStringTypeHandler}

It is also possible to override the default type handler for String in the config, but it may cause error when querying data if the columns are defined as VARCHAR, not NVARCHAR.

Only #{name,typeHandler=org.apache.ibatis.type.NStringTypeHandler} works.