sql 在所有表中搜索所有Cloumns以获取值SQL
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 在所有表中搜索所有Cloumns以获取值SQL相关的知识,希望对你有一定的参考价值。
--initialize transaction
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
SET nocount ON
--initial declarations
DECLARE @rowID INT , @maxRowID INT
DECLARE @ SQL nvarchar(4000)
DECLARE @searchValue varchar(100)
DECLARE @statements TABLE (rowID INT , SQL varchar(8000))
CREATE TABLE # results (tableName varchar(250), tableSchema varchar(250), columnName varchar(250))
SET @rowID = 1
SET @searchValue = 'test'
--create CTE table holding metadata
; WITH MyInfo (tableName, tableSchema, columnName) AS (
SELECT TABLE_NAME , table_schema, COLUMN_NAME FROM information_schema.columns WHERE data_type NOT IN ( 'image' , 'text' , 'timestamp' , 'binary' )
)
--create search strings
INSERT INTO @statements
SELECT row_number() over (order by tableName, columnName) AS rowID, 'insert into #results select distinct ''' + tableName + ''', ''' + tableSchema + ''', ''' + columnName + ''' from [' + tableSchema + '].[' + tableName + '] where convert(varchar,[' + columnName + ']) like ''%' + @searchValue + '%''' FROM myInfo
--initialize while components and process search strings
SELECT @maxRowID = MAX (rowID) FROM @statements
WHILE @rowID <= @maxRowID
BEGIN
SELECT @ SQL = SQL FROM @statements WHERE rowID = @rowID
EXEC sp_executeSQL @ SQL
SET @rowID = @rowID + 1
end
--view results and cleanup
SELECT * FROM # results
DROP TABLE # results
以上是关于sql 在所有表中搜索所有Cloumns以获取值SQL的主要内容,如果未能解决你的问题,请参考以下文章
在SQLserver2008中,怎么在整个数据库的所有表中搜索一个数据,求详解
sql 查所有表中是不是有某个值
如何操作SQL IN运算符以返回所有值作为结果
在 SQL Server 表中搜索值列表的最有效方法
需要帮助以获取访问表单以将值插入查询
SQL查询从通配符列等于值的所有表中选择所有行[重复]