Why do some SQL strings have an 'N' prefix?

refer: http://databases.aspfaq.com/general/why-do-some-sql-strings-have-an-n-prefix.html

You may have seen Transact-SQL code that passes strings around using an N prefix. This denotes that the subsequent string is in Unicode (the N actually stands for National language character set). Which means that you are passing an NCHAR, NVARCHAR or NTEXT value, as opposed to CHAR, VARCHAR or TEXT. See Article #2354 for a comparison of these data types. 
 
Unicode is typically used in database applications which are designed to facilitate code pages which extend beyond the English and Western Europe code pages (Erland Sommarskog, a native of Sweden, refers to this set as "Germanic and Romance languages"), for example Chinese. Unicode is designed so that extended character sets can still "fit" into database columns. What this means is that Unicode character data types are limited to half the space, because each byte actually takes two bytes (Unicode is sometimes referred to as "double-wide"). For more information on Unicode, seeUnicode.org. Note that there are many encoding schemes in the Unicode standard, but SQL Server only supports one: UTF-16. 
 
While using Unicode is a design choice you can make in building your own applications, some facilities in SQL server require it. One example is sp_executeSQL. If you try the following:

EXEC sp_ExecuteSQL ‘SELECT 1‘

You will get this error:

Server: Msg 214, Level 16, State 2, Procedure sp_executesql, Line 1 
Procedure expects parameter ‘@statement‘ of type ‘ntext/nchar/nvarchar‘.

You can get around this in two ways:

-- (a) using the N prefix 
 
EXEC sp_ExecuteSQL N‘SELECT 1‘ 
 
-- (b) using a variable 
 
DECLARE @sql NVARCHAR(100) 
SET @sql = N‘SELECT 1‘ 
EXEC sp_ExecuteSQL @sql

Note that implicit conversion makes the N prefix optional in case (b); however, for legibility and consistency, you should always use the prefix when defining Unicode strings. One reason is that leaving it off can actually change your data if it contains Unicode characters (losing the additional information), as in the following example:

DECLARE @n NVARCHAR(10) 
SET @n = ‘a‘ 
PRINT @n 
SET @n = N‘a‘ 
PRINT @n

The first assignment, which didn‘t use the N prefix, gets printed as a regular a. Only the second maintains the character that was actually supposed to be represented. As you can imagine, if you are intending to support data entry in foreign languages and code pages, you will likely need to test for Unicode support (making sure that such columns support Unicode, and that data won‘t be lost when passed into stored procedures, functions, etc.). Note that your application will need to handle Unicode as well; for example, when you try to print this character from ASP...

<% 
    Response.Write("a") 
%>

...it actually prints out the string aa. (This result might depend on your codepage and regional settings.) So you might consider translating your data into its ASCII equivalent, e.g. a = ā. 
 
Another reason you want to avoid implicit conversion is that there are some potentially serious performance issues. Consider the following quite simple repro:

USE tempdb
GO 

CREATE TABLE a
(
    b VARCHAR(3),
    c NVARCHAR(3)
)
CREATE INDEX b ON a(b)
CREATE INDEX c ON a(c)
GO 

SET NOCOUNT ON 

INSERT a SELECT ‘foo‘, N‘foo‘
INSERT a SELECT ‘bar‘, N‘bar‘ 

DECLARE
    @b VARCHAR(3),
    @c NVARCHAR(3) 

SELECT
    @b = ‘foo‘,
    @c = N‘foo‘ 

SELECT * FROM a WHERE b = @b
SELECT * FROM a WHERE b = @c
SELECT * FROM a WHERE c = @b
SELECT * FROM a WHERE c = @c
SELECT * FROM a WHERE b LIKE @b
SELECT * FROM a WHERE b LIKE @c
SELECT * FROM a WHERE c LIKE @b
SELECT * FROM a WHERE c LIKE @c 

DROP TABLE a

Paste the code into Query Analyzer, turn execution plan on, and let her rip. You‘ll observe the following breakdown of percentage of work (roughly, depending on your hardware):

VARCHAR = VARCHAR 4.48%
VARCHAR = NVARCHAR 13.31%
NVARCHAR = VARCHAR 4.48%
NVARCHAR = NVARCHAR 4.48%
VARCHAR LIKE VARCHAR 4.48%
VARCHAR LIKE NVARCHAR 13.31%
NVARCHAR LIKE VARCHAR 4.48%
NVARCHAR LIKE NVARCHAR 4.48%

Now, that‘s not the whole story; we all know that there are many other factors, such as I/O, that will impact the actual time each portion of the query takes. The key is that implicit conversion *can* cause a table scan instead of an index seek, and on larger tables this can really hurt. While it‘s important to understand why this happens and in which scenarios, my recommendation is to match your character-based datatypes as explicitly as possible. 
 
One other thing to watch out for: your database may be using Unicode without your knowledge. If you upsize from Access to SQL Server, for example, character-based text columns might be translated to Unicode (I believe this is a catch-all technique; in case Access was storing Unicode strings, or if you might be storing Unicode strings later, you won‘t lose data or require changes). I think the Access upsizing tools should be updated to force a conscious choice, so that you aren‘t wasting space for nothing, and so that you know that you made a decision at all. 
 
For a more thorough discussion of Unicode and the N prefix, please see KB #239530, this MSDN article, and this Google thread
 
In other RDBMS platforms, or in the ANSI and/or ISO specifications, you might see prefixes other than N being used against values. (Current versions of SQL Server only support Unicode.) Here are the additional monikers I am aware of:

B This is used to denote a BINARY string expressed in bits (0 and 1 only)
X This is used to denote a BINARY string expressed in hexadecimal (0 -> F)

Related Articles

Why do some SQL strings have an 'N' prefix?

时间: 2024-07-29 23:08:04

Why do some SQL strings have an 'N' prefix?的相关文章

SQL*Loader FAQ

SQL*Loader FAQ: Contents [hide] 1 What is SQL*Loader and what is it used for? 2 How does one use the SQL*Loader utility? 3 How does one load MS-Excel data into Oracle? 4 Is there a SQL*Unloader to download data to a flat file? 5 Can one load variable

sqler sql 转rest api 源码解析(四)macro 的执行

macro 说明 macro 是sqler 的核心,当前的处理流程为授权处理,数据校验,依赖执行(include),聚合处理,数据转换 处理 授权处理 这个是通过golang 的js 包处理的,通过将golang 的http 请求暴露为js 的fetch 方法,放在js 引擎的执行,通过 http 状态吗确认是否是执行的权限,对于授权的处理,由宏的配置指定,建议通过http hreader处理 参考格式:    authorizer = <<JS       (function(){    

【译】微型ORM:PetaPoco【不完整的翻译】

PetaPoco是一款适用于.Net 和Mono的微小.快速.单文件的微型ORM. PetaPoco有以下特色: 微小,没有依赖项……单个的C#文件可以方便的添加到任何项目中. 工作于严格的没有装饰的Poco类,和几乎全部加了特性的Poco类 Insert/Delete/Update/Save and IsNew 等帮助方法. 分页支持:自动得到总行数和数据 支持简单的事务 更好的支持参数替换,包括从对象属性中抓取命名的参数. 很好的性能,剔除了Linq,并通过Dynamic方法快速的为属性赋值

DXDBGrid使用方法

http://www.cnblogs.com/gtsup/archive/2012/08/28/2660197.html dxDBGrid使用集锦 转载自:http://hi.baidu.com/ddjhw/item/05cdf1c516f37b6bf7c95d2c dxDBGrid使用集锦 一.如何设定可以多列自动排序?    只能使用分组:如按a1,a2,a3,a4排序    先按a1分组,再a2,再a3    然后设置a4的排列(升/降)二.如何设定左边几列,不能滚动?    建立Band

Mysql学习之--卸载源码mysql-5.6安装mysql-5.5

Mysql学习之--卸载源码mysql-5.6安装mysql-5.5 系统环境: 操作系统:RedHat EL6 DB Soft:  Mysql 5.5.12     Mysql 在linux下的安装方式有两种版本,一种为Binary(二进制),另外一种为Source(源码包),本文为Source Install方式. 由于,本机已经安装了mysql-5.6的版本,前面的版本采用源码包安装,只需要删除相应的安装文件即可! 1.卸载mysql-5.6 删除/var/lib/mysql下的文件: [

数据库学习之--Linux下Mysql源码包安装

数据库学习之--Linux下Mysql源码包安装 系统环境: 操作系统:RedHat EL6 DB Soft:  Mysql 5.6.4-m7     Mysql 在linux下的安装方式有两种版本,一种为Binary(二进制),另外一种为Source(源码包),本文为Source Install方式. 1.安装前的准备 解压安装包 [[email protected] ~]$ ls mysql-5.6.4-m7  mysql-5.6.4-m7.tar.gz  mysql-5.6.4-m7.ta

insert NULL into mysql

https://stackoverflow.com/questions/36898130/python-how-to-insert-null-mysql-values You are inserting the string 'NULL', not the NULL value. If these values are coming from a Python structure, you need to use something else to map to the NULL value i

Dapper.net ORM

参考链接:https://github.com/StackExchange/dapper-dot-net Dapper - a simple object mapper for .Net Dapper is a single file you can drop in to your project that will extend your IDbConnection interface. It provides 3 helpers: Dapper 只有一个文件,你可以把它放入你的项目,它扩展I

Python与数据库[2] -&gt; 关系对象映射/ORM -&gt; ORM 与 sqlalchemy 模块

ORM 与 sqlalchemy 1 关于ORM / About ORM 1.1 ORM定义 / Definition of ORM ORM(Object Relational Mapping),即对象关系映射.简单的说,ORM将数据库中的表与面向对象语言中的类建立了一种对应关系.这样,我们要操作数据库,数据库中的表或者表中的一条记录就可以直接通过操作类或者类实例来完成. 如果写程序用适配器(Adaptor)和程序交互,则需要要写原生SQL语句.如果进行复杂的查询,那SQL语句就要进行一点一点拼