首页 > 数据库技术 > 详细

mssql查询某个值存在某个表里的哪个字段的值里面

时间:2014-08-26 13:20:16      阅读:332      评论:0      收藏:0      [点我收藏+]

第一步:创建 查询某个值存在某个表里的哪个字段的值里面 的存储过程

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

第二步:从数据库中找到这个刚生成的存储过程。
bubuko.com,布布扣

 

第三步:

bubuko.com,布布扣

点击确定后

bubuko.com,布布扣

 

第四步:右键spFind_Column_In_DB_Limy->执行存储过程

bubuko.com,布布扣

 

按照需要,填入参数。

 

 

可能和它一起用的SQL语句:

查看与某一个表相关的视图、存储过程、函数

select a.* from sysobjects a, syscomments b where a.id = b.id and b.text like ‘%表名%‘

 

mssql查询某个值存在某个表里的哪个字段的值里面

原文:http://www.cnblogs.com/gates/p/3936945.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!