C# N' 在 SQL 脚本中代表什么?(插入脚本中字符前使用的那个)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14353238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:34:09  来源:igfitidea点击:

What does N' stands for in a SQL script ? (the one used before characters in insert script)

c#asp.netdatabase

提问by Ebenezar John Paul

I generated an sql script like this,

我生成了一个这样的sql脚本,

INSERT [dbo].[TableName] ([Sno], [Name], [EmployeeId], [ProjectId], [Experience]) 
VALUES (1, N'Dave', N'ESD157', N'FD080', 7)

I wonder whats that N' exactly mean and whats its purpose here.

我想知道 N' 到底是什么意思,它在这里的目的是什么。

NOTE: By searching for the answer all i can get is that N' is a prefix for National language standard and its for using unicode data. But honestly i am not able to get a clear idea about the exact operation of N' here. I'd appreciate your help and please make it in more of an understandableway. Thanks in advance.

注意:通过搜索答案,我只能得到 N' 是国家语言标准的前缀,它用于使用 unicode 数据。但老实说,我无法清楚地了解 N' 的确切操作。感谢您的帮助,并请以更易于理解的方式提供帮助。提前致谢。

采纳答案by Richard Schneider

Nis used to specify a unicode string.

N用于指定 unicode 字符串。

Here's a good discussion: Why do some SQL strings have an 'N' prefix?

这是一个很好的讨论:为什么有些 SQL 字符串有一个“N”前缀?

In your example Nprefix is not required because ASCII characters (with value less than 128) map directly to unicode. However, if you wanted to insert a name that was not ASCII then the Nprefix would be required.

在您的示例中,N不需要前缀,因为 ASCII 字符(值小于 128)直接映射到 unicode。但是,如果您想插入一个非 ASCII 名称,则N需要前缀。

INSERT [dbo].[TableName] ([Sno], [Name], [EmployeeId], [ProjectId], [Experience]) 
VALUES (1, N'Wāhi', 'ESD157', 'FD080', 7)

回答by Habib

N is to specify that its a string type value.

N 是指定它是一个字符串类型的值。

[N]'tsql_string'

[N]'tsql_string'

Is a constant string. tsql_string can be any nvarchar or varchar data type. If the N is included, the string is interpreted as nvarchardata type.

是一个常量字符串。tsql_string 可以是任何 nvarchar 或 varchar 数据类型。如果包含 N,则字符串被解释为 nvarchar数据类型。

回答by Vishal Suthar

The "N"prefix stands for National Language in the SQL-92 standard, and is used for representing unicode characters.

"N"前缀代表国家语言在SQL-92标准,并用于表示Unicode字符。

Any time you pass Unicode data to SQL Server you must prefix the Unicode string with N.

任何时候将 Unicode 数据传递给 SQL Server 时,都必须在 Unicode 字符串前加上N.

It is used when the type is from NVARCHAR, NCHARor NTEXT.

当类型为 from NVARCHAR, NCHARor 时使用NTEXT

For more info refer to this: Why do some SQL strings have an 'N' prefix?

有关更多信息,请参阅:Why do some SQL strings have an 'N' prefix?

回答by John Woo

This denotes that the subsequent string is in Unicode(the N actually stands for National language character set).

这表示后续字符串在UnicodeN 实际上代表国家语言字符集)。

Which means that you are passing an NCHAR, NVARCHARor NTEXTvalue, as opposed to CHAR, VARCHARor TEXT.

这意味着您传递的是NCHAR,NVARCHARNTEXT值,而不是CHAR,VARCHARTEXT

回答by Marc Gravell

'abcd'is a literal for a [var]charstring (or maybe text, but varchar(max)would be more common now) - occupying 4 bytes memory, and using whatever code-page the SQL server is configured for. N'abcd'is a literal for a n[var]charstring (or maybe ntext, but nvarchar(max)would be preferable), occupying 8 bytes of memory using UTF-16. This allows for full international usage, and frankly n[var]charshould probably be the default in most systems.

'abcd'[var]char字符串的文字(或者可能text,但varchar(max)现在更常见) - 占用 4 字节内存,并使用 SQL 服务器配置的任何代码页。N'abcd'n[var]char字符串的文字(或者可能ntext,但nvarchar(max)会更好),使用 UTF-16 占用 8 个字节的内存。这允许完全国际使用,坦率地说n[var]char应该是大多数系统中的默认设置。

回答by MikeT

each country has its own specific letters and symbols so a database set up for English US will not recognise the £ symbol which a English UK database would, the same goes for Spanish, French, German

每个国家都有自己特定的字母和符号,因此为美国英语建立的数据库不会识别英国英语数据库会识别的 £ 符号,西班牙语、法语、德语也是如此

Also other languages like Chinese, Japanese, Hebrew, Arabic don't use any Latin characters.

其他语言,如中文、日语、希伯来语、阿拉伯语也不使用任何拉丁字符。

so anyone trying to enter any data not contained in the local character set will fail or suffer data corruption, if you are using varchar, so if there is even the remotest possibility that your database will need to support more than one local character set then you have to use the nationalised language character set aka unicode aka NChar, which allows the character sets nationality to be recorded with the character. providing international text support

因此,如果您使用的是 varchar,任何试图输入未包含在本地字符集中的任何数据的人都将失败或遭受数据损坏,因此,如果您的数据库需要支持多个本地字符集,那么即使是最遥远的可能性,那么您必须使用民族化语言字符集,即 unicode aka NChar,它允许字符集国籍与字符一起记录。提供国际文本支持

Likewise adding the N Prefix to a string instructs the database to include the Nation code as well as the character code

同样,将 N 前缀添加到字符串指示数据库包括国家代码以及字符代码