1.建立一个测试表
CREATE table myTestChar
(
c1 int not null identity primary key,
c2 char(8)
)
插入两条语句:
INSERT into myTestChar VALUES('中国')
INSERT into myTestChar VALUES(N'中国')
select * from myTestChar
c1 c2
1 ??
2 ??
返回的都是乱码,这是因为汉字和字符采用的是不同的编码方式,不论是否强制的采用unicode编码方式都是乱码。字符无法存储中文。
( 有的数据库版本不会出现这种现象)
2.给该表添加一个字段,是nchar类型的
ALTER table mytestChar
add c3 nchar(8)
插入两条记录:
INSERT into myTestChar VALUES(N'中国','中国')
INSERT into myTestChar VALUES(N'中国',N'中国')
返回结果:
c1 c2 c3
1 ?? NULL
2 ?? NULL
3 ?? ??
4 ?? 中国
INSERT into myTestChar VALUES(N'中国','china')
INSERT into myTestChar VALUES(N'中国',N'china')
返回结果:
c1 c2 c3
1 ?? NULL
2 ?? NULL
3 ?? ??
4 ?? 中国
5 ?? china
6 ?? china
结论:存储中国字的时候需要强制编码下。因为存储一个中国字需要两个字节。
INSERT into myTestChar VALUES(N'中国中国中国','china')
执行上面语言会出现:
将截断字符串或二进制数据。
语句已终止。
这个因为6个字需要12char来存储。
INSERT into myTestChar VALUES(N'中国中')
则执行成功。