N prefix and parameter

删除回忆录丶 提交于 2019-12-10 20:47:51

问题


I have some stored procedure

CREATE PROC MyProc ( @FullName NVARCHAR(200) = NULL )
    AS --.............

When I call this proc as exec MyProc 'Some english text' it works good.

But if call it as exec MyProc 'Русский текст' that is using Russian alphabet, it doesn't work properly.

And the call exec MyProc N'Русский текст' works good again.

I have a client application and... I need to add N prefix to parameters? If yes, how do I do it?


回答1:


The N would only be needed (manually) if you are concatenating a string in the .net code. It's automatic if you declare a SQLParameter as nvarchar: the framework takes care of it for you.

So your client code is incorrect and opens you up to SQL injection

Anyway, the N says that the string literal is unicode.




回答2:


N means National language character set also well known as Unicode.

See on Microsoft KB: You must precede all Unicode strings with a prefix N when you deal with Unicode string constants in SQL Server




回答3:


What kind of client application do you use?

  • if it is .Net - just specify that parameter has Unicode type.
  • If it is OLEDB/ADO - the same thing, param must be marked as unicode, and pass wchar_t instead of char

Update Sample from msdn (pay attention to NVarChar):

yourCommand.Parameters.Add(
    "@FullName", SqlDbType.NVarChar, 80).Value = "toasters";

And please avoid SQL injection ;)



来源:https://stackoverflow.com/questions/6857256/n-prefix-and-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!