例如,我的数据库中有 100 个表(table1、table2、table3、table4 和 table5 .....),table1 的列 col1、col2...col7 就像每个表都有一些列一样。col7 列的值为“BOLD”。在这里,我想检索包含值“BOLD”的表。
问问题
87 次
2 回答
0
也许这个问题有你正在寻找的东西。
脚本取自已接受的答案:
SELECT 'SELECT * FROM [' + tables.TABLE_NAME + '] WHERE ['
+ Columns.Column_Name + '] = ' + CONVERT(varchar(50),@COLUMNVALUE)
FROM INFORMATION_SCHEMA.Columns as Columns
INNER JOIN INFORMATION_SCHEMA.TABLES as tables
On Columns.TABLE_NAME = tables.TABLE_NAME
WHERE Columns.DATA_TYPE = @COLUMNTYPE
于 2013-08-17T00:48:01.780 回答
0
根据 Felipe 的回答,您可以使用生成的 SELECT,并循环执行它们:
SELECT 'SELECT TOP 1 1 FROM [' + tables.TABLE_NAME + '] WHERE [' + Columns.Column_Name + '] LIKE ''%' + CONVERT(varchar(50),'BOLD') + '%''' As Qry,
IDENTITY(int,1,1) As Id,
tables.TABLE_NAME As TableName,
Cast(null As Bit) As ContainsText
INTO #Queries
FROM INFORMATION_SCHEMA.Columns as Columns
INNER JOIN INFORMATION_SCHEMA.TABLES as tables
On Columns.TABLE_NAME = tables.TABLE_NAME
WHERE Columns.DATA_TYPE in ('varchar','nvarchar','text')
DECLARE @TableName NVARCHAR(MAX), @Qry NVARCHAR(MAX), @ContainsText BIT, @id INT
WHILE EXISTS (SELECT * FROM #Queries WHERE ContainsText IS NULL)
BEGIN
SELECT TOP 1 @id = id, @TableName = TableName, @Qry = Qry FROM #Queries WHERE ContainsText IS NULL
EXEC (@Qry)
If (@@ROWCOUNT > 0)
Set @ContainsText = 1
Else
Set @ContainsText = 0
UPDATE #Queries SET ContainsText = @ContainsText WHERE id = @id
END
SELECT * FROM #Queries Where ContainsText = 1
此脚本将获取包含文本类型(nvarchar、varchar 或 text)列的所有表,然后将逐一执行 SELECT 以评估是否存在包含给定文本('BOLD')的任何行。
于 2013-08-17T03:20:35.047 回答