首页 > 解决方案 > 使用带引号转义的 unicode N 前缀

问题描述

我有一个问题,我需要将 select 语句作为值存储在数据库中的字段中,以便 Web 服务稍后执行它并在不同的应用程序中显示结果。

当我使用表中的 select 语句更新值时转义单引号,我失去了 unicodeN前缀功能。

例子:

update table
set sqlstatement = 'select No as N''Ī'' from table'
where ID = 1

N执行保存的SQL语句时,列名返回为'I'而不是'Ī' 使用前缀时有什么特殊的方法可以转义单引号吗?

标签: sqlsql-server

解决方案


将 N 添加到 'select No as N''Ī'' from table' 的头部

declare @table as table(id int identity(1,1),sqlstatement nvarchar(100))
insert into @table values('select No as N''Ī'' from table') 
--before
select sqlstatement from @table where Id=1

declare @sqlstatement as nvarchar(100)
update @table
set sqlstatement = N'select No as N''Ī'' from table'
where ID = 1

--After
select sqlstatement from @table where Id=1

推荐阅读