我想在整个MS SQL 2000数据库中搜索一个值。这将仅有助于发展。考虑这个问题时,请记住这一点。
这将获取我要查找的所有表名和数据类型的列:
SELECT Columns.COLUMN_NAME, tables.TABLE_NAME FROM INFORMATION_SCHEMA.Columns as Columns JOIN INFORMATION_SCHEMA.TABLES as tables On Columns.TABLE_NAME = tables.TABLE_NAME WHERE Columns.DATA_TYPE = 'INT'
我在想这样的事情:
-- Vars DECLARE @COUNTER INT DECLARE @TOTAL INT DECLARE @TABLE CHAR(128) DECLARE @COLUMN CHAR(128) DECLARE @COLUMNTYPE CHAR(128) DECLARE @COLUMNVALUE INT -- What we are looking for SET @COLUMNTYPE = 'INT' SET @COLUMNVALUE = 3 SET @COUNTER = 0 -- Find out how many possible columns exist SELECT @TOTAL = COUNT(*) FROM INFORMATION_SCHEMA.Columns as Columns JOIN INFORMATION_SCHEMA.TABLES as tables On Columns.TABLE_NAME = tables.TABLE_NAME WHERE Columns.DATA_TYPE = @COLUMNTYPE PRINT CAST(@TOTAL AS CHAR) + 'possible columns' WHILE @COUNTER < @TOTAL BEGIN SET @COUNTER = @COUNTER +1 -- ADD MAGIC HERE END
有任何想法吗?
更新 我最近发现此工具运行良好。
由于它只是开发人员(可能不一定非常优雅),如何使用TSQL生成一堆TSQL,然后将其复制回查询窗口并执行?
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
看起来不是很漂亮,但它应该可以工作…替代方法可能是将类似上面的内容插入到表变量中,然后使用遍历表变量EXEC (@Sql)。但是出于开发目的,这可能不值得…
EXEC (@Sql)