What is the meaning of the prefix N in T-SQL statements and when should I use it?

前端 未结 4 799
长情又很酷
长情又很酷 2020-11-22 01:01

I have seen prefix N in some insert T-SQL queries. Many people have used N before inserting the value in a table.

I searched, but I was not able to unde

4条回答
  •  粉色の甜心
    2020-11-22 01:22

    1. Performance:

    Assume your where clause is like this:

    WHERE NAME='JON'
    

    If the NAME column is of any type other than nvarchar or nchar, then you should not specify the N prefix. However, if the NAME column is of type nvarchar or nchar, then if you do not specify the N prefix, then 'JON' is treated as non-unicode. This means the data type of NAME column and string 'JON' are different and so SQL Server implicitly converts one operand’s type to the other. If the SQL Server converts the literal’s type to the column’s type then there is no issue, but if it does the other way then performance will get hurt because the column's index (if available) wont be used.

    2. Character set:

    If the column is of type nvarchar or nchar, then always use the prefix N while specifying the character string in the WHERE criteria/UPDATE/INSERT clause. If you do not do this and one of the characters in your string is unicode (like international characters - example - ā) then it will fail or suffer data corruption.

提交回复
热议问题