首页 > 资讯列表 > 编程/数据库 >> 数据库操作教程

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

数据库操作教程 2022-09-23 17:06:04 转载来源: 网络整理/侵权必删

复制代码代码如下:USE[数据库名称]; --1.定义需要查找的关键字。在搜索中,使用模糊搜索:LIKE'%@key_find%' DECLARE@key_findNVARCHAR(MAX)='123';--假设是找字符串"123" --2.用游标Cursor_Table,遍历所有表 DECLARECursor_TableCURSORFOR    SELECTnamefromsysobjectsWHERExtype='u'ANDname<>'dtproperties'; OPENCursor_Table; DECLARE@tableNameNVARCHAR(MAX); FETCHNEXTfromCursor_TableINTO@tableName; WHILE@@fetch_status=0 BEGIN    DECLARE@tempSQLTextNVARCHAR(MAX)='';  &

复制代码 代码如下:

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;

标签: sqlserver2012 数据库 所有 表里 查找 字符串 方法


声明:本文内容来源自网络,文字、图片等素材版权属于原作者,平台转载素材出于传递更多信息,文章内容仅供参考与学习,切勿作为商业目的使用。如果侵害了您的合法权益,请您及时与我们联系,我们会在第一时间进行处理!我们尊重版权,也致力于保护版权,站搜网感谢您的分享!

站长搜索

http://www.adminso.com

Copyright @ 2007~2024 All Rights Reserved.

Powered By 站长搜索

打开手机扫描上面的二维码打开手机版


使用手机软件扫描微信二维码

关注我们可获取更多热点资讯

站长搜索目录系统技术支持