1、创建存储过程,我在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 #tbl2、执行:spFind_Column_In_DB 2,100

解决方案 »

  1.   

    问个白痴问题  我如何测试找出 我需要的数据呢 比如我要找一个值为10的列   不太懂存储过程  只熟悉T-SQL
      

  2.   

    1、执行:exec spFind_Column_In_DB 2,10
    2、这个存储过程会返回一堆结果,你随便调一行,上面有表名和列名,你就:
    select * from 表名 where 列名=10,如果非空,就是找到这个数据了