MS SQL SERVER 全库搜索
程序员文章站
2022-05-19 15:42:12
...
定位单个数据库中等于某值的记录所在的表和列。 第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行就是根据关键字具体过滤列的数据。
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
上一篇: 网页版井字游戏(TicTacToe)人机对战的制作(附思路和源码)
下一篇: 适合男人的座右铭
推荐阅读
-
解决SQL Server的“此数据库没有有效所有者”问题
-
SQL Server数据库重命名、数据导出的方法说明
-
如何恢复SQL Server 2000损坏的数据库文件
-
C#实现Excel表数据导入Sql Server数据库中的方法
-
Sql Server 数据库索引整理语句,自动整理数据库索引
-
让Django支持Sql Server作后端数据库的方法
-
SQL Server中通过扩展存储过程实现数据库的远程备份与恢复
-
sql server中通过查询分析器实现数据库的备份与恢复方法分享
-
SQL Server 作业的备份(备份作业非备份数据库)
-
卸载VS2011 Developer Preview后Sql Server2008 R2建立数据库关系图报“找不到指定的模块”错误的解决方法