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

后端 未结 14 811
一个人的身影
一个人的身影 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: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
    

提交回复
热议问题