博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
在SQL2008查找某数据库中的列是否存在某个值
阅读量:6975 次
发布时间:2019-06-27

本文共 1631 字,大约阅读时间需要 5 分钟。

在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'

 

本文版权归作者所有,未经作者同意不得转载。

你可能感兴趣的文章
初级Java程序员所面临的4大挑战
查看>>
《算法基础:打开算法之门》一1.5 拓展阅读
查看>>
移动应用开发者应该关注的 Google I/O 两项更新
查看>>
2014 年美国程序员薪资调查
查看>>
方差,标准差,协方差、期望值
查看>>
java异常笔记
查看>>
区域链实践第一步——区域链测试环境搭建
查看>>
《C语言及程序设计》实践项目——画分支结构流程图
查看>>
Qt 自定义信号与槽
查看>>
百度地图 ip查询 service
查看>>
Java新手如何学习三大框架
查看>>
Learn Jenkins the hard way (0) - Jenkins的罪与罚
查看>>
Hadoop history
查看>>
mysql limit offset
查看>>
statpot:使用mongo+bootstrap+highcharts做统计报表
查看>>
文件上传的渐进式增强
查看>>
Linux Shell 脚本限制ssh最大用户登录数
查看>>
incompatible with sql_mode=only_full_group_by
查看>>
C# 判断远程文件是否存在
查看>>
分享一款jQuery全屏滚动页面特性案例
查看>>