mysql全库搜索关键字_MS SQL SERVER 全库搜索

定位单个数据库中等于某值的记录所在的表和列。 第13行的xtype=167代表只搜索数据类型是varchar的列。 第18行就是根据关键字具体过滤列的数据。 SQL Server create proc global_search@key varchar(2000)asdeclare tab_cursor cursor for select name from s

定位单个数据库中等于某值的记录所在的表和列。

第13行的xtype=167代表只搜索数据类型是varchar的列。

第18行就是根据关键字具体过滤列的数据。

SQL Server

create proc global_search

@key varchar(2000)

as

declare tab_cursor cursor for select name from sysobjects where type = 'U'

declare @sql nvarchar(2000)

declare @tab_name nvarchar(100)

declare @col_name nvarchar(100)

declare @row_count int

open tab_cursor

fetch next from tab_cursor into @tab_name

while(@@fetch_status = 0)

begin

declare col_cursor cursor for select name from syscolumns where id = OBJECT_ID(@tab_name) and xtype = 167

open col_cursor

fetch next from col_cursor into @col_name

while(@@fetch_status = 0)

begin

set @sql = 'declare row_cursor cursor for select count(*) from ' + @tab_name + ' where ' + @col_name + ' like ''%' + @key + '%'''

exec(@sql)

open row_cursor

fetch next from row_cursor into @row_count

if @row_count > 0

print @tab_name + '.' + @col_name

close row_cursor

deallocate row_cursor

fetch next from col_cursor into @col_name

end

close col_cursor

deallocate col_cursor

fetch next from tab_cursor into @tab_name

end

close tab_cursor

deallocate tab_cursor

create proc global_search

@key nvarchar(2000)

as

declare @sql nvarchar(2000)

declare @tab_name nvarchar(100)

declare @col_name nvarchar(100)

declare @row_count int

declare @has_cursor int

declare @col_cursor cursor

declare @tab_cursor cursor

set @tab_cursor = cursor for select name from sysobjects where type = 'U'

open @tab_cursor

fetch next from @tab_cursor into @tab_name

while(@@fetch_status = 0)

begin

set @col_cursor = cursor for select name from syscolumns where id = OBJECT_ID(@tab_name) and xtype = 231 and length > 13

open @col_cursor

fetch next from @col_cursor into @col_name

while(@@fetch_status = 0)

begin

set @sql = N'select count(*) from ' + @tab_name + ' where ' + @col_name + ' = ''%' + @key + '%'''

exec sp_executesql @sql, N'@row_count int', @row_count

if @row_count > 0

print @tab_name + '.' + @col_name

fetch next from @col_cursor into @col_name

end

if cursor_status('local', '@col_cursor') > -1

close @col_cursor

if cursor_status('local', '@col_cursor') > -3

deallocate @col_cursor

fetch next from @tab_cursor into @tab_name

end

if cursor_status('local', '@tab_cursor') > -1

close @tab_cursor

if cursor_status('local', '@tab_cursor') > -3

deallocate @tab_cursor

f68f2add0b68e4f9810432fce46917b7.png

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值