MS SQL SERVER 全库搜索

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

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