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

后端 未结 15 1478
执念已碎
执念已碎 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 18:58

    You could;

    1. Script the database to a single file and search the file for tblEmployees using a text editor. In SQL Server Management Studio (SSMS), right click over the database and choose Generate Scripts.
    2. Use SSMS 'View Dependencies' by right clicking over tblEmployees to see which other objects are dependent on it
    3. Use a free third-party tool such as Redgate Software's SQL Search to search all database objects by name and content by keyword.
    0 讨论(0)
  • 2020-11-28 19:02

    You can also try ApexSQL Search – it’s a free SSMS add-in similar to SQL Search.

    If you really want to use only SQL you might want to try this script:

    select
    S.name as [Schema],
    o.name as [Object],
    o.type_desc as [Object_Type],
    C.text as [Object_Definition]
    from sys.all_objects O inner join sys.schemas S on O.schema_id = S.schema_id
    inner join sys.syscomments C on O.object_id = C.id
    where S.schema_id not in (3,4) -- avoid searching in sys and INFORMATION_SCHEMA schemas
    and C.text like '%ICE_%'
    order by [Schema]
    
    0 讨论(0)
  • 2020-11-28 19:03

    I was given access to a database, but not the table where my query was being stored in.

    Inspired by @marc_s answer, I had a look at HeidiSQL which is a Windows program that can deal with MySQL, SQL Server, and PostgreSQL.

    I found that it can also search a database for a string.

    It will search each table and give you how many times it found the string per table!

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

    You can export your database (if small) to your hard drive / desktop, and then just do a string search via a text search program or text editor.

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

    This code searching procedure and function but not search in table :)

    SELECT name 
    FROM   sys.all_objects 
    WHERE  Object_definition(object_id) 
    LIKE '%text%' 
    ORDER BY name
    
    0 讨论(0)
  • 2020-11-28 19:06

    This will search for a string over every database:

    declare @search_term varchar(max)
    set @search_term = 'something'
    
    select @search_term = 'use ? SET QUOTED_IDENTIFIER ON
    select
        ''[''+db_name()+''].[''+c.name+''].[''+b.name+'']'' as [object],
        b.type_desc as [type],
        d.obj_def.value(''.'',''varchar(max)'') as [definition]
    from (
        select distinct
            a.id
        from sys.syscomments a
        where a.[text] like ''%'+@search_term+'%''
    ) a
    inner join sys.all_objects b
        on b.[object_id] = a.id
    inner join sys.schemas c
        on c.[schema_id] = b.[schema_id]
    cross apply (
        select
            [text()] = a1.[text]
        from sys.syscomments a1
        where a1.id = a.id
        order by a1.colid
        for xml path(''''), type
    ) d(obj_def)
    where c.schema_id not in (3,4) -- avoid searching in sys and INFORMATION_SCHEMA schemas
        and db_id() not in (1,2,3,4) -- avoid sys databases'
    
    if object_id('tempdb..#textsearch') is not null drop table #textsearch
    create table #textsearch
    (
        [object] varchar(300),
        [type] varchar(300),
        [definition] varchar(max)
    )
    
    insert #textsearch
    exec sp_MSforeachdb @search_term
    
    select *
    from #textsearch
    order by [object]
    
    0 讨论(0)
提交回复
热议问题