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
What does N' stands for in a SQL script ? (the one used before characters in insert script)
提问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
N
is 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 N
prefix 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 N
prefix 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 是指定它是一个字符串类型的值。
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
, NCHAR
or NTEXT
.
当类型为 from NVARCHAR
, NCHAR
or 时使用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).
这表示后续字符串在Unicode
(N 实际上代表国家语言字符集)。
Which means that you are passing an NCHAR
, NVARCHAR
or NTEXT
value, as opposed to CHAR
, VARCHAR
or TEXT
.
这意味着您传递的是NCHAR
,NVARCHAR
或NTEXT
值,而不是CHAR
,VARCHAR
或TEXT
。
回答by Marc Gravell
'abcd'
is a literal for a [var]char
string (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]char
string (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]char
should 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 前缀添加到字符串指示数据库包括国家代码以及字符代码