我坐在一个巨大的数据库前,不知道这些表必须如何链接在一起.为了找出答案,我想列出所有表以及在任何行中包含特定值的列.这可能吗?一种方法可能是将整个数据库转储到一个文本文件中,而不是使用任何文本编辑器来搜索值 - 但如果数据库太大,这将是纯粹的痛苦. 解决方案 如何搜索所有的列关键字的数据库中的表?http://vyaskn.tripod.com/search_all_columns_in_all_tables.htm这是实际的 T-SQL,以防链接失效:CREATE PROC SearchAllTables(@SearchStr nvarchar(100))作为开始-- 版权所有 © 2002 Narayana Vyas Kondreddi.版权所有.-- 目的:为给定的搜索字符串搜索所有表的所有列-- 作者:Narayana Vyas Kondreddi-- 网站:http://vyaskn.tripod.com-- 测试:SQL Server 7.0 和 SQL Server 2000-- 修改日期:2002 年 7 月 28 日 22:50 GMTCREATE TABLE #Results (ColumnName nvarchar(370), ColumnValue nvarchar(3630))设置无计数声明 @TableName nvarchar(256)、@ColumnName nvarchar(128)、@SearchStr2 nvarchar(110)SET @TableName = ''SET @SearchStr2 = QUOTENAME('%' + @SearchStr + '%','''')虽然@TableName 不为空开始SET @ColumnName = ''设置@表名=(SELECT MIN(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME))来自 INFORMATION_SCHEMA.TABLESWHERE TABLE_TYPE = '基础表'AND QUOTENAME(TABLE_SCHEMA) + '.'+ QUOTENAME(TABLE_NAME) >@表名和对象属性(对象 ID(QUOTENAME(TABLE_SCHEMA) + '.'+ QUOTENAME(TABLE_NAME)), 'IsMSShipped') = 0)WHILE (@TableName IS NOT NULL) AND (@ColumnName IS NOT NULL)开始设置@列名=(SELECT MIN(QUOTENAME(COLUMN_NAME))来自 INFORMATION_SCHEMA.COLUMNSWHERE TABLE_SCHEMA = PARSENAME(@TableName, 2)AND TABLE_NAME = PARSENAME(@TableName, 1)AND DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar')AND QUOTENAME(COLUMN_NAME) >@列名)如果@ColumnName 不为空开始插入#Results执行('选择''' + @TableName + '.'+ @ColumnName + ''', LEFT(' + @ColumnName + ', 3630)FROM ' + @TableName + ' (NOLOCK) ' +'WHERE' + @ColumnName + 'LIKE' + @SearchStr2)结尾结尾结尾SELECT ColumnName, ColumnValue FROM #Results结尾Is there a way to search for one value (in my case it is a UID of the type char(64)) inside any column of any table inside one MS SQL Server database?I'm sitting in front of a huge database without any idea how the tables had to be linked together. To find that out I'd like to list all tables and there columns that contain a certain value in any row. Is that possible?One way could be to just dump the entire database into a text file and than use any text-editor to search for the value - but this would be pure pain if the database is too huge. 解决方案 How to search all columns of all tables in a database for a keyword?http://vyaskn.tripod.com/search_all_columns_in_all_tables.htmEDIT: Here's the actual T-SQL, in case of link rot: CREATE PROC SearchAllTables(@SearchStr nvarchar(100))ASBEGIN-- Copyright © 2002 Narayana Vyas Kondreddi. All rights reserved.-- Purpose: To search all columns of all tables for a given search string-- Written by: Narayana Vyas Kondreddi-- Site: http://vyaskn.tripod.com-- Tested on: SQL Server 7.0 and SQL Server 2000-- Date modified: 28th July 2002 22:50 GMTCREATE TABLE #Results (ColumnName nvarchar(370), ColumnValue nvarchar(3630))SET NOCOUNT ONDECLARE @TableName nvarchar(256), @ColumnName nvarchar(128), @SearchStr2 nvarchar(110)SET @TableName = ''SET @SearchStr2 = QUOTENAME('%' + @SearchStr + '%','''')WHILE @TableName IS NOT NULLBEGIN SET @ColumnName = '' SET @TableName = ( SELECT MIN(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) > @TableName AND OBJECTPROPERTY( OBJECT_ID( QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) ), 'IsMSShipped' ) = 0 ) WHILE (@TableName IS NOT NULL) AND (@ColumnName IS NOT NULL) BEGIN SET @ColumnName = ( SELECT MIN(QUOTENAME(COLUMN_NAME)) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = PARSENAME(@TableName, 2) AND TABLE_NAME = PARSENAME(@TableName, 1) AND DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar') AND QUOTENAME(COLUMN_NAME) > @ColumnName ) IF @ColumnName IS NOT NULL BEGIN INSERT INTO #Results EXEC ( 'SELECT ''' + @TableName + '.' + @ColumnName + ''', LEFT(' + @ColumnName + ', 3630) FROM ' + @TableName + ' (NOLOCK) ' + ' WHERE ' + @ColumnName + ' LIKE ' + @SearchStr2 ) END END ENDSELECT ColumnName, ColumnValue FROM #Results END 这篇关于在数据库内任何表的任何列中搜索一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-27 10:57