将希腊字符插入到varchar列中,并使用Cyrillic排序。

huangapple go评论59阅读模式
英文:

Greek characters into varchar column and with Cyrillic colation

问题

我有SQL Server 2019。如果我有一个带有西里尔文排序规则和一个varchar列的表,我找不到一种方法来插入希腊字母:

CREATE TABLE dbo.tmp (
    id int NOT NULL,
    name varchar(75) collate Cyrillic_General_CI_AS
)

INSERT INTO dbo.tmp (id, name)
VALUES (1, N'Εργ Μας');

SELECT *
FROM dbo.tmp;

然后结果是

??? ???

我需要知道是否有解决方案可以在保持varchar类型和西里尔文排序规则的情况下进行插入。或者这些参数与希腊字母不兼容吗?

英文:

I have SQL Server 2019. If I have a table with Cyrillic collation and a varchar column I can't find a way to insert Greek letters:

CREATE TABLE dbo.tmp (
    id int NOT NULL
    , name varchar(75) collate Cyrillic_General_CI_AS
)

INSERT INTO dbo.tmp (id, name)
VALUES (1, N'Εργ Μας');

SELECT *
FROM dbo.tmp;

Then the result is

??? ???

I need to know is there a solution to make the insert while keeping the varchar type and the Cyrillic collation. Or these parameters are incompatible with Greek letters?

答案1

得分: 1

抱歉,我只返回翻译好的部分,不提供其他内容。

"I am affraid there's no other solution than change of the DB collation to Greek_CI_AS if you want to store Greek characters."

  • "恐怕没有其他解决办法,除非将数据库排序规则更改为Greek_CI_AS,如果您想存储希腊字符。"

"Also you can change it to one of UTF collations. Change it to LATIN1_GENERAL_100_CI_AS_SC to LATIN1_GENERAL_100_CI_AS_SC_UTF8. This one should support both of the character sets."

  • "您还可以将其更改为其中一个UTF排序规则。将其更改为LATIN1_GENERAL_100_CI_AS_SCLATIN1_GENERAL_100_CI_AS_SC_UTF8。这个应该支持两种字符集。"

"Here you can find a good article how change your collation: https://learn.microsoft.com/en-us/sql/relational-databases/collations/set-or-change-the-column-collation?view=sql-server-ver16"

  • "在这里,您可以找到一篇关于如何更改排序规则的好文章:https://learn.microsoft.com/en-us/sql/relational-databases/collations/set-or-change-the-column-collation?view=sql-server-ver16"

"You can use in-place conversion or copy."

  • "您可以使用原地转换或复制。"

"In-place conversion."

  • "原地转换。"

"To convert the column in-place to use UTF-8, run an ALTER COLUMN statement that sets the required data type and a UTF-8 enabled collation:"

  • "要原地将列转换为使用UTF-8,请运行ALTER COLUMN语句,设置所需的数据类型和启用UTF-8的排序规则:"

"This method is easy to implement, however it's a possibly blocking operation which may become an issue for large tables and busy applications."

  • "这种方法容易实施,但可能是一个可能阻塞的操作,对于大表和繁忙的应用程序可能会成为问题。"

"Copy and replace. Consider one of the existing tables defined below:"

  • "复制和替换。考虑下面定义的现有表之一:"

"To convert the column to use UTF-8, copy the data to a new table where the target column is already the required data type and a UTF-8 enabled collation, and then replace the old table:"

  • "要将列转换为使用UTF-8,请将数据复制到一个新表中,其中目标列已经是所需的数据类型和启用UTF-8的排序规则,然后替换旧表:"

"This method is much faster than in-place conversion, but handling complex schemas with many dependencies (FKs, PKs, Triggers, DFs) and syncing the tail of the table (if the database is in use) requires more planning."

  • "这种方法比原地转换快得多,但处理具有许多依赖关系(FK、PK、触发器、DF等)的复杂架构以及同步表尾部(如果数据库正在使用)需要更多的计划。"
英文:

I am affraid there's no other solution than change of the DB collation to Greek_CI_AS if you want to store Greek characters.

Also you can change it to one of UTF collations. Change it to LATIN1_GENERAL_100_CI_AS_SC to LATIN1_GENERAL_100_CI_AS_SC_UTF8. This one should support both of the character sets.

Here you can find a good article how change your collation: https://learn.microsoft.com/en-us/sql/relational-databases/collations/set-or-change-the-column-collation?view=sql-server-ver16

You can use in-place conversion or copy.

In-place conversion.

-- NVARCHAR column is encoded in UTF-16 because a supplementary character enabled collation is used
CREATE TABLE dbo.MyTable (CharCol NVARCHAR(50) COLLATE Latin1_General_100_CI_AI_SC);

-- VARCHAR column is encoded the Latin code page and therefore is not Unicode capable
CREATE TABLE dbo.MyTable (CharCol VARCHAR(50) COLLATE Latin1_General_100_CI_AI);

To convert the column in-place to use UTF-8, run an ALTER COLUMN statement that sets the required data type and a UTF-8 enabled collation:

ALTER TABLE dbo.MyTable 
ALTER COLUMN CharCol VARCHAR(50) COLLATE Latin1_General_100_CI_AI_SC_UTF8

This method is easy to implement, however it's a possibly blocking operation which may become an issue for large tables and busy applications.

Copy and replace. Consider one of the existing tables defined below:

-- NVARCHAR column is encoded in UTF-16 because a supplementary character enabled collation is used
CREATE TABLE dbo.MyTable (CharCol NVARCHAR(50) COLLATE Latin1_General_100_CI_AI_SC);
GO

-- VARCHAR column is encoded using the Latin code page and therefore is not Unicode capable
CREATE TABLE dbo.MyTable (CharCol VARCHAR(50) COLLATE Latin1_General_100_CI_AI);
GO

To convert the column to use UTF-8, copy the data to a new table where the target column is already the required data type and a UTF-8 enabled collation, and then replace the old table:

CREATE TABLE dbo.MyTableNew (CharCol VARCHAR(50) COLLATE Latin1_General_100_CI_AI_SC_UTF8);
GO
INSERT INTO dbo.MyTableNew 
SELECT * FROM dbo.MyTable;
GO
DROP TABLE dbo.MyTable;
GO
EXEC sp_rename 'dbo.MyTableNew', 'dbo.MyTable';
GO

This method is much faster than in-place conversion, but handling complex schemas with many dependencies (FKs, PKs, Triggers, DFs) and syncing the tail of the table (if the database is in use) requires more planning.

huangapple
  • 本文由 发表于 2023年6月13日 15:04:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76462403.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定