如何不丢失存储过程参数中的unicode字符

How to not lose unicode characters in Stored Procedure parameter

问题陈述:
我们有一个带有后端作为 SQL 服务器的遗留应用程序。到目前为止,我们在传递非 unicode 值时没有遇到任何问题。现在,我们正在从用户界面获取 unicode 字符。

Unicode 字符如下所示在 UI 中传递。正在将这些数据插入 table.

目前,我们像下面这样传递 unicode 字符,我们正在丢失非英语字符。

EXEC dbo.ProcedureName @IN_DELIM_VALS = '삼성~AX~Aland Islands~ALLTest1~Aland Islands~~~~'

我们尝试了什么:
如果我们传递带有 N 前缀的 unicode,非英语字符将正确插入 table。

EXEC dbo.ProcedureName @IN_DELIM_VALS = N'삼성~AX~Aland Islands~ALLTest1~Aland Islands~~~~'

但是,添加 N 前缀需要 UI 更改代码。由于它是遗留应用程序,我们希望避免 UI 更改。我们想在sql服务器端处理。

当我阅读有关传递不带 N 前缀的参数时,数据被隐式转换为默认代码页并且韩文字符丢失。 Reference

Prefix a Unicode character string constants with the letter N to signal UCS-2 or UTF-16 input, depending on whether an SC collation is used or not. Without the N prefix, the string is converted to the default code page of the database that may not recognize certain characters. Starting with SQL Server 2019 (15.x), when a UTF-8 enabled collation is used, the default code page is capable of storing UNICODE UTF-8 character set.

我们的提问:
有没有办法在分配给存储过程参数之前向存储过程参数添加 N 前缀,这样我们就不会丢失 unicode 字符。

由于无法添加 N 前缀,在将参数传递给 SQL 服务器后,我们将进行以下应用程序代码更改。理想情况下,应用程序应该传递具有正确 nvarchar 数据类型的参数,以便它具有 N 前缀。

SqlCommand cmd  = new SqlCommand("EXEC dbo.ProcedureName @IN_DELIM_VALS", myConnection);
cmd.Parameters.Add(new SqlParameter("@IN_DELIM_VALS", SqlDbType.NVarChar,400)).Value 
  = "삼성~AX~Aland Islands~ALLTest1~Aland Islands~~~~";
cmd.ExecuteNonQuery();