码迷,mamicode.com
首页 > 数据库 > 详细

在SQL2008查找某数据库中的列是否存在某个值

时间:2014-12-08 10:32:18      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   ar   color   os   使用   sp   for   

在SQL2008查找某数据库中的列是否存在某个值

--SQL2008查找某数据库中的列是否存在某个值
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

 

在SQL2008查找某数据库中的列是否存在某个值

标签:des   style   blog   ar   color   os   使用   sp   for   

原文地址:http://www.cnblogs.com/lyhabc/p/4150407.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!