日期:2014-05-16  浏览次数:20592 次

这种字符查找能实现吗---大神请帮忙,紧急
我想查出所有数据库对象里面含有'CostMethodKey'这个字符串的对象名(如:sp、view、fun),例如:
有一个存储过程SP_A,里面含有这句话:“
 SELECT * FROM dbo.RunDetailEAV WHERE attribute='CostMethodKey' 

” ---这句话包含CostMethodKey这个字符串,所以我需要把这个存储过程的名字(SP_A)查出来。但是我要的不仅仅是sp,其他对象的定义脚本里面只要有这个字符串的,我都要把它们捞出来。
能实现吗?
------解决方案--------------------
--2008查找某数据库中的列是否存在某个值
create proc spFind_Column_In_DB
(
@type int,--类型:1为文字类型、2为数值类型
@str nvarchar(100)--需要搜索的名字
)
as
--创建临时表存放结果
create table #tbl(PK int identity primary key ,tbl sysname,col sysname)
declare @tbl nvarchar(300),@col sysname,@sql nvarchar(1000)
if @type=1 
begin
declare curTable cursor fast_forward
for 
select '['+SCHEMA_NAME(SCHEMA_ID)+'].['+o.name+']' tableName,'['+c.name+']' columnName from sys.columns c inner join sys.objects o on c.object_id=o.object_id
where o.type_desc='user_table' and user_type_id in (167,175,231,239,35,99)
  end
else
begin 
declare curTable cursor fast_forward
for 
select '['+SCHEMA_NAME(SCHEMA_ID)+'].['+o.name+']' tableName,'['+c.name+']' columnName from sys.columns c inner join sys.objects o on c.object_id=o.object_id
where o.type_desc='user_table' and user_type_id in (56,48,52,59,60,62,106,108,122)
end
open curtable
fetch next from curtable into @tbl,@col
while @@FETCH_STATUS=0
begin
set @sql='if exists (select * from '+@tbl+' where '
if @type=1
begin
set @sql += @col + ' like ''%'+@str +'%'')'
end
else 
begin
set @sql +=@col + ' in ('+@str+'))'
end

set @sql += ' INSERT #TBL(tbl,col) VALUES('''+@tbl+''','''+@col+''')'
--print @sql
exec (@sql)
fetch next from curtable into @tbl,@col
end
close curtable 
deallocate curtable
select * from #tbl

--使用例子,查询库中存在aaa这个值的列:
exec  spFind_Column_In_DB  1,'aaa'

------解决方案--------------------
SELECT OBJECT_NAME(id) FROM sys.syscomments WHERE text LIKE '%CostMethodKey%'

------解决方案--------------------
额,你要的是定义啊?从sys.sql_modules这里查