在SQL2008查找某数据库中的列是否存在某个值
2015-08-10 10:49
393 查看
--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'
转自:http://www.cnblogs.com/lyhabc/p/4150407.html
相关文章推荐
- 多条件搜索拼接Sql语句
- 清理oracle预警的警告信息
- 详解Oracle DELETE和TRUNCATE 的区别
- MySQL5.6 GTID新特性实践
- 源码编译安装mysql 5.5.27 脚本
- 如何在H2数据库中定义函数
- Mantis、Testlink从Windows移植到Ubuntu系统
- oracle转mysql
- NoSQL开篇—为什么要使用NoSQL
- jdbc mysql 取数,突然取不到数据,数据库中有数据
- Java连接oracle数据库的两种常用方法
- 5.1 扩展库--MySQLdb
- 配置Mybatis时报configuration错误
- Java连接oracle数据库的两种常用方法
- MySQL基本使用
- Java连接oracle数据库的两种常用方法
- MongoDB无法启动的解决方法
- SQL语句select from where group by having order by 顺序
- windows环境下导出mysql文件
- Spark-sql支持的sql语法