不久前我遇到了一个类似的问题,我不得不quoted identifier
在几个数据库中的数百个程序中找到并修复不正确的设置以及许多其他问题。任务的一部分是查找并删除任何显式设置选项,例如quoted_identifer
,以及许多其他常见问题,包括在许多使用动态 sql 并设置为 off的遗留过程中ansi_nulls
transaction isolation level
删除引号的使用。"
quoted_identifer
这是我的解决方案的核心,您可能希望根据自己的要求进行修改。
我将所有相关对象的 sql 文本 - 过程、函数等放入临时表中,然后进行各种字符串替换以解决特定问题。然后我遍历并使用动态 sql 来重新创建对象,这自然会得到正确的 default set options
。任何失败的都在表中列出,然后我手动处理。
select m.definition, Cast(o.name as varchar(1000)) [name], m.object_id, 0 Done, 0 Success , o.type
--into #working
from sys.sql_modules m join sys.objects o on o.object_id=m.object_id and o.type in ('FN', 'P', 'V', 'TR')
where m.uses_quoted_identifier=0
update #working set definition=Replace(definition, 'create function', 'create or alter function')
update #working set definition=Replace(definition, 'create view', 'create or alter view')
update #working set definition=Replace(definition, 'create trigger', 'create or alter trigger')
update #working set definition=Replace(definition, 'create proc', 'create or alter proc')
update #working set definition=Replace(definition, 'create proc', 'create or alter proc')
update #working set definition=Replace(definition, 'create proc', 'create or alter proc')
/*
update #working set definition=Replace(definition, 'set ansi_nulls off', '')
update #working set definition=Replace(definition, 'set ansi_warnings off', '')
update #working set definition=Replace(definition, 'set quoted_identifier off', '')
update #working set definition=Replace(definition, '(nolock)', '')
update #working set definition=Replace(definition, 'set transaction isolation level read uncommitted', '')
*/
select * from #working
declare @Sql nvarchar(max), @Id int
while exists (select * from #working where Done=0)
begin
select top (1) @Sql=definition, @Id=object_id from #working where Done=0
update #working set Done=1, Success=1 where object_id=@Id
begin try
exec (@Sql)
end try
begin catch
update #working set Success=Error_Number(), name=Error_Message() where object_id=@Id
end catch
end