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

后端 未结 15 1477
执念已碎
执念已碎 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.

提交回复
热议问题