当由sp_MSforeachdb函数执行时,NOT LIKE子句不起作用

时间:2016-03-11 16:28:28

标签: sql-server-2008 tsql

当我执行下面的代码时,我仍然在ObjName列中查看以' x'开头的值。值。我不明白为什么。我也尝试使用以下LIKE子句,但无济于事:

AND o.name NOT LIKE'' x%''

如何纠正我的问题的任何帮助/指示将不胜感激。谢谢你的时间。请参阅下面的图片以及我的一个结果集。

NotLikeClauseIssue

这是我的代码:

SET NOCOUNT ON;
DECLARE @text NVARCHAR(128)
DECLARE @text1 NVARCHAR(128)
DECLARE @text2 NVARCHAR(5)
DECLARE @sql NVARCHAR(MAX)
SET @text = 'DBSQLSERVER.' -- ENTER THE SQL SERVER BEING SEARCHED FOR.
SET @text1 = 'DBSQLSERVER].' -- ENTER THE SQL SERVER BEING SEARCHED FOR.
SET @text2 = 'x'                                                    --                                                                                                                                          |
SET @sql =                                                                          --                                                                                                                                          |
                                                                                                                    --                                                                                                                                          V           
                'USE [?]                                                                    
                SELECT ''?'' [DBName], @@SERVERNAME AS [WorkingServer], ''DBSQLSERVER'' AS [ServerToSearch], o.name AS [ObjName], o.type_desc [TypeDesc]
                FROM sys.sql_modules m 
                INNER JOIN sys.objects o 
                ON m.object_id = o.object_id
                WHERE m.definition LIKE ' + CHAR(39) + '%' + @text + '%' + CHAR(39) + '
                OR m.definition LIKE ' + CHAR(39) + '%' + @text1 + '%' + CHAR(39) + '
                AND o.name NOT LIKE '+ CHAR(39) + @text2 + '%' + CHAR(39) + '
                ORDER BY o.name asc'                
EXEC sp_MSforeachdb @sql;   

1 个答案:

答案 0 :(得分:1)

SELECT ''?'' [DBName], @@SERVERNAME AS [WorkingServer], ''DBSQLSERVER'' AS [ServerToSearch], o.name AS [ObjName], o.type_desc [TypeDesc]
FROM sys.sql_modules m 
INNER JOIN sys.objects o 
       ON m.object_id = o.object_id
WHERE o.name NOT LIKE ' + CHAR(39) + @text2 + '%' + CHAR(39) + ' 
  AND (    m.definition LIKE ' + CHAR(39) + '%' + @text  + '%' + CHAR(39) + '
        OR m.definition LIKE ' + CHAR(39) + '%' + @text1 + '%' + CHAR(39) + '
      ) 
ORDER BY o.name asc'