sql server 2012 数据库所有表里查找某字符串的方法

时间:2024-01-14 16:16:03 


USE [数据库名称];

 --1.定义需要查找的关键字。在搜索中,使用模糊搜索:LIKE '%@key_find%'
 DECLARE @key_find NVARCHAR(MAX) = '123';--假设是找字符串"123"

 --2.用游标Cursor_Table,遍历所有表
 DECLARE Cursor_Table CURSOR FOR
     SELECT name from sysobjects WHERE xtype = 'u' AND name <> 'dtproperties';
 OPEN Cursor_Table;
 DECLARE @tableName NVARCHAR(MAX);
 FETCH NEXT from Cursor_Table INTO @tableName;
 WHILE @@fetch_status = 0
 BEGIN
     DECLARE @tempSQLText NVARCHAR(MAX) = '';

     --3.在表中,用游标columnCursor,遍历所有字段。注意,只遍历字符串类型的字段(列)
     DECLARE columnCursor CURSOR FOR
         SELECT Name FROM SysColumns WHERE ID = Object_Id( @tableName ) and
                                                                             (
                                                                                 xtype = 35 or --text
                                                                                 xtype = 99 or --ntext
                                                                                 xtype = 167 or --varchar
                                                                                 xtype = 175 or --char
                                                                                 xtype = 231 or --nvarchar
                                                                                 xtype = 239 or --nchar
                                                                                 xtype = 241 --xml
                                                                             )
     OPEN columnCursor;
     DECLARE @columnName NVARCHAR(MAX);
     FETCH NEXT from columnCursor INTO @columnName;
     WHILE @@fetch_status = 0
     BEGIN

         --4.在表的字段中,对每一行进行模糊搜索,并输出找到的信息。
         DECLARE @DynamicSQLText NVARCHAR(MAX) = 'IF ( EXISTS ( SELECT * FROM [' + @tableName + '] WHERE [' + @columnName + '] LIKE ''%' + @key_find + '%'' ) ) BEGIN DECLARE @CurrentTableCount Bigint = ( SELECT COUNT(*) From [' + @tableName + '] ); PRINT ''Find : Table [' + @tableName + '], Column [' + @columnName + '], Row Count:'' + CAST( @CurrentTableCount AS NVARCHAR(MAX) ) + ''.'';  END';
         EXEC( @DynamicSQLText );
         FETCH NEXT from columnCursor INTO @columnName
     END
     exec(@tempSQLText);
     CLOSE columnCursor;
     DEALLOCATE columnCursor;
     FETCH NEXT from Cursor_Table INTO @tableName;
 END
 CLOSE Cursor_Table;
 DEALLOCATE Cursor_Table;
标签:数据库,所有表,字符串
0
投稿

猜你喜欢

  • python网络爬虫实战

    2021-04-16 13:31:07
  • vue嵌套组件传参实例分享

    2024-06-07 15:23:11
  • python嵌套函数使用外部函数变量的方法(Python2和Python3)

    2022-05-16 20:29:25
  • GitHub上值得推荐的8个python 项目

    2021-01-11 22:40:12
  • python pands实现execl转csv 并修改csv指定列的方法

    2022-11-20 01:45:27
  • Python 实现简单的电话本功能

    2021-11-16 09:47:58
  • SQL Server中索引使用及维护

    2008-11-25 11:13:00
  • python使用pyshp读写shp文件的实现

    2023-10-02 04:07:13
  • np.newaxis 实现为 numpy.ndarray(多维数组)增加一个轴

    2021-02-14 07:48:20
  • Python闭包及装饰器运行原理解析

    2022-12-16 20:59:06
  • Vue生命周期实例分析总结

    2024-06-07 15:23:38
  • Python数据结构之双向链表详解

    2023-02-01 05:10:07
  • 浅谈Python2获取中文文件名的编码问题

    2021-08-11 08:18:45
  • 深入解析Golang中JSON的编码与解码

    2023-07-04 12:45:32
  • PHP中PDO事务处理操作示例

    2023-11-21 14:54:12
  • Python如何实现转换URL详解

    2021-06-28 20:23:46
  • C#如何读取Txt大数据并更新到数据库详解

    2024-01-24 18:51:11
  • Python 序列化 pickle/cPickle模块使用介绍

    2021-04-28 17:23:21
  • 简单讲解Python中的字符串与字符串的输入输出

    2021-09-14 18:57:08
  • MySQL带你秒懂索引下推

    2024-01-15 18:34:42
  • asp之家 网络编程 m.aspxhome.com