varchar,nvarchar不同呀。小心出错
今天调程序,执行SQL老是报错。最终发现是 varchar 和 nvarchar 造成的。郁闷。
本来没有这个问题,但是接口使用方传过来的字串编码错误,导致乱码。
CREATE TABLE [TEST] (
[nn] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
CONSTRAINT [PK_TEST] PRIMARY KEY CLUSTERED
(
[nn]
) ON [PRIMARY]
) ON [PRIMARY]
GO
![](/Images/OutliningIndicators/None.gif)
select * from TEST
![](/Images/OutliningIndicators/None.gif)
insert into TEST(nn)
values(N'BSS?ê?§ID')
![](/Images/OutliningIndicators/None.gif)
insert into TEST(nn)
values(N'BSSÕÊ»§ID')
/*
服务器: 消息 2627,级别 14,状态 1,行 1
违反了 PRIMARY KEY 约束 'PK_TEST'。不能在对象 'TEST' 中插入重复键。
语句已终止。
![](/Images/OutliningIndicators/InBlock.gif)
*/
![](/Images/OutliningIndicators/None.gif)
declare @s nvarchar(50)
,@s2 nvarchar(50)
![](/Images/OutliningIndicators/None.gif)
select @s =N'BSS?ê?§ID' , @s2 = N'BSSÕÊ»§ID'
![](/Images/OutliningIndicators/None.gif)
select 1 where @s=@s2
![](/Images/OutliningIndicators/None.gif)
select 1 where N'BSS?ê?§ID'=N'BSSÕÊ»§ID'
/*
![](/Images/OutliningIndicators/InBlock.gif)
(所影响的行数为 0 行)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
(所影响的行数为 0 行)
*/
![](/Images/OutliningIndicators/None.gif)
declare @s varchar(50)
,@s2 varchar(50)
![](/Images/OutliningIndicators/None.gif)
select @s =N'BSS?ê?§ID' , @s2 = N'BSSÕÊ»§ID'
![](/Images/OutliningIndicators/None.gif)
select 1 where @s=@s2
![](/Images/OutliningIndicators/None.gif)
select 1 where N'BSS?ê?§ID'=N'BSSÕÊ»§ID'
/*
![](/Images/OutliningIndicators/InBlock.gif)
(所影响的行数为 1 行)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
(所影响的行数为 0 行)
![](/Images/OutliningIndicators/InBlock.gif)
*/
本来没有这个问题,但是接口使用方传过来的字串编码错误,导致乱码。
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
QQ:273352165
evlon#126.com
转载请注明出处。