我什么时候应该使用SQL Server Unicode'N'常量?

时间:2021-10-14 01:10:00

I've been looking into the use of the Unicode 'N' constant within my code, for example:

我一直在研究在代码中使用Unicode'N'常量,例如:

select object_id(N'VW_TABLE_UPDATE_DATA', N'V');

insert into SOME_TABLE (Field1, Field2) values (N'A', N'B');

After doing some reading around when to use it, and I'm still not entirely clear as to the circumstances under which it should and should not be used.

在阅读了何时使用它之后,我仍然不完全清楚应该和不应该使用它的情况。

Is it as simple as using it when data types or parameters expect a unicode data type (as per the above examples), or is it more sophiticated than that?

当数据类型或参数期望unicode数据类型(根据上面的示例)时,它是否像使用它一样简单,或者它是否比它更加sophiticated?

The following Microsoft site gives an explanation, but I'm also a little unclear as to some of the terms it is using http://msdn.microsoft.com/en-us/library/ms179899.aspx

下面的Microsoft站点给出了解释,但我对它使用的一些术语有点不清楚http://msdn.microsoft.com/en-us/library/ms179899.aspx

Or to precis:

或者说:

Unicode constants are interpreted as Unicode data, and are not evaluated by using a code page. Unicode constants do have a collation. This collation primarily controls comparisons and case sensitivity. Unicode constants are assigned the default collation of the current database, unless the COLLATE clause is used to specify a collation.

Unicode常量被解释为Unicode数据,并且不使用代码页进行评估。 Unicode常量确实有一个排序规则。此归类主要控制比较和区分大小写。除非使用COLLATE子句指定排序规则,否则为Unicode常量分配当前数据库的默认排序规则。

What does it mean by:

它是什么意思:

  • 'evaluated by using a code page'?
  • '使用代码页评估'?
  • Collation?
  • 整理?

I realise this is quite a broad question, but any links or help would be appreciated.

我意识到这是一个相当广泛的问题,但任何链接或帮助将不胜感激。

Thanks

谢谢

2 个解决方案

#1


1  

Is it as simple as using it when data types or parameters expect a unicode data type?

当数据类型或参数需要unicode数据类型时,它是否像使用它一样简单?

Pretty much.

差不多。

To answer your other points:

回答你的其他观点:

A code page is another name for encoding of a character set. For example, windows code page 1255 encodes Hebrew. This is normally used for 8bit encodings for characters. In terms of your question, strings may be evaluated using different code pages (so the same bit pattern may be interpreted as a Japanese character or an Arabic one, depending on what code page was used to evaluate it).

代码页是字符集编码的另一个名称。例如,Windows代码页1255编码希伯来语。这通常用于8位字符编码。就您的问题而言,可以使用不同的代码页来评估字符串(因此相同的位模式可以被解释为日语字符或阿拉伯语字符,具体取决于用于评估它的代码页)。

Collation is about how SQL Server is to order strings - this depends on code page, as you would order strings in different languages differently. See this article for an example.

排序规则是关于SQL Server如何排序字符串 - 这取决于代码页,因为您将以不同的语言对不同语言的字符串进行排序。有关示例,请参阅此文章。

#2


1  

National character nchar() and nvarchar() use two bytes per character and support international character set -- think internet. The N prefix converts a string constant to two bytes per character. So if you have people from different countries and would like their names properly stored -- something like:

国家字符nchar()和nvarchar()每个字符使用两个字节并支持国际字符集 - 思考互联网。 N前缀将字符串常量转换为每个字符两个字节。因此,如果您有来自不同国家/地区的人,并希望正确存储他们的名称,例如:

CREATE TABLE SomeTable ( 
   id int
  ,FirstName nvarchar(50)
  );

Then use:

然后使用:

INSERT  INTO SomeTable
        ( Id, FirstName )
VALUES  ( 1, N'Guðjón' );

and

SELECT  *
FROM    SomeTable
WHERE   FirstName = N'Guðjón';

#1


1  

Is it as simple as using it when data types or parameters expect a unicode data type?

当数据类型或参数需要unicode数据类型时,它是否像使用它一样简单?

Pretty much.

差不多。

To answer your other points:

回答你的其他观点:

A code page is another name for encoding of a character set. For example, windows code page 1255 encodes Hebrew. This is normally used for 8bit encodings for characters. In terms of your question, strings may be evaluated using different code pages (so the same bit pattern may be interpreted as a Japanese character or an Arabic one, depending on what code page was used to evaluate it).

代码页是字符集编码的另一个名称。例如,Windows代码页1255编码希伯来语。这通常用于8位字符编码。就您的问题而言,可以使用不同的代码页来评估字符串(因此相同的位模式可以被解释为日语字符或阿拉伯语字符,具体取决于用于评估它的代码页)。

Collation is about how SQL Server is to order strings - this depends on code page, as you would order strings in different languages differently. See this article for an example.

排序规则是关于SQL Server如何排序字符串 - 这取决于代码页,因为您将以不同的语言对不同语言的字符串进行排序。有关示例,请参阅此文章。

#2


1  

National character nchar() and nvarchar() use two bytes per character and support international character set -- think internet. The N prefix converts a string constant to two bytes per character. So if you have people from different countries and would like their names properly stored -- something like:

国家字符nchar()和nvarchar()每个字符使用两个字节并支持国际字符集 - 思考互联网。 N前缀将字符串常量转换为每个字符两个字节。因此,如果您有来自不同国家/地区的人,并希望正确存储他们的名称,例如:

CREATE TABLE SomeTable ( 
   id int
  ,FirstName nvarchar(50)
  );

Then use:

然后使用:

INSERT  INTO SomeTable
        ( Id, FirstName )
VALUES  ( 1, N'Guðjón' );

and

SELECT  *
FROM    SomeTable
WHERE   FirstName = N'Guðjón';