LIKE 运算符、N 和 % SQL 服务器在 nvarchar 列上不起作用
LIKE operator, N and % SQL Server doesn't work on nvarchar column
有什么方法可以使以下查询有效吗?
declare @t nvarchar(20)
set @t='حس'
SELECT [perno] ,[pName]
FROM [dbo].[People]
Where [pName] like N''+@t +'%'
我不能这样使用:
Where [pName] like N'حس%'
或者使用存储过程:
ALTER PROCEDURE [dbo].[aTest]
(@t nvarchar(20))
AS
BEGIN
SELECT [perno] ,[pName]
FROM [dbo].[People]
WHERE ([People].[pName] LIKE N'' +@t + '%')
END
您不需要在 WHERE
子句中使用 N
前缀,因为您的变量已经是 nvarchar
,并且您传递的变量不是 文字字符串.
这是一个例子:
CREATE TABLE People
(
ID INT,
Name NVARCHAR(45)
);
INSERT INTO People VALUES
(1, N'حسام'),
(2, N'حسان'),
(3, N'حليم');
DECLARE @Name NVARCHAR(45) = N'حس';--You need to use N prefix when you pass the string literal
SELECT *
FROM People
WHERE Name LIKE @Name + '%'; --You can use it here when you pass string literal, but since you are passing a variable, you don't need N here
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.
来自docs
Prefix Unicode character string constants with the letter N. Without the N prefix, the string is converted to the default code page of the database. This default code page may not recognize certain characters.
为了简单回答评论中的问题,您使用了错误的数据类型,因此 ALTER
存储过程并将参数的数据类型从 VARCHAR
更改为 NVARCHAR
.
更新:
由于您使用的是 SP,您可以创建您的 SP(根据您的)为
CREATE PROCEDURE MyProc
(
@Var NVARCHAR(45)
)
AS
BEGIN
SELECT *
FROM People
WHERE Name LIKE ISNULL(@Var, Name) + '%';
--Using ISNULL() will return all rows if you pass NULL to the stored procedure
END
并将其命名为
EXEC MyProc N'حس'; --If you don't use N prefix then you are pass a varchar string
如果你看到,当你将文字字符串传递给你的 SP 时,你需要使用 N
前缀,而不是在 SP 内部或 WHERE
子句中。
在这些行中
declare @t nvarchar(20)
set @t='حس'
'حس'
是一个 varchar 常量,然后您将其分配给 nvarchar 变量。但是您已经丢失了原始转换为该 varchar 常量的数据,并且您无法取回它。
解决方案是使用 nvarchar 常量:
set @t=N'حس'
可能会更简单:
试试这个
declare @t nvarchar(20)
set @t='حس';
SELECT @t; --the result is "??"
您将变量声明为 NVARCHAR
是正确的。但是 literal 不知道它的目标。如果没有 N
,它将被视为具有默认排序规则的 VARCHAR
。
下面一行
Where [pName] like N''+@t +'%'
将搜索 pName LIKE '??%'
。
解决方案应该是
set @t=N'حس'; --<-- N-prefix
有什么方法可以使以下查询有效吗?
declare @t nvarchar(20)
set @t='حس'
SELECT [perno] ,[pName]
FROM [dbo].[People]
Where [pName] like N''+@t +'%'
我不能这样使用:
Where [pName] like N'حس%'
或者使用存储过程:
ALTER PROCEDURE [dbo].[aTest]
(@t nvarchar(20))
AS
BEGIN
SELECT [perno] ,[pName]
FROM [dbo].[People]
WHERE ([People].[pName] LIKE N'' +@t + '%')
END
您不需要在 WHERE
子句中使用 N
前缀,因为您的变量已经是 nvarchar
,并且您传递的变量不是 文字字符串.
这是一个例子:
CREATE TABLE People
(
ID INT,
Name NVARCHAR(45)
);
INSERT INTO People VALUES
(1, N'حسام'),
(2, N'حسان'),
(3, N'حليم');
DECLARE @Name NVARCHAR(45) = N'حس';--You need to use N prefix when you pass the string literal
SELECT *
FROM People
WHERE Name LIKE @Name + '%'; --You can use it here when you pass string literal, but since you are passing a variable, you don't need N here
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.
来自docs
Prefix Unicode character string constants with the letter N. Without the N prefix, the string is converted to the default code page of the database. This default code page may not recognize certain characters.
为了简单回答评论中的问题,您使用了错误的数据类型,因此 ALTER
存储过程并将参数的数据类型从 VARCHAR
更改为 NVARCHAR
.
更新:
由于您使用的是 SP,您可以创建您的 SP(根据您的
CREATE PROCEDURE MyProc
(
@Var NVARCHAR(45)
)
AS
BEGIN
SELECT *
FROM People
WHERE Name LIKE ISNULL(@Var, Name) + '%';
--Using ISNULL() will return all rows if you pass NULL to the stored procedure
END
并将其命名为
EXEC MyProc N'حس'; --If you don't use N prefix then you are pass a varchar string
如果你看到,当你将文字字符串传递给你的 SP 时,你需要使用 N
前缀,而不是在 SP 内部或 WHERE
子句中。
在这些行中
declare @t nvarchar(20)
set @t='حس'
'حس'
是一个 varchar 常量,然后您将其分配给 nvarchar 变量。但是您已经丢失了原始转换为该 varchar 常量的数据,并且您无法取回它。
解决方案是使用 nvarchar 常量:
set @t=N'حس'
可能会更简单:
试试这个
declare @t nvarchar(20)
set @t='حس';
SELECT @t; --the result is "??"
您将变量声明为 NVARCHAR
是正确的。但是 literal 不知道它的目标。如果没有 N
,它将被视为具有默认排序规则的 VARCHAR
。
下面一行
Where [pName] like N''+@t +'%'
将搜索 pName LIKE '??%'
。
解决方案应该是
set @t=N'حس'; --<-- N-prefix