首页 新闻 会员 周边

sql存储过程全站搜索、并分页

0
悬赏园豆:5 [已解决问题] 解决于 2013-09-11 10:22

--但只已知一个字符串,去查询整个数据库内有那些表里面包含这个字符串,
用游标可以实现了这个需求,但是我想求一个能查询数据库里面所有nvarchar、varchar并且能分页的存储过程: 
 

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
 exec('select  distinct '+@colname+' from ' +@tbname +'  where '+ @colname+' like '+'''%'+@string+'%''')
 
 fetch next from colroy into @colname
 end       
  
 close colroy
 deallocate colroy
 
fetch next from tbroy into @tbname
end
close tbroy
deallocate tbroy
end
 
exec Full_Search  '市场'
 
drop proc Full_Search

 

 

次时代的主页 次时代 | 初学一级 | 园豆:5
提问于:2013-09-11 09:58
< >
分享
最佳答案
0

问题打错了~

次时代 | 初学一级 |园豆:5 | 2013-09-11 10:21
其他回答(1)
0

建议使用CTE(Common Table Expressions,公用表表达式),参考:SQL Server2005的几种分页方法

收获园豆:5
dudu | 园豆:31003 (高人七级) | 2013-09-11 10:02
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册