给定一个数字,我如何发现在什么表和列中可以找到它?
我不在乎速度快不快,只要管用就行。
给定一个数字,我如何发现在什么表和列中可以找到它?
我不在乎速度快不快,只要管用就行。
当前回答
非常感谢这个有用的脚本。
如果你的表有不可转换的字段,你可能需要在代码中添加以下修改:
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 NOT IN ('text', 'image', 'ntext')
AND QUOTENAME(COLUMN_NAME) > @ColumnName
)
克里斯
其他回答
到目前为止,我发现的最好和最通用的解决方案是通过管道将db的转储传递给您正在搜索的grep。
例如,Mysql:
mysqldump -pPASSWORD database | grep 'search phrase'
或者如果你得到了太多的结果,你可以把它们输出到一个文件:
mysqldump -pPASSWORD database | grep 'search phrase' > results.txt
如果你安装了phpMyAdmin,使用它的搜索功能。
选择您的数据库。
请确保您选择的是DataBase,而不是表,否则您将得到一个完全不同的搜索对话框。
单击“搜索”页签 列表项选择所需的搜索词 选择要搜索的表
使用JOIN和CURSOR的另一种方法:
USE My_Database;
-- Store results in a local temp table so that. I'm using a
-- local temp table so that I can access it in SP_EXECUTESQL.
create table #tmp (
tbl nvarchar(max),
col nvarchar(max),
val nvarchar(max)
);
declare @tbl nvarchar(max);
declare @col nvarchar(max);
declare @q nvarchar(max);
declare @search nvarchar(max) = 'my search key';
-- Create a cursor on all columns in the database
declare c cursor for
SELECT tbls.TABLE_NAME, cols.COLUMN_NAME FROM INFORMATION_SCHEMA.TABLES AS tbls
JOIN INFORMATION_SCHEMA.COLUMNS AS cols
ON tbls.TABLE_NAME = cols.TABLE_NAME
-- For each table and column pair, see if the search value exists.
open c
fetch next from c into @tbl, @col
while @@FETCH_STATUS = 0
begin
-- Look for the search key in current table column and if found add it to the results.
SET @q = 'INSERT INTO #tmp SELECT ''' + @tbl + ''', ''' + @col + ''', ' + @col + ' FROM ' + @tbl + ' WHERE ' + @col + ' LIKE ''%' + @search + '%'''
EXEC SP_EXECUTESQL @q
fetch next from c into @tbl, @col
end
close c
deallocate c
-- Get results
select * from #tmp
-- Remove local temp table.
drop table #tmp
非常感谢这个有用的脚本。
如果你的表有不可转换的字段,你可能需要在代码中添加以下修改:
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 NOT IN ('text', 'image', 'ntext')
AND QUOTENAME(COLUMN_NAME) > @ColumnName
)
克里斯
您可能需要为数据库构建一个倒立索引。它肯定是相当快的。