是否可以同时更改服务器所有数据库的所有表的排序规则?
我找到了很多脚本但是只有一个数据库。我们有100多个数据库,我需要对所有数据库使用唯一的排序规则。
我们可以使用某种脚本列出吗?
感谢。
(抱歉英语不好)
Obs1:如果可能,我也需要更改列排序规则。我们需要对所有内容进行一次整理!
谢谢朋友们。
编辑:
这里我有一个列出" all"数据库的表,它显示了结果中的代码。
我需要做一些循环,因为我需要的是改变所有数据库的所有表和列。
我只能在一个独特的数据库中运行此代码。
DECLARE @collate SYSNAME
SELECT @collate = 'Latin1_General_CI_AS'
SELECT
'[' + SCHEMA_NAME(o.[schema_id]) + '].[' + o.name + '] -> ' + c.name
, 'ALTER TABLE [' + SCHEMA_NAME(o.[schema_id]) + '].[' + o.name + ']
ALTER COLUMN [' + c.name + '] ' +
UPPER(t.name) +
CASE WHEN t.name NOT IN ('ntext', 'text')
THEN '(' +
CASE
WHEN t.name IN ('nchar', 'nvarchar') AND c.max_length != -1
THEN CAST(c.max_length / 2 AS VARCHAR(10))
WHEN t.name IN ('nchar', 'nvarchar') AND c.max_length = -1
THEN 'MAX'
ELSE CAST(c.max_length AS VARCHAR(10))
END + ')'
ELSE ''
END + ' COLLATE ' + @collate +
CASE WHEN c.is_nullable = 1
THEN ' NULL'
ELSE ' NOT NULL'
END
FROM sys.columns c WITH(NOLOCK)
JOIN sys.objects o WITH(NOLOCK) ON c.[object_id] = o.[object_id]
JOIN sys.types t WITH(NOLOCK)
ON c.system_type_id = t.system_type_id AND c.user_type_id = t.user_type_id
WHERE t.name IN ('char', 'varchar', 'text', 'nvarchar', 'ntext', 'nchar')
AND c.collation_name != @collate
AND o.[type] = 'U'
EDIT2:
我在互联网上搜索,我找到了这个神奇的过程:SP_MSFOREACHDB。
我正在使用sp_helptext来了解我如何在proc中使用它。但又一次没有成功。
EDIT 3: FINALLY I DID IT !!!
这是我做的过程:
DECLARE @collate SYSNAME
SELECT @collate = 'Latin1_General_CI_AS'
declare @cmd as nvarchar(4000)
declare @banco as varchar(100)
-- Bancos que serão consultados
Select name into #tmp from master.sys.databases Where name not in ('master', 'model', 'msdb',
'tempdb')
--while
-- Loop pelos bancos
While (Select count(1) from #tmp) > 0
begin
Select @banco = min(name) from #tmp
set @cmd = '
use [' + @banco + ' ]
insert into collate_adm.dbo.tblCollateScript
SELECT ''?'' as Banco,
''['' + SCHEMA_NAME(o.[schema_id]) + ''].['' + o.name + ''] -> '' + c.name
, '' use [' + @banco + ' ] ALTER TABLE ['' + SCHEMA_NAME(o.[schema_id]) + ''].['' +
o.name + '']
ALTER COLUMN ['' + c.name + ''] '' +
UPPER(t.name) +
CASE WHEN t.name NOT IN (''ntext'', ''text'')
THEN ''('' +
CASE
WHEN t.name IN (''nchar'', ''nvarchar'') AND c.max_length !=
-1
THEN CAST(c.max_length / 2 AS VARCHAR(10))
WHEN t.name IN (''nchar'', ''nvarchar'') AND c.max_length =
-1
THEN ''MAX''
ELSE CAST(c.max_length AS VARCHAR(10))
END + '')''
ELSE ''''
END + '' COLLATE Latin1_General_CI_AS '' +
CASE WHEN c.is_nullable = 1
THEN '' NULL''
ELSE '' NOT NULL''
END
FROM sys.columns c WITH(NOLOCK)
JOIN sys.objects o WITH(NOLOCK) ON c.[object_id] = o.[object_id]
JOIN sys.types t WITH(NOLOCK) ON c.system_type_id = t.system_type_id AND c.user_type_id =
t.user_type_id
WHERE t.name IN (''char'', ''varchar'', ''text'', ''nvarchar'', ''ntext'', ''nchar'')
AND c.collation_name != ''Latin1_General_CI_AS''
AND o.[type] = ''U''
'
set @cmd = replace(replace(@cmd,'?', @banco) , 'XXXcollateXXX', @collate)
begin try
exec sp_executeSQL @cmd -- Executa comando gerado pelo script
end try
begin catch
insert into tblCollateScript (rotina, script,Data) values ('pr_BuscaCotas', @cmd,
GETDATE())
end catch
Delete from #tmp Where name = @banco
end
drop table #tmp
end
谢谢大家的一切!