如何从查询窗口向 SQL Server 插入 unicode 文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1350968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 03:23:16  来源:igfitidea点击:

how to insert unicode text to SQL Server from query window

sqlunicode

提问by ebattulga

I'm using the following code:

我正在使用以下代码:

INSERT INTO tForeignLanguage ([Name]) VALUES ('Араб')

this value inserted like this '????'

这个值像这样插入'????'

How do I insert unicode text from the sql management studio query window?

如何从 sql management studio 查询窗口插入 unicode 文本?

回答by Ian Kemp

The following should work, Nindicates a "Unicode constant string" in MSSQL:

以下应该有效,N表示 MSSQL 中的“Unicode 常量字符串”:

INSERT INTO tForeignLanguage ([Name]) VALUES (N'Араб')

回答by Sisay Nigatu

The perfect solution with data type limitation:

数据类型限制的完美解决方案:

Basically in MS-SQL Server Amharic text not working properly when the column datatype is 'text'.Therefore to put Amharic text on column with datatype text, first change the text datatype to 'nvarchar(MAX)' or just 'nvarchar' with any char length that MS-SQL Server supported.

基本上在 MS-SQL Server 中,当列数据类型为“text”时,阿姆哈拉语文本无法正常工作。因此,要将阿姆哈拉语文本放在数据类型为文本的列上,首先将文本数据类型更改为“ nvarchar(MAX)”或“ nvarchar” MS-SQL Server 支持的字符长度。

回答by A. Nadjar

Thanks to Ian's answer, you can directly run this code from query window:

感谢 Ian 的回答,您可以直接从查询窗口运行此代码:

declare @FamilyName nvarchar(40)
set @FamilyName = N'嗄嗄嗄'

insert into table(LoginName, Password)  select @FamilyName as LoginName, 123 as Password

However, if you wish to perform the above insert through stored procedure, it's needed to attach Nas prefix:

但是,如果您希望通过存储过程执行上述插入,则需要附加N为前缀:

CREATE PROCEDURE Example
    @FAMILY_NAME   NVARCHAR(40)
AS
BEGIN

    SET NOCOUNT ON;
    declare @query nvarchar(400);


    set @query  ='insert into table(LoginName, Password)  select N'''+ @FAMILY_NAME +''' as LoginName, 123 as Password';

    EXECUTE sp_executesql @query;

END

Hope this helps..

希望这可以帮助..

回答by Hedego

In my case, the task at hand was to update an SQL table which contains list of countries in two languages in which the local language (Amharic) column was null so executing the following works fine.

就我而言,手头的任务是更新一个 SQL 表,其中包含两种语言的国家/地区列表,其中本地语言(阿姆哈拉语)列为空,因此执行以下工作正常。

    Update [tableName] set [columnName] = N'????'

The N in N'????' is the key to put the string as it is in your specific column.

N' 中的 N'????' 是将字符串放在特定列中的关键。

回答by Ashish

Just make datatype NVarcharin database and following;

只需NVarchar在数据库中创建数据类型并遵循;

internal string InsertUpdate(classname obj)
{
    SqlParameter[] sqlparam = new SqlParameter[];
    sqlparam[] = new SqlParameter("@DESC1", SqlDbType.NVarChar);
    sqlparam[].Value = NullHandler.String(obj.DESC1);
    sqlparam[] = new SqlParameter("@DESC2", SqlDbType.NVarChar);
    sqlparam[].Value = NullHandler.String(obj.DESC2);
    obj.InsertUpdateTable("spname", "sp", sqlparam);
    if (sqlparam[].Value != DBNull.Value)
        return sqlparam[].Value.ToString();
}