如何在 SQL Server 中使用 nvarchar 变量为 unicode 加上前缀“N”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2836606/
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
How to user prefix 'N' for unicode with nvarchar variable in SQL Server?
提问by Nijesh Patel
How to user prefix 'N' for unicode with nvarchar variable in SQL Server? For example:
如何在 SQL Server 中使用 nvarchar 变量为 unicode 加上前缀“N”?例如:
Given this variable:
鉴于此变量:
declare @Query1 nvarchar(max)
I can assign to it like this:
我可以像这样分配给它:
set @Query1 = N'??????'
But what if I want to use N@Query1
somewhere?
但是如果我想在N@Query1
某个地方使用怎么办?
回答by KM.
You only need to use N'xyz' when you have literal text. Once the nvarchar data is in a variable or result set column of nvarchar data type you no longer need to use the N'...' notation, the system knows the data type at that point.
当您有文字文本时,您只需要使用 N'xyz'。一旦 nvarchar 数据位于 nvarchar 数据类型的变量或结果集列中,您就不再需要使用 N'...' 表示法,系统就会知道此时的数据类型。
try it out:
试试看:
DECLARE @A nvarchar(100)
,@B nvarchar(100)
SET @A = N'anything here!!'
SET @B=@A --would work the same if coming from a query result set as well
SELECT @B --will show special unicode characters
回答by REXMAN
declare @nv1 nvarchar(50) = N'??????', @nv2 nvarchar(50) = '??????', @v varchar(50) = N'??????'
declare @nv3 nvarchar(50) = @nv1 + ' hallo', @nv4 nvarchar(50) = @nv1 + N' hallo'
select @nv1, @nv2, @nv3, @nv4, @v
回答by Javi Agredo
Thanks to marc_s for his answer, that solved my problem. I highlight it here as an answer for those who can't find it.
感谢 marc_s 的回答,这解决了我的问题。我在这里强调它作为那些找不到它的人的答案。
" if @Query1 IS a NVARCHAR variable, then it IS a NVARCHAR variable and you don't need to prefix it with another 'N' to make it NVARCHAR.... just use it "
“如果@Query1 是一个 NVARCHAR 变量,那么它就是一个 NVARCHAR 变量,你不需要在它前面加上另一个 'N' 来使它成为 NVARCHAR ......只需使用它”
回答by Ben Robinson
It is used with string literals to indicate the text should be treated as unicode. e.g.
它与字符串文字一起使用以指示应将文本视为 unicode。例如
DECLARE @something NVARCHAR(100)
SET @something = N'sometext'
回答by Jeremy
Declare @var nvarchar(255)
Set @var = N'Hello World'
print @var
create table #tmp( columnA nvarchar(255) not null)
insert into #tmp(columnA) Values (N'Test')
Select @var = columnA from #tmp
print @var
drop table #tmp