gpt4 book ai didi

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

转载 作者:qq735679552 更新时间:2022-09-29 22:32:09 28 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章sql server 2012 数据库所有表里查找某字符串的方法由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

代码如下

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,

最后此篇关于sql server 2012 数据库所有表里查找某字符串的方法的文章就讲到这里了,如果你想了解更多关于sql server 2012 数据库所有表里查找某字符串的方法的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com