如何查找使用 SET ANSI_NULLS OFF 创建的例程?

How to find routines that are created with SET ANSI_NULLS OFF?

我已经创建了一个过滤的非聚集索引以优化特定的查询集,但是我开始从各种来源得到以下错误:

UPDATE failed because the following SET options have incorrect settings: 'ANSI_NULLS'. Verify that SET options are correct for use with indexed views and/or indexes on computed columns and/or filtered indexes and/or query notifications and/or XML data type methods and/or spatial index operations.

似乎一些遗留例程是使用 SET ANSI_NULLS OFF 选项创建的,当引擎尝试从给定上下文更新目标 table 时,会抛出错误。

我想知道有没有办法查看使用此选项创建了哪些例程。例如,如果您编写这样的例程,您会得到如下内容:

SET ANSI_NULLS OFF
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE ....

我通常使用像下面这样的脚本在对象定义中找到东西,但这个设置不是它的一部分:

DECLARE @SearchWord NVARCHAR(128) = 'SET ANSI_NULLS OFF' 
SELECT [ROUTINE_NAME] 
FROM [INFORMATION_SCHEMA].[ROUTINES]
WHERE [ROUTINE_DEFINITION] LIKE '%' + @SearchWord+'%' 
UNION
SELECT OBJECT_NAME([id]) 
FROM [SYSCOMMENTS]
WHERE [text] LIKE '%' + @SearchWord + '%' 
GROUP BY OBJECT_NAME([id])
UNION 
SELECT OBJECT_NAME(object_id)
FROM [sys].[sql_modules]
WHERE [definition] LIKE '%' + @SearchWord + '%' ;

您可以只安装 SQL Search from Redgate 或其他供应商的类似产品。这样搜索起来就很方便了

对于 one-off,类似于

SELECT * FROM sys.sql_modules WHERE definition LIKE '% ansi %'

应该这样做。或者

SELECT * FROM sys.sql_modules WHERE definition uses_ansi_nulls = 1

sys.sql_modules (Transact-SQL)

一个简单的搜索就可以做到这一点,例如这样

SELECT DISTINCT
       o.name AS Object_Name,
       o.type_desc,
       m.*
FROM   sys.sql_modules m
  INNER JOIN sys.objects o ON m.object_id = o.object_id
WHERE  m.uses_ansi_nulls = 0