在SSMS中,如何找到包含星号,某些文本,然后是另一个星号的字符串?

时间:2016-11-14 17:03:39

标签: sql sql-server tsql ssms

我们有一个包含大量SP和视图的数据库,其中大多数都很棒,但有些标准具有狡猾的标准(以前的员工使用翻译程序从MS Access转移)。

例如,有一些带有critera的SP:

WHERE dbo.something NOT LIKE '*TEST*'

设置此内容的人意味着:

WHERE dbo.something NOT LIKE '%TEST%'

我发现有一些人已经在使用这种方法,但我必须输入确切的文字。用%Character替换单词TEST不起作用(%返回0条记录,TEST返回一些记录)。

USE [databasename]
SELECT @SEARCHSTRING = '*TEST*', @notcontain = ''

SELECT DISTINCT sysobjects.name AS [Object Name] 
,case when sysobjects.xtype = 'P' then 'Stored Proc'
when sysobjects.xtype = 'TF' then 'Function'
when sysobjects.xtype = 'TR' then 'Trigger'
when sysobjects.xtype = 'V' then 'View'
end as [Object Type]
,USER_NAME(sysobjects.uid) AS trigger_owner 
--,s.name AS table_schema 
,OBJECT_NAME(parent_obj) AS table_name 
FROM sysobjects,syscomments
WHERE sysobjects.id = syscomments.id
AND sysobjects.type in ('P','TF','TR','V')
AND sysobjects.category = 0
AND CHARINDEX(@SEARCHSTRING,syscomments.text)>0
AND ((CHARINDEX(@notcontain,syscomments.text)=0 
or CHARINDEX(@notcontain,syscomments.text)<>0)) 

我想知道是否有人知道我可以在所有对象中搜索字符串的方式,其中包含星号,一些文本和另一个星号。 非常感谢

2 个答案:

答案 0 :(得分:0)

这是另一位开发者给我的。也许这会成功吗?

DECLARE @SearchStr nvarchar(100)
SET @SearchStr = 'Coincide'--'Absolute Value of the Difference' --'DOIRteDc' --'Smartin1' -- 'Rgillesp'--'--'## YOUR STRING HERE ##'

-- Copyright © 2002 Narayana Vyas Kondreddi. All rights reserved.
-- Purpose: To search all columns of all tables for a given search string
-- Written by: Narayana Vyas Kondreddi
-- Site: http://vyaskn.tripod.com
-- Updated and tested by Tim Gaunt
-- http://www.thesitedoctor.co.uk
-- http://blogs.thesitedoctor.co.uk/tim/2010/02/19/Search+Every+Table+And+Field+In+A+SQL+Server+Database+Updated.aspx
-- Tested on: SQL Server 7.0, SQL Server 2000, SQL Server 2005 and SQL Server 2010
-- Date modified: 03rd March 2011 19:00 GMT
CREATE TABLE #Results (ColumnName nvarchar(370), ColumnValue nvarchar(3630))

SET NOCOUNT ON

DECLARE @TableName nvarchar(256), @ColumnName nvarchar(128), @SearchStr2 nvarchar(110)
SET  @TableName = ''
SET @SearchStr2 = QUOTENAME('%' + @SearchStr + '%','''')

WHILE @TableName IS NOT NULL

BEGIN
    SET @ColumnName = ''
    SET @TableName = 
    (
        SELECT MIN(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME))
        FROM     INFORMATION_SCHEMA.TABLES
        WHERE         TABLE_TYPE = 'BASE TABLE'
            AND    QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) > @TableName
            AND    OBJECTPROPERTY(
                    OBJECT_ID(
                        QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)
                         ), 'IsMSShipped'
                           ) = 0
    )

    WHILE (@TableName IS NOT NULL) AND (@ColumnName IS NOT NULL)

    BEGIN
        SET @ColumnName =
        (
            SELECT MIN(QUOTENAME(COLUMN_NAME))
            FROM     INFORMATION_SCHEMA.COLUMNS
            WHERE         TABLE_SCHEMA    = PARSENAME(@TableName, 2)
                AND    TABLE_NAME    = PARSENAME(@TableName, 1)
                AND    DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar', 'int', 'decimal')
                AND    QUOTENAME(COLUMN_NAME) > @ColumnName
        )

        IF @ColumnName IS NOT NULL

        BEGIN
            INSERT INTO #Results
            EXEC
            (
                'SELECT ''' + @TableName + '.' + @ColumnName + ''', LEFT(' + @ColumnName + ', 3630) FROM ' + @TableName + ' (NOLOCK) ' +
                ' WHERE ' + @ColumnName + ' LIKE ' + @SearchStr2
            )
        END
    END    
END

SELECT distinct ColumnName, ColumnValue FROM #Results

DROP TABLE #Results

答案 1 :(得分:0)

我知道很久以前就问过这个问题,但是你的问题可能是一个简单的解决方案:::

select sys.objects.name,sys.objects.type_desc, syscomments.id from sys.objects 
inner join
syscomments
    on sys.objects.object_id = syscomments.id
WHERE syscomments.text like '%[^/]*%*[^/]%'    --here the * that is following the / and preceding /has been ignored because i dont think you want to replace the comments that has /* ....... */

希望这可以帮到你!