How do I search an SQL Server database for a string?

后端 未结 15 1480
执念已碎
执念已碎 2020-11-28 18:40

I know it\'s possible, but I don\'t know how.

I need to search an SQL Server database for all mentions of a specific string.

For example: I would like t

相关标签:
15条回答
  • 2020-11-28 19:19

    The content of all stored procedures, views and functions are stored in field text of table sysComments. The name of all objects are stored in table sysObjects and the columns are in sysColumns.

    Having this information, you can use this code to search in content of views, stored procedures, and functions for the specified word:

    Select b.name from syscomments a
    inner join sysobjects b on a.id = b.id
    where text like '%tblEmployes%'
    

    This query will give you the objects which contains the word "tblEmployes" .

    To search by the name of Objects you can use this code:

    Select name from sysobjects
    where name like  '%tblEmployes%'
    

    And finally to find the objects having at least one column containing the word "tblEmployes", you can use this code:

    Select b.name from syscolumns a inner join sysobjects b on a.id = b.id
    where a.name like  '%tblEmployes%'
    

    You can combine these three queries with union:

    Select distinct b.name from syscomments a
    inner join sysobjects b on a.id = b.id
    where text like '%tblEmployes%'
    union
    Select distinct name from sysobjects
    where name like  '%tblEmployes%'
    union
    Select distinct b.name from syscolumns a inner join sysobjects b on a.id = b.id
    where a.name like  '%tblEmployes%'
    

    With this query you have all objects containing the word "tblEmployes" in content or name or as a column.

    0 讨论(0)
  • 2020-11-28 19:21

    If I want to find where anything I want to search is, I use this:

    DECLARE @search_string    varchar(200)
        SET @search_string = '%myString%'
    
        SELECT DISTINCT
               o.name AS Object_Name,
               o.type_desc,
               m.definition
          FROM sys.sql_modules m
               INNER JOIN
               sys.objects o
                 ON m.object_id = o.object_id
         WHERE m.definition Like @search_string;
    
    0 讨论(0)
  • 2020-11-28 19:23

    If you need to find database objects (e.g. tables, columns, and triggers) by name - have a look at the free Redgate Software tool called SQL Search which does this - it searches your entire database for any kind of string(s).

    Enter image description here

    Enter image description here

    It's a great must-have tool for any DBA or database developer - did I already mention it's absolutely free to use for any kind of use??

    0 讨论(0)
提交回复
热议问题