SQL全站搜索

create proc
Full_Search(@string varchar(50))
as
begin

declare @tbname
varchar(50)
declare tbroy cursor for select name from sysobjects
where
xtype= 'u ' --第一个游标遍历所有的表

open tbroy
fetch next from tbroy into
@tbname
while @@fetch_status=0
begin

declare @colname
varchar(50)
declare colroy cursor for select name from syscolumns
where
id=object_id(@tbname) and xtype in (
select xtype from systypes
where
name in ( 'varchar ', 'nvarchar ', 'char ', 'nchar ') --数据类型为字符型的字段
)
--第二个游标是第一个游标的嵌套游标,遍历某个表的所有字段

open colroy
fetch next from colroy
into @colname
while @@fetch_status=0
begin

declare @sql
nvarchar(1000),@j int
select @sql= 'select @i=count(1) from ' +@tbname + '
where '+ @colname+ ' like '+ '''%'+@string+ '%'''
exec sp_executesql
@sql,N'@i int output',@i=@j output --输出满足条件表的记录数
if @j> 0
BEGIN

select 包含字串的表名=@tbname
--exec( 'select distinct '+@colname+' from '
+@tbname + ' where '+ @colname+ ' like '+ '''%'+@string+ '%''')
END

fetch next from colroy into @colname
end

close colroy

deallocate colroy

fetch next from tbroy into @tbname
end

close tbroy
deallocate tbroy
end
go

exec Full_Search
'搜索关键字'

最新文章

  1. React Native环境配置和简单使用
  2. webDriver环境搭建与测试
  3. php 接收表单 方法的区别
  4. thinkphp 常用
  5. vi编辑器基本用法介绍
  6. cf754D
  7. C#递归搜索指定目录下的文件或目录
  8. http-关于application/x-www-form-urlencoded等字符编码的解释说明
  9. CentOS6.9中挂载NTFS移动硬盘
  10. 【MyBatis源码分析】插件实现原理
  11. Web项目中手机注册短信验证码实现的全流程及代码
  12. python 加密算法及其相关模块的学习(hashlib,random,string,math)
  13. asp.net mvc 通过StyleBundle添加样式后,没有作用
  14. cp备份操作时如何忽略指定的目录
  15. Matplotlib新手上路(下)
  16. Unity Shader入门精要之 screen post-processing effect
  17. Linux下SVN配置hook经验总结
  18. 【运维理论】RAID级别简介
  19. sed删除空行和开头的空格和tab键
  20. jdk版本对应数字

热门文章

  1. [bzoj2431][HAOI2009][逆序对数列] (dp计数)
  2. noip模拟赛 读
  3. Spring + quartz实现定时发送邮件功能
  4. 微软消息队列MessageQueue(MQ)
  5. CI 日志类
  6. cisco路由器上的DHCP
  7. [JavaEE] Testing the Java EE Application : Basic Arquillian integration test
  8. Android天气预报+百度天气接口
  9. 开发汉澳sinox64位,对接汉澳矩阵电脑
  10. ZOJ2599:Graduated Lexicographical Ordering(很经典的数位DP)