How to find a text inside SQL Server procedures / triggers?

后端 未结 14 663
一个人的身影
一个人的身影 2020-12-04 04:48

I have a linkedserver that will change. Some procedures call the linked server like this: [10.10.100.50].dbo.SPROCEDURE_EXAMPLE. We have triggers also doing thi

相关标签:
14条回答
  • 2020-12-04 05:02

    I've used these in the past:

    • Searching all user stored procedures for a table name
    • Search and replace SQL Server data in all columns of all tables

    In this particular case, where you need to replace a specific string across stored procedures, the first link is probably more relevant.

    A little off-topic, the Quick Find add-in is also useful for searching object names with SQL Server Management Studio. There's a modified version available with some improvements, and another newer version also available on Codeplex with some other useful add-ins as well.

    0 讨论(0)
  • 2020-12-04 05:06

    There are much better solutions than modifying the text of your stored procedures, functions, and views each time the linked server changes. Here are some options:

    1. Update the linked server. Instead of using a linked server named with its IP address, create a new linked server with the name of the resource such as Finance or DataLinkProd or some such. Then when you need to change which server is reached, update the linked server to point to the new server (or drop it and recreate it).

    2. While unfortunately you cannot create synonyms for linked servers or schemas, you CAN make synonyms for objects that are located on linked servers. For example, your procedure [10.10.100.50].dbo.SPROCEDURE_EXAMPLE could by aliased. Perhaps create a schema datalinkprod, then CREATE SYNONYM datalinkprod.dbo_SPROCEDURE_EXAMPLE FOR [10.10.100.50].dbo.SPROCEDURE_EXAMPLE;. Then, write a stored procedure that accepts a linked server name, which queries all the potential objects from the remote database and (re)creates synonyms for them. All your SPs and functions get rewritten just once to use the synonym names starting with datalinkprod, and ever after that, to change from one linked server to another you just do EXEC dbo.SwitchLinkedServer '[10.10.100.51]'; and in a fraction of a second you're using a different linked server.

    There may be even more options. I highly recommend using the superior techniques of pre-processing, configuration, or indirection rather than changing human-written scripts. Automatically updating machine-created scripts is fine, this is preprocessing. Doing things manually is awful.

    0 讨论(0)
  • 2020-12-04 05:06
    select text
    from syscomments
    where text like '%your text here%'
    0 讨论(0)
  • 2020-12-04 05:06

    Just wrote this for generic full outer cross ref

    create table #XRefDBs(xtype varchar(2),SourceDB varchar(100), Object varchar(100), RefDB varchar(100))
    
    declare @sourcedbname varchar(100),
            @searchfordbname varchar(100),
            @sql nvarchar(4000)
    declare curs cursor for
        select name 
        from sysdatabases
        where dbid>4
    open curs
    fetch next from curs into @sourcedbname
    while @@fetch_status=0
        begin
        print @sourcedbname
        declare curs2 cursor for 
            select name 
            from sysdatabases
            where dbid>4
            and name <> @sourcedbname
        open curs2
        fetch next from curs2 into @searchfordbname
        while @@fetch_status=0
            begin
            print @searchfordbname
            set @sql = 
            'INSERT INTO #XRefDBs (xtype,SourceDB,Object, RefDB)
            select DISTINCT o.xtype,'''+@sourcedbname+''', o.name,'''+@searchfordbname+'''
            from '+@sourcedbname+'.dbo.syscomments c
            join '+@sourcedbname+'.dbo.sysobjects o on c.id=o.id
            where o.xtype in (''V'',''P'',''FN'',''TR'')
            and (text like ''%'+@searchfordbname+'.%''
              or text like ''%'+@searchfordbname+'].%'')'
            print @sql
            exec sp_executesql @sql
            fetch next from curs2 into @searchfordbname
            end
        close curs2
        deallocate curs2
        fetch next from curs into @sourcedbname
        end
    close curs
    deallocate curs
    
    select * from #XRefDBs
    
    0 讨论(0)
  • 2020-12-04 05:12
    -- Declare the text we want to search for
    DECLARE @Text nvarchar(4000);
    SET @Text = 'employee';
    
    -- Get the schema name, table name, and table type for:
    
    -- Table names
    SELECT
           TABLE_SCHEMA  AS 'Object Schema'
          ,TABLE_NAME    AS 'Object Name'
          ,TABLE_TYPE    AS 'Object Type'
          ,'Table Name'  AS 'TEXT Location'
    FROM  INFORMATION_SCHEMA.TABLES
    WHERE TABLE_NAME LIKE '%'+@Text+'%'
    UNION
     --Column names
    SELECT
          TABLE_SCHEMA   AS 'Object Schema'
          ,COLUMN_NAME   AS 'Object Name'
          ,'COLUMN'      AS 'Object Type'
          ,'Column Name' AS 'TEXT Location'
    FROM  INFORMATION_SCHEMA.COLUMNS
    WHERE COLUMN_NAME LIKE '%'+@Text+'%'
    UNION
    -- Function or procedure bodies
    SELECT
          SPECIFIC_SCHEMA     AS 'Object Schema'
          ,ROUTINE_NAME       AS 'Object Name'
          ,ROUTINE_TYPE       AS 'Object Type'
          ,ROUTINE_DEFINITION AS 'TEXT Location'
    FROM  INFORMATION_SCHEMA.ROUTINES 
    WHERE ROUTINE_DEFINITION LIKE '%'+@Text+'%'
          AND (ROUTINE_TYPE = 'function' OR ROUTINE_TYPE = 'procedure');
    
    0 讨论(0)
  • 2020-12-04 05:14

    You can search within the definitions of all database objects using the following SQL:

    SELECT 
        o.name, 
        o.id, 
        c.text,
        o.type
    FROM 
        sysobjects o 
    RIGHT JOIN syscomments c 
        ON o.id = c.id 
    WHERE 
        c.text like '%text_to_find%'
    
    0 讨论(0)
提交回复
热议问题