在代码库中搜索对表名称的引用

发布于 2024-07-11 17:28:21 字数 404 浏览 4 评论 0原文

我有一个充满 VB6 应用程序遗留代码的目录。

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

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

我的一些想法:

  1. 搜索以下关键字: “来自”、“更新”、“插入”和 手动记下表名称 围绕这些短语。

    问题:大量手动工作

  2. 使用 SQL 运行应用程序 跟踪并尝试练习每一个 函数,然后扫描日志 表名

    问题:同样的手动工作,而且我可能会忽略一些晦涩的函数

有人可以建议更好的替代方案吗?

I have a directory full of legacy code from a VB6 application.

I have been asked to provide a list of all the tables that this application uses, so we can assign a special SQL Server username to it.

What is the best way to scan a codebase for references to table names?

Some ideas I've had:

  1. Search for the following keywords:
    "FROM", "UPDATE", "INSERT", and
    manually note the table name(s)
    surrounding those phrases.

    Problem: Lots of manual work

  2. Run the application with a SQL
    Trace, and try to exercise each
    function, then scan the logs for
    table names

    Problem: Same manual work, plus I might overlook some obscure functions

Can anyone suggest better alternatives?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘿看小鸭子会跑 2024-07-18 17:28:21

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

编辑
根据数据访问的处理方式,您可能希望扩展搜索列表以包括来自 information_schema.routines 的存储过程

使用 finstr、光标以及可能的黑暗面编辑 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

I would select from information_schema.tables and save the results to file to build a table list and then use a bat file or command line regex tool to use the table list as a source for comparisons against the files in the source code directory. You could output what files had a hit, and what table names were hit (what line the hit was on if you're interested). I'm not a grep whiz, but I think that would be the right sort of tool to use.

EDIT
Depending on how the data access was handled, you might want to expand the search list to include stored procs from information_schema.routines

Edit 2 Approach using finstr, a cursor, and maybe the dark side

Please note that while the below should work, if pointed at the wrong directory, it could cause havoc. Also, it will only work if the source code is accessible from the server and xp_cmdshell is enabled. Maybe the whole idea is evil, I don't know.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文