我有一个由大量表组成的 informix 数据库。
我知道某个表中某处有一个字符串“示例”,但不知道它是哪个表或哪个列。(我知道这是一个非常罕见的情况)
由于表较多,无法手动查找。我如何在这个大型数据库中找到这个值?有什么查询可以找到吗?
提前致谢!
In Informix, determining the columns that contain character data is fiddly, but doable. Fortunately, you're unlikely to be using the esoteric features such as DISTINCT TYPE that would make the search harder. (Alphadogg's suggestion of unload - using dbexport as I note in my comment to his answer - makes sense, especially if the text might appear in a CLOB or TEXT field.) You need to know that types CHAR, NCHAR, VARCHAR, NVARCHAR, and LVARCHAR have type numbers of 0, 13, 15, 16 and 43. Further, if the column is NOT NULL, 256 is added to the number. Hence, the character data columns in the database are found via this query:
SELECT t.owner, t.tabname, c.colname, t.tabid, c.colno
FROM "informix".systables t, "informix".syscolumns c
WHERE t.tabid = c.tabid
AND c.coltype IN (0, 13, 15, 16, 43, 256, 269, 271, 272, 299)
AND t.tabtype = 'T' -- exclude views, synonyms, etc.
AND t.tabid >= 100 -- exclude the system catalog
ORDER BY t.owner, t.tabname, c.colno;
This generates the list of places to search. You could get fancy and have the SELECT generate a string suitable for resubmission as a query - that's left as an exercise for the reader.
通常,您有两种方法。
一种方法是将每个表转储到单独的文本文件并 grep 文件。这可以在小型数据库上手动完成,或者在较大数据库的情况下通过脚本完成。查看 UNLOAD 和 dbaccess。BASH 脚本中的示例:(您需要在脚本中静态或通过查询生成表列表。)
unload_tables () {
for table in ${TABLE_LIST} do
dbaccess database_name << EOF
unload to "${OUT_PATH}/${table}/.out"
select * from $table;
EOF
done
}
或者,这有点复杂。systables
您可以使用and以类似的自动方式为数据库中的每个表和列创建特定的 SELECT(按“示例”过滤每个列)syscolumns
,然后运行每个 sql。
例如,此查询显示所有表中的所有列:
SELECT tabname, colno, colname, coltype, collength
FROM systables a, syscolumns b
WHERE a.tabid = b.tabid
调整这一点很容易,这样 SELECT 返回一个格式正确的 SQL 字符串,允许您查询数据库以匹配“示例”。抱歉,我没有现成的完整解决方案,但是如果您在 Google 上搜索“informix systables syscolumns”,您会看到很多可以使用这些信息的方式。