如何在SQL Server的数据库中搜索字符串

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在SQL Server的数据库中搜索字符串相关的知识,希望对你有一定的参考价值。

此查询可以从任何SQL Server数据库的所有表/视图中搜索任何字符串值(表名,表数据等),当您将任何字符串放在“此处的文本”时,它将从所有表中搜索您的给定字符串/视图存在于该数据库中,

DECLARE @SearchStr nvarchar(100) 
    SET @SearchStr = 'your text here'

DECLARE @Results TABLE(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')
                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 ColumnName, ColumnValue FROM @Results

但我喜欢在procedures/functions/triggers中搜索给定的字符串以及SQL Server 2005中是否存在任何查询。由于此查询不在这些区域中搜索。

答案

在过程/函数/触发器中搜索给定的字符串

这实际上要容易得多。

SELECT OBJECT_NAME(object_id), definition
  FROM sys.sql_modules
 WHERE definition LIKE '%'+@SearchStr+'%'

使用它的一种方法是将它添加到SQL代码的末尾,即修改最后的SELECT:

SELECT ColumnName, ColumnValue
  FROM @Results
 UNION ALL
SELECT OBJECT_NAME(object_id), definition
  FROM sys.sql_modules
 WHERE definition LIKE '%'+@SearchStr+'%'

就个人而言,我只是将它们作为单独的陈述一个接一个地分开运行。

另一答案

看看视图sys.sql_modules

或者,在2005年之前的SQL Server版本中,syscomments

另一答案

这也是一个有用的查询,从msDB中找到类似proc等的东西意味着这将在SQL Server Agent的Jobs中找到table / function / proc ref。

USE [msdb] 
GO 
SELECT j.job_id, s.srvname, j.name, js.step_id, js.command, j.enabled 
FROM dbo.sysjobs j 
JOIN dbo.sysjobsteps js ON js.job_id = j.job_id  
JOIN master.dbo.sysservers s     ON s.srvid = j.originating_server_id 
WHERE js.command LIKE N'%YOUR TEXT HERE%' 

在上面查询接受的答案

SELECT OBJECT_NAME(object_id), definition
FROM sys.sql_modules
WHERE definition LIKE '%'+@SearchStr+'%'

只能从所选数据库中查找过程/函数/触发器中的给定文本。

以下查询用于检查SQL Server代理作业的作业状态:

SELECT sj.name
   , sja.*
FROM msdb.dbo.sysjobactivity AS sja
INNER JOIN msdb.dbo.sysjobs AS sj ON sja.job_id = sj.job_id

SELECT TOP 10 * FROM msdb.dbo.sysjobhistory 

查询以查找DB中表的约束详细信息。

SELECT
    FK_Table = FK.TABLE_NAME,
    FK_Column = CU.COLUMN_NAME,
    PK_Table = PK.TABLE_NAME,
    PK_Column = PT.COLUMN_NAME,
    Constraint_Name = C.CONSTRAINT_NAME
FROM
    INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK
    ON C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK
    ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU
    ON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME
INNER JOIN (
            SELECT
                i1.TABLE_NAME,
                i2.COLUMN_NAME
            FROM
                INFORMATION_SCHEMA.TABLE_CONSTRAINTS i1
            INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE i2
                ON i1.CONSTRAINT_NAME = i2.CONSTRAINT_NAME
            WHERE
                i1.CONSTRAINT_TYPE = 'PRIMARY KEY'
           ) PT
    ON PT.TABLE_NAME = PK.TABLE_NAME
    WHERE PT.table_name = 'Table Name'

要检查SQL作业代理是否正在运行作业状态:

use msdb                          
go                         
SELECT  j.name JobName            
, STUFF(STUFF(h.run_date,5,0,'-'),8,0,'-') as Date
, CAST(STR(h.run_date, 8, 0) AS DATETIME) + CAST(STUFF(STUFF(RIGHT('000000' + CAST (h.run_time AS VARCHAR(6)), 6),5, 0, ':'), 3, 0, ':') AS DATETIME) AS StartDate
, STUFF(STUFF(RIGHT(REPLICATE('0', 6) + CAST(h.run_duration as varchar(6)), 6), 3, 0, ':'), 6, 0, ':') AS DURATION
, CASE h.run_status               
          WHEN 0 THEN 'failed'           
          WHEN 1 THEN 'Succeded'         
          WHEN 2 THEN 'Retry'            
          WHEN 3 THEN 'Cancelled'        
          WHEN 4 THEN 'In Progress'      
        END AS ExecutionStatus           

FROM    sysjobhistory h           
        INNER JOIN sysjobs j             
            ON j.job_id = h.job_id       
                     WHERE STEP_ID = 0
                     AND CAST(STR(h.run_date, 8, 0) AS DATETIME) + CAST(STUFF(STUFF(RIGHT('000000' + CAST (h.run_time AS VARCHAR(6)), 6),5, 0, ':'), 3, 0, ':') AS DATETIME)>=getdate()-90
                     AND (j.name Like 'DW%' 
                     )
ORDER BY j.name, STUFF(STUFF(h.run_date,5,0,'-'),8,0,'-')

所以这对于在DB中搜索某些文本的人来说会很有帮助。

这是在sql server中查找SP / view / trigger等定义的查询。

SELECT definition   
FROM sys.sql_modules  
WHERE object_id = OBJECT_ID(N'Object Name Here'); 

以上是关于如何在SQL Server的数据库中搜索字符串的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Sql Server 2008 R2 存储过程中搜索字符串?

如何在 vb.net、SQL Server 中为多个搜索制作代码

如何使用 .NET 代码优先搜索多个 SQL Server 数据库

如何在SQL Server返回的DataGrid中显示数据(后面没有代码)

如何在 SQL Server 中进行包含字符串每个单词的搜索查询?

SQL Server多表搜索字符串