无法更新 sys.columns - 还有其他方法吗?

发布于 2024-09-28 16:15:13 字数 772 浏览 6 评论 0原文

我刚刚发现,由于我在具有不同排序规则的不同服务器上创建了数据库,然后迁移到具有新排序规则的新服务器,现在我继续遇到麻烦,我决定更改旧排序规则的所有值。

所以我尝试执行这个查询:

sp_configure "Allow Updates", 1
GO
RECONFIGURE WITH OVERRIDE
GO

UPDATE sys.columns SET collation_name = 
    'SQL_Latin1_General_CP1_CI_AS' WHERE collation_name = 'Hebrew_CI_AS'
GO

sp_configure "Allow Updates", 0
GO
RECONFIGURE
GO

但这里是查询的输出:

Configuration option 'allow updates' changed from 0 to 1. 
    Run the RECONFIGURE statement to install.

Msg 259, Level 16, State 1, Line 2
Ad hoc updates to system catalogs are not allowed.

Configuration option 'allow updates' changed from 1 to 0. 
    Run the RECONFIGURE statement to install.

UPDATE
根据下面的答案,我只是在寻找一种自动化的方式来执行该操作。

I just found out, that since I created my DB on a different server that had a different collation, then immigrated to a new server with new collation, now I keep on having trouble, and I decided to change all the values of the old collation.

So I tried to execute this query:

sp_configure "Allow Updates", 1
GO
RECONFIGURE WITH OVERRIDE
GO

UPDATE sys.columns SET collation_name = 
    'SQL_Latin1_General_CP1_CI_AS' WHERE collation_name = 'Hebrew_CI_AS'
GO

sp_configure "Allow Updates", 0
GO
RECONFIGURE
GO

But here is the output of the query:

Configuration option 'allow updates' changed from 0 to 1. 
    Run the RECONFIGURE statement to install.

Msg 259, Level 16, State 1, Line 2
Ad hoc updates to system catalogs are not allowed.

Configuration option 'allow updates' changed from 1 to 0. 
    Run the RECONFIGURE statement to install.

UPDATE
According to the answers bellow, I am just looking for an automated way to perform the action.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吃→可爱长大的 2024-10-05 16:15:13

您需要发出 ALTER TABLE 命令才能更改特定列的排序规则。

例如,

ALTER TABLE YourTable 
ALTER COLUMN ColA VARCHAR(10) COLLATE Latin1_General_CI_AS NOT NULL

查看此 MSDN 参考

You need to issue ALTER TABLE commands in order to change the collations of the specific columns.

e.g.

ALTER TABLE YourTable 
ALTER COLUMN ColA VARCHAR(10) COLLATE Latin1_General_CI_AS NOT NULL

Check out this MSDN reference

︶葆Ⅱㄣ 2024-10-05 16:15:13

您可以使用 alter table 更改每列的排序规则。

更改数据库排序规则的唯一方法是删除并重新创建数据库。

You can use alter table to change the collation for each column.

The only way to change a database collation is to drop and recreate the database.

深者入戏 2024-10-05 16:15:13

此查询生成一个脚本,该脚本会将所有 Hebrew_CI_AS 排序列交换为 Latin1_General_CI_AS 排序规则:

DECLARE @Script varchar(MAX)
SET @Script = CAST((
SELECT 'ALTER TABLE [' + Tables.Name + '] ALTER COLUMN [' + 
  [Columns].Name + '] ' + Types.Name + '(' + 
  CAST([Columns].max_length AS varchar) + ') COLLATE Latin1_General_CI_AS ' + 
  CASE WHEN [Columns].is_nullable = 0 THEN 'NOT ' ELSE '' END + 'NULL '
FROM sys.tables Tables INNER JOIN sys.all_columns [Columns]
  ON [Tables].[object_id] = [Columns].[object_id]
  INNER JOIN sys.types Types ON [Columns].system_type_id = Types.system_type_id
WHERE [Columns].collation_name = 'Hebrew_CI_AS'
FOR XML PATH('')
) AS varchar(MAX))

PRINT @Script
EXEC(@Script)

注意: 对于索引/约束的列,您将需要手动编辑,但这也是因为执行上述查询时错误结果(对于约束等)包含表 &列名称,您必须承认它仍然比手动完成所有操作要好。

This query is generates a script that will swap all Hebrew_CI_AS collated columns to Latin1_General_CI_AS collation:

DECLARE @Script varchar(MAX)
SET @Script = CAST((
SELECT 'ALTER TABLE [' + Tables.Name + '] ALTER COLUMN [' + 
  [Columns].Name + '] ' + Types.Name + '(' + 
  CAST([Columns].max_length AS varchar) + ') COLLATE Latin1_General_CI_AS ' + 
  CASE WHEN [Columns].is_nullable = 0 THEN 'NOT ' ELSE '' END + 'NULL '
FROM sys.tables Tables INNER JOIN sys.all_columns [Columns]
  ON [Tables].[object_id] = [Columns].[object_id]
  INNER JOIN sys.types Types ON [Columns].system_type_id = Types.system_type_id
WHERE [Columns].collation_name = 'Hebrew_CI_AS'
FOR XML PATH('')
) AS varchar(MAX))

PRINT @Script
EXEC(@Script)

NOTE: For columns that are indexed/constrained you'll need to edit manually, but that's also something since when executing the above query the error-result (for a constraint etc.) contains the table & column name, you must admit it's still better than do everything manually.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文