IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_tool_searcher]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_tool_searcher]
GO

CREATE proc [dbo].[mps_tool_searcher](@WHAT nvarchar(800))    
as    
begin    

set nocount on ;
--================================================
--搜索存储过程、函数内容
SELECT SPECIFIC_CATALOG 数据库,ROUTINE_NAME 函数或存储过程,charindex(@WHAT,ROUTINE_DEFINITION) 位置 ,CREATED as [创建日期]  
,LAST_ALTERED as [修改日期]  
,ROUTINE_DEFINITION 脚本内容,'sp_helptext '+ROUTINE_NAME 查看脚本  
FROM INFORMATION_SCHEMA.ROUTINES   
WHERE ROUTINE_DEFINITION LIKE '%'+@WHAT+'%'    

--================================================
-- 搜索当前数据库所有触发器信息  
SELECT  
    object_name(a.parent_obj) as [表名]  
    ,a.name as [触发器名称]  
    ,(case when b.is_disabled=0 then '启用' else '禁用' end) as [状态]  
    ,b.create_date as [创建日期]  
    ,b.modify_date as [修改日期]  
    , 'sp_helptext '+cast(a.name as varchar(100)) as [查看触发器语句]  
FROM sysobjects a  
    INNER JOIN sys.triggers b  
        ON b.object_id=a.id  
    INNER JOIN syscomments c  
        ON c.id=a.id  
WHERE a.xtype='tr' 
and CHARINDEX(@WHAT,c.text)>0
ORDER BY [表名]

--================================================
--搜索当前数据表中数据
if OBJECT_ID('tempdb..#t') is not null
drop table tempdb..#t

declare @sql nvarchar(max)
select top 100 @sql=ISNULL(@sql+';','')+'insert #t select top 1 '''+tablename+''' ,'''
+columnname+''','+columnname+' from '+tablename+' where charindex('''+@WHAT+''','+columnname+')>0'
from
(
select a.name as tablename,b.name as columnname
from sysobjects a
join syscolumns b on a.id=b.id
join systypes c on b.xtype=c.xtype
where lower(a.type)='u'
and c.name in('text','ntext','varchar','char','nvarchar','nchar')
) t

create table #t(表名 nvarchar(128),字段名 nvarchar(128),字段数据 nvarchar(max));

exec(@sql)
select * from #t
--================================================
set nocount off ;

end