declare @星毅 nvarchar(100)
set @星毅=N'新年好!'
---------^^^^凡是nvarchar,nchar,ntext的常量前必须加N
select @星毅
-------------你的window是中文版的!也许可以不加,但如果你的程序在英文版的上面运行就回报错!
-------------你要养成良好的编程习惯!!
set @星毅=N'新年好!'
---------^^^^凡是nvarchar,nchar,ntext的常量前必须加N
select @星毅
-------------你的window是中文版的!也许可以不加,但如果你的程序在英文版的上面运行就回报错!
-------------你要养成良好的编程习惯!!
declare @大力 varchar(100)set @星毅=N'新年好!'
set @大力='大家好!'select len(@星毅) N型的字符数,len(@大力) 非N型的字符数
-----------上面是相同的
select datalength(@星毅) N型的字节数,datalength(@大力) 非N型的字节数
-----------上面的结果是N型是非N型的两倍!!
当在 WHERE 子句中搜索 Unicode 字符串时,请在搜索字符串之前加字符 N
为什么?感觉有点还不是很清晰,所以需要大家再点拨下,:)
declare @大力 varchar(100)set @星毅=N'dali'
set @大力='dali'select len(@星毅) N型的字符数,len(@大力) 非N型的字符数
-----------上面是相同的
select datalength(@星毅) N型的字节数,datalength(@大力) 非N型的字节数
-----------上面的结果是N型是非N型的两倍!!
select * from 表 where N型字段='星毅'
会查找出来!但那是不规则的写法!!一旦你的程序在客户那运行,如果所以应改为:
select * from 表 where N型字段=N'星毅'
---------
create database test
go
ALTER DATABASE test COLLATE Czech_CI_AI
go
use test
go
declare @a nvarchar
declare @b nvarcharset @a=N'大力'
set @b='星毅'
select @a
select @b
按照你所贴的,结果为:
大
?如果我在所建立的Test数据库中再执行,结果为:
use test
godeclare @a nvarchar(2)
declare @b nvarchar(2)set @a=N'大力'
set @b='星毅'
select @a
select @bOutput:
大力
星毅对吗,好象不对吧?
你没运行吗??create database test
go
ALTER DATABASE test COLLATE Czech_CI_AI
go
use test
go
declare @a nvarchar
declare @b nvarcharset @a=N'大力'
set @b='星毅'
select @a
select @b
go
ALTER DATABASE test COLLATE Czech_CI_AI
-----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^你没运行吗??
go
use test -----这局也要!!!
go
declare @a nvarchar
declare @b nvarcharset @a=N'大力'
set @b='星毅'
select @a
select @b
可是如果我现在再在数据库test下运行这个
use test
godeclare @a nvarchar(2)
declare @b nvarchar(2)set @a=N'大力'
set @b='星毅'
select @a
select @b那么输出就是:
大力
星毅
用于非 Unicode 数据的代码页: 1250
支持的 Windows 区域设置: 捷克语
-------------
大力
?----------怪了!这也许就是不写N的坏处吧!同样的程序在我的sql2k企业版上,和你的机器运行结果竟然不同!你想向一下,你的程序如果在繁体的机器上又回出什么结果!
先运行你最初的代码:
那么输出结果应该是:
大
?现在再在你新建的数据库test下,执行:
use test
godeclare @a nvarchar(2)
declare @b nvarchar(2)set @a=N'大力'
set @b='星毅'
select @a
select @b看看输出结果如何,因为这时已经更改了代码页了,为1250。
我用的是sql server 2000 developer enterprise
大力
---------------
星毅win2kas+sql2kde
我的:
drop database test
go
create database test
go
use test
go
declare @a nvarchar(2)
declare @b nvarchar(2)
set @a=N'大力'
set @b='星毅'
select @a
select @b
刚才看了下,排序规则还是Chinese-PRC-CI-AS还想请教下大力一个问题:在设计时,采用char,varchar,text,还是nchar,nvarchar,ntext好呢?
nchar最大4000