视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
MSSQLSERVER全库搜索
2020-11-09 16:16:16 责编:小采
文档


定位单个数据库中等于某值的记录所在的表和列。 第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 $velocityCount-->
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

下载本文
显示全文
专题