Sql server 如何在SQLServer中使用动态查询存储非英语unicode字符

Sql server 如何在SQLServer中使用动态查询存储非英语unicode字符,sql-server,Sql Server,我想将非英语unicode字符保存到SQL Server中的表中 declare @cSql varchar(200) set @cSql = '' declare @cCode varchar(10) set @cCode = '001' declare @cKName Nvarchar(200) set @cKName = N'ಒಂದು' Insert into DepMast (omCode, omKName) Values(@cCode, @cKName) 在执行上述代码时,将根

我想将非英语unicode字符保存到SQL Server中的表中

declare @cSql varchar(200)
set @cSql = ''

declare @cCode varchar(10)
set @cCode = '001'

declare @cKName Nvarchar(200)
set @cKName = N'ಒಂದು'

Insert into DepMast (omCode, omKName)
Values(@cCode, @cKName)
在执行上述代码时,将根据需要保存unicode字符

但是,如果使用动态查询,则会存储
“???”

set @cSql = 'Insert into DepMast (omCode, omKName)
             Values(''' + @cCode + ''',''' + @cKName + ''')'

exec(@cSql)

关于使用动态查询存储unicode字符有什么想法吗?

这种行为的两个原因-首先,您将
@cSql
声明为
varchar
数据类型,将
@cKName
声明为
nvarchar
数据类型。其次,在插入时,您没有在值前面加上
N
。这应该可以解决问题

declare @cSql nvarchar(200) --changed datatype to nvarchar
set @cSql=''
declare @cCode varchar(10)
set @cCode='001'
declare @cKName nvarchar(200)
set @cKName=N'ಒಂದು' 
set @cSql='Insert into DepMast (omCode,omKName)
Values('''+@cCode+''',N'''+@cKName+''')' -- added the "N" prefix
exec(@cSql)

在动态查询中使用Insert Into Select语句。您需要使用键值将Unicode字符存储在某个参考表中。

您的数据库排序规则是什么?它是SQL\u拉丁1\u General\u CP1\u CI\u AS