Sql server 为多个已安装的过程启用带引号的标识符

Sql server 为多个已安装的过程启用带引号的标识符,sql-server,tsql,quoted-identifier,Sql Server,Tsql,Quoted Identifier,我有数百个过程安装到数据库中,其中引用的标识符设置为off,我需要将它们设置为on。我可以使用以下命令查看这些过程的列表 SELECT name = OBJECT_NAME([object_id]), uses_quoted_identifier FROM sys.sql_modules WHERE uses_quoted_identifier <> 1 AND OBJECT_NAME([object_id]) LIKE '%%' ORDER BY OBJECT_NAME([obje

我有数百个过程安装到数据库中,其中引用的标识符设置为off,我需要将它们设置为on。我可以使用以下命令查看这些过程的列表

SELECT name = OBJECT_NAME([object_id]), uses_quoted_identifier
FROM sys.sql_modules
WHERE uses_quoted_identifier <> 1 AND OBJECT_NAME([object_id]) LIKE '%%'
ORDER BY OBJECT_NAME([object_id])
SELECT name=OBJECT\u name([OBJECT\u id]),使用带引号的标识符
从sys.sql\u模块
其中使用带引号的标识符1和对象名称([OBJECT\u id]),如“%”
按对象名称([OBJECT\u id])排序
现在我知道我不能直接更新sys.sql\u模块来设置uses\u quoted\u标识符。我可以手动打开所有脚本并重新安装,但这很耗时。我也可以创建一个批处理文件来运行相同的过程,但这仍然很耗时,尽管稍微少一些

有没有更简单的方法来更新这些

更新 在做更多研究时,我发现这篇文章让我意识到,引用的标识符都是我自己做的,因为我已经有了一个批处理命令,该命令正在从特定目录安装过程:

我意识到,使用本文,我可以将-I添加到我的sqlcmd中,以启用引用标识符:


我将暂时保留这个问题,以防任何人有一个技巧可以通过编程方式更新过程中引用的标识符,但这应该可以暂时解决我的问题。

不久前,我遇到了一个类似的问题,我不得不在数百个程序中查找并修复不正确的
引用的标识符设置以及许多其他问题多个数据库中的过程。任务的一部分是查找并删除任何显式的集合选项,如
quoted_identifer
ansi_nulls
事务隔离级别
,以及许多其他常见问题,包括在许多使用动态sql且
quoted_identifer
设置为off的遗留过程中删除quotes
的用法

这是我的解决方案的要点,您可能希望根据自己的需求对其进行修改

我将所有相关对象(进程、函数等)的sql文本放入临时表中,然后进行各种字符串替换以修复特定问题。然后我迭代并使用动态sql重新创建对象,这自然会得到正确的默认
设置选项
。表中会指示任何失败的,然后我处理m一年一次

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

在创建存储过程(和用户定义函数)时您应该确保
SET ANSI_NULLS
SET QUOTED_IDENTIFIER
具有正确的设置。如果需要更改设置,则需要删除并重新创建这些设置。在某种源代码控制系统中已经有了这些设置,对吧,因此只需使用正确的设置重新部署它们。