如何在tsql中将N个字符与变量相加?
如何在tsql中将N个字符与变量相加 此sql查询返回正确的结果如何在tsql中将N个字符与变量相加?,sql,sql-server-2008,tsql,Sql,Sql Server 2008,Tsql,如何在tsql中将N个字符与变量相加 此sql查询返回正确的结果 select * from PersonsInfoTbl where Name LIKE N'علی%'; declare @Name nvarchar(50) set @Name = 'علی'; select * from PersonsInfoTbl where Name LIKE N''+@Name+'%'; 此sql查询返回不正确的结果 select * from PersonsInfoTbl where Na
select * from PersonsInfoTbl where Name LIKE N'علی%';
declare @Name nvarchar(50)
set @Name = 'علی';
select * from PersonsInfoTbl where Name LIKE N''+@Name+'%';
此sql查询返回不正确的结果
select * from PersonsInfoTbl where Name LIKE N'علی%';
declare @Name nvarchar(50)
set @Name = 'علی';
select * from PersonsInfoTbl where Name LIKE N''+@Name+'%';
我的数据库是Microsoft SQL Server 2008 R2如果您设置变量@Name而不使用N“”,则SQL必须知道您将其设置为nvarchar
declare @Name nvarchar(50)
set @Name = N'علی';
select * from @t where Name LIKE N''+@Name+'%';
这将返回行如果您设置变量@Name而不使用N'',那么sql必须知道您将其设置为nvarchar
declare @Name nvarchar(50)
set @Name = N'علی';
select * from @t where Name LIKE N''+@Name+'%';
这将返回行添加另一个变量以将SQL命令存储为字符串。此外,您还需要在变量和命令上使用unicode N,以便文本也存储为unicode
declare @Name nvarchar(50)
declare @sqlcmd nvarchar(max)
set @Name = N'علی';
set @sqlcmd = N'select * from PersonsInfoTbl where Name LIKE '''+@Name+'%'''
EXECUTE(@sqlcmd)
编辑:我认为在这种特定情况下不需要动态部分,但您会发现这在将来编写查询时很有用。添加另一个变量以将SQL命令存储为字符串。此外,您还需要在变量和命令上使用unicode N,以便文本也存储为unicode
declare @Name nvarchar(50)
declare @sqlcmd nvarchar(max)
set @Name = N'علی';
set @sqlcmd = N'select * from PersonsInfoTbl where Name LIKE '''+@Name+'%'''
EXECUTE(@sqlcmd)
编辑:我认为在这种特定情况下不需要动态部分,但您会发现这在将来编写查询时很有用。您必须在任何nvarchar字符串文本前面加上N。如果不这样做,SQL Server将假定它不是nvarchar 例如:
declare @Name1 nvarchar(50), @Name2 nvarchar(50)
set @Name1 = N'علی';
set @Name2 = 'علی';
select @Name1 "Name1", @Name2 "Name2"
将返回此文件:
Name1 Name2
-------- --------
علی ???
试试这个:
declare @Name nvarchar(50)
set @Name = N'علی';
select * from PersonsInfoTbl where Name LIKE @Name + N'%';
您必须在任何nvarchar字符串文字前面加上N。如果不这样做,SQL Server将假定它不是nvarchar 例如:
declare @Name1 nvarchar(50), @Name2 nvarchar(50)
set @Name1 = N'علی';
set @Name2 = 'علی';
select @Name1 "Name1", @Name2 "Name2"
将返回此文件:
Name1 Name2
-------- --------
علی ???
试试这个:
declare @Name nvarchar(50)
set @Name = N'علی';
select * from PersonsInfoTbl where Name LIKE @Name + N'%';