mybatis-3 icon indicating copy to clipboard operation
mybatis-3 copied to clipboard

mybatis does not support N' for sql server

Open sharpboy2008 opened this issue 2 years ago • 8 comments

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.

sharpboy2008 avatar Mar 28 '22 15:03 sharpboy2008

Hello @sharpboy2008 ,

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

harawata avatar Mar 29 '22 15:03 harawata

non-English will become question marks,such as below, one is with N. the other is not. can this be fixed? image

sharpboy2008 avatar Mar 30 '22 14:03 sharpboy2008

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.

harawata avatar Mar 30 '22 19:03 harawata

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}

sharpboy2008 avatar Mar 31 '22 03:03 sharpboy2008

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

sharpboy2008 avatar Mar 31 '22 03:03 sharpboy2008

@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.

harawata avatar Mar 31 '22 19:03 harawata

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

qlwszl avatar May 23 '22 06:05 qlwszl

我有如下针对 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

awxiaoxian2020 avatar May 23 '22 08:05 awxiaoxian2020

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

harawata avatar Mar 08 '23 22:03 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.

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

ettingshausen avatar Apr 09 '24 16:04 ettingshausen