2009-01-23 38 views
0

我有一个目录充满了来自VB6应用程序的遗留代码。搜索代码库以查找表名称

我被要求提供这个应用程序使用的所有表的列表,所以我们可以给它分配一个特殊的SQL Server用户名。

扫描代码库以引用表名称的最佳方式是什么?

一些想法我有:

  1. 搜索以下关键字: “FROM”, “更新”, “插入”,并 手动注意表名(S) 周围的那些短语。

    问题:体力劳动

  2. 大量的运行与SQL 跟踪的应用程序,并尝试运用各 功能,然后扫描日志 表名

    问题:同手动工作,再加上我可能会忽略一些不起眼的功能

任何人都可以提出更好的选择吗?

回答

4

我会从information_schema.tables中选择并将结果保存到文件以构建表格列表,然后使用bat文件或命令行正则表达式工具将表格列表用作源代码来对照源代码中的文件进行比较目录。你可以输出什么文件有一个命中,什么表名被命中(如果你感兴趣的话,命中的是哪一行)。我不是一个grep高手,但我认为这将是正确的使用工具。

编辑 根据数据访问是如何处理的,你可能想扩大搜索列表使用finstr,游标包括INFORMATION_SCHEMA.ROUTINES

编辑2方法存储的特效,也许黑暗的一面

请注意,虽然下面应该工作,如果指向错误的目录,它可能会造成严重破坏。此外,只有源代码可从服务器访问并启用xp_cmdshell时,它才会起作用。也许整个想法是邪恶的,我不知道。

create table #files (filepath varchar(4000)) 
create table #tablesfound (tablename sysname, filepath varchar(4000)) 

declare @sql nvarchar(4000) 
Declare @cmd nvarchar(400) 
Declare @dir varchar(256) 
Declare @tbl sysname 
set @dir = 'source code directory with e.g. c:\source\' 
declare crsX cursor for 
Select table_name from information_schema.tables 
open crsX 
Fetch Next from crsX into @tbl 

While (@@Fetch_Status = 0) 
Begin 
    set @cmd = 'findstr /S /M ' + quotename(@tbl, char(34)) + ' ' + @dir + '*.*' 

    insert into #files exec xp_cmdshell @cmd 
    if exists (Select 1 from #files where filepath is not null) 
    Begin 
     insert into #tablesfound (tablename, filepath) 
     Select @tbl, filepath from #files where filepath is not null 
     delete from #files 
    End 
    print @cmd 
    Fetch Next from crsX into @tbl 
End 
close crsX 
Deallocate crsX 

Select * from #tablesfound 
+0

我喜欢它。我会尽力并接受你的答案,如果它解决了。 – JosephStyons 2009-01-23 14:51:27