在所有存储过程中搜索模式然后打开它进行更改的方法

2024-04-12

如何在所有存储过程中搜索某个模式,然后打开要编辑的存储过程?

SQL Server 2005 内部有内置的东西吗?

或者是否有任何第三方插件可以进行此搜索?

我也在使用 Red Gate 的 SQL Prompt,但我没有注意到该选项。

目前我正在使用以下命令进行搜索

SELECT ROUTINE_SCHEMA, ROUTINE_NAME, ROUTINE_DEFINITION 
    FROM INFORMATION_SCHEMA.ROUTINES 
    WHERE ROUTINE_DEFINITION LIKE '%tblVacationAllocationItem%' 
    AND ROUTINE_TYPE='PROCEDURE'
    ORDER BY ROUTINE_SCHEMA, ROUTINE_NAME

这工作得很好,但它在其中一列中返回存储过程的内容,这很难阅读。所以我必须使用对象资源管理器来查找并打开存储过程以查看完整内容。

Edited: SQL依赖跟踪器 http://www.red-gate.com/products/SQL_Dependency_Tracker/index.htm允许您使用一系列图形布局动态探索所有数据库对象依赖关系。这看起来可以回答搜索模式时的一些问题。还有其他类似于 SQL Dependency Tracker 的软件吗?

Edited: SQL 搜索 http://www.red-gate.com/products/sql-development/sql-search/ by Redgate http://www.red-gate.com/是用于搜索的工具。它会在您键入时进行搜索(类似于 Bing 或 Google)。它也很快!现在(2/24/2011)价格仍然是免费的,但我认为在某个时候他们将开始收费。


有一个名为 sp_grep 的开源存储过程,它允许您根据数据库对象的 DDL/代码查找数据库对象。我一直使用这个过程来查找满足特定条件的对象。这在数据库重构中非常有用。

要以编程方式打开和修改 SQL 对象,您可以在任何 .Net 应用程序中使用 SQLDMO 对象。Here http://www.simple-talk.com/sql/database-administration/automating-common-sql-server-tasks-using-dmo/是一些使用 SQLDMO 的示例。

示例: exec sp_grep 'colA='

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO

/*********************************************************************
* Stored procedure  sp_grep 
* SQL Server:   Microsoft SQL Server 6.0, 4.21 for Windows NT, 
*               Microsoft SQL Server 4.2 for OS/2.
* Author:       Andrew Zanevsky, AZ Databases, Inc.
* Version/Date: Version 1.1,  October 26, 1995
* Description:  Searches syscomments table in the current database
*               for occurences of a combination of strings. 
*               Correclty handles cases when a substring begins in 
*               one row of syscomments and continues in the next. 
* Parameters: - @parameter describes the search:
*               string1 {operation1 string2} {operation2 string 3} ...
*               where - stringN is a string of characters enclosed in
*                       curly brackets not longer than 80 characters. 
*                       Brackets may be omitted if stringN does not 
*                       contain spaces or characters: +,-,&;
*                     - operationN is one of the characters: +,-,&.
*               Parameter is interpreted as follows:
*               1.Compose the list of all objects where string1 occurs.
*               2.If there is no more operations in the parameter,
*                 then display the list and stop. Otherwise continue.
*               3.If the next operation is + then add to the list all 
*                   objects where the next string occurs;
*                 else if the next operation is - then delete from the 
*                   list all objects where the next string occurs;
*                 else if the next operation is & then delete from the 
*                   list all objects where the next string does not 
*                   occur (leave in the list only those objects where 
*                   the next string occurs);
*               4.Goto step 2.
*               Parameter may be up to 255 characters long, and may not 
*               contain <CarriageReturn> or <LineFeed> characters.
*               Please note that operations are applied in the order
*               they are used in the parameter string (left to right). 
*               There is no other priority of executing them. Every 
*               operation is applied to the list combined as a result 
*               of all previous operations.
*               Number of spaces between words of a string matters in a
*               search (e.g. "select *" is not equal to "select  *").
*               Short or frequently used strings (such as "select") may 
*               produce a long result set.
*
*             - @case: i = insensitive / s = sensitive (default)
*               Insensitive search is performed regardless of this parameter 
*               if SQL Server is set up with case insensitive sort order.
*
* Examples:     sp_grep employee 
*                 list all objects where string 'employee' occurs;
*               sp_grep employee, i
*                 list all objects where string 'employee' occurs in 
*                 any case (upper, lower, or mixed), such as 
*                 'EMPLOYEE', 'Employee', 'employee', etc.;
*               sp_grep 'employee&salary+department-trigger'
*                 list all objects where either both strings 'employee'
*                 and 'salary' occur or string 'department' occurs, and 
*                 string 'trigger' does not occur;
*               sp_grep '{select FirstName + LastName}'
*                 list all objects where string 
*                 "select FirstName + LastName" occurs;
*               sp_grep '{create table}-{drop table}'
*                 list all objects where tables are created and not 
*                 dropped.
*                 
**********************************************************************/

-- sp_grep   v1.0 03/16/1995, v1.1 10/26/1995
-- Author:   Andrew Zanevsky, AZ Databases, Inc. 
-- E-mail:   [email protected] /cdn-cgi/l/email-protection
ALTER proc [dbo].[sp_grep] @parameter varchar(255) = null, @case char(1) = 's'
as

declare @str_no          tinyint, 
        @msg_str_no      varchar(3),
        @operation       char(1), 
        @string          varchar(80), 
        @oper_pos        smallint,
        @context         varchar(255),
        @i               tinyint,
        @longest         tinyint,
        @msg             varchar(255)

if @parameter is null /* provide instructions */
begin
    print 'Execute sp_grep "{string1}operation1{string2}operation2{string3}...", [case]'
    print '- stringN is a string of characters up to 80 characters long, '
    print '  enclosed in curly brackets. Brackets may be omitted if stringN '
    print '  does not contain leading and trailing spaces or characters: +,-,&.'
    print '- operationN is one of the characters: +,-,&. Interpreted as or,minus,and.'
    print '  Operations are executed from left to right with no priorities.'
    print '- case: specify "i" for case insensitive comparison.'
    print 'E.g. sp_grep "alpha+{beta gamma}-{delta}&{+++}"'
    print '     will search for all objects that have an occurence of string "alpha"'
    print '     or string "beta gamma", do not have string "delta", '
    print '     and have string "+++".'
    return
end

/* Check for <CarriageReturn> or <LineFeed> characters */
if charindex( char(10), @parameter ) > 0 or charindex( char(13), @parameter ) > 0
begin
    print 'Parameter string may not contain <CarriageReturn> or <LineFeed> characters.'
    return
end

if lower( @case ) = 'i'
        select  @parameter = lower( ltrim( rtrim( @parameter ) ) )
else
        select  @parameter = ltrim( rtrim( @parameter ) )

create table #search ( str_no tinyint, operation char(1), string varchar(80), last_obj int )
create table #found_objects ( id int, str_no tinyint )
create table #result ( id int )

/* Parse the parameter string */
select @str_no = 0
while datalength( @parameter ) > 0
begin
  /* Get operation */
  select @str_no = @str_no + 1, @msg_str_no = rtrim( convert( char(3), @str_no + 1 ) )
  if @str_no = 1
    select  @operation = '+'
  else 
  begin
    if substring( @parameter, 1, 1 ) in ( '+', '-', '&' )
        select  @operation = substring( @parameter, 1, 1 ),
                @parameter = ltrim( right( @parameter, datalength( @parameter ) - 1 ) )
    else
    begin
        select @context = rtrim( substring( 
                        @parameter + space( 255 - datalength( @parameter) ), 1, 20 ) )
        select @msg = 'Incorrect or missing operation sign before "' + @context + '".'
        print  @msg 
        select @msg = 'Search string ' + @msg_str_no + '.'
        print  @msg 
        return
    end
  end

  /* Get string */
  if datalength( @parameter ) = 0
  begin
      print 'Missing search string at the end of the parameter.'
      select @msg = 'Search string ' + @msg_str_no + '.'
      print  @msg 
      return
  end
  if substring( @parameter, 1, 1 ) = '{'
  begin
      if charindex( '}', @parameter ) = 0
      begin
          select @context = rtrim( substring( 
                      @parameter + space( 255 - datalength( @parameter) ), 1, 200 ) )
          select @msg = 'Bracket not closed after "' + @context + '".'
          print  @msg 
          select @msg = 'Search string ' + @msg_str_no + '.'
          print  @msg 
          return
      end
      if charindex( '}', @parameter ) > 82
      begin
          select @context = rtrim( substring( 
                      @parameter + space( 255 - datalength( @parameter) ), 2, 20 ) )
          select @msg = 'Search string ' + @msg_str_no + ' is longer than 80 characters.'
          print  @msg 
          select @msg = 'String begins with "' + @context + '".'
          print  @msg 
          return
      end        
      select  @string    = substring( @parameter, 2, charindex( '}', @parameter ) - 2 ),
              @parameter = ltrim( right( @parameter, 
                              datalength( @parameter ) - charindex( '}', @parameter ) ) )
  end
  else
  begin
      /* Find the first operation sign */
      select @oper_pos = datalength( @parameter ) + 1
      if charindex( '+', @parameter ) between 1 and @oper_pos
          select @oper_pos = charindex( '+', @parameter )
      if charindex( '-', @parameter ) between 1 and @oper_pos
          select @oper_pos = charindex( '-', @parameter )
      if charindex( '&', @parameter ) between 1 and @oper_pos
          select @oper_pos = charindex( '&', @parameter )

      if @oper_pos = 1
      begin
          select @context = rtrim( substring( 
                      @parameter + space( 255 - datalength( @parameter) ), 1, 20 ) )
          select @msg = 'Search string ' + @msg_str_no + 
                        ' is missing, before "' + @context + '".'
          print  @msg 
          return
      end        
      if @oper_pos > 81
      begin
          select @context = rtrim( substring( 
                      @parameter + space( 255 - datalength( @parameter) ), 1, 20 ) )
          select @msg = 'Search string ' + @msg_str_no + ' is longer than 80 characters.'
          print  @msg 
          select @msg = 'String begins with "' + @context + '".'
          print  @msg 
          return
      end        

      select  @string    = substring( @parameter, 1, @oper_pos - 1 ),
              @parameter = ltrim( right( @parameter, 
                              datalength( @parameter ) - @oper_pos + 1 ) )
  end
  insert #search values ( @str_no, @operation, @string, 0 )

end
select @longest = max( datalength( string ) ) - 1
from   #search
/* ------------------------------------------------------------------ */
/* Search for strings */
if @case = 'i'
begin
    insert #found_objects
    select a.id, c.str_no
    from   syscomments a, #search c
    where  charindex( c.string, lower( a.text ) ) > 0

    insert #found_objects
    select a.id, c.str_no
    from   syscomments a, syscomments b, #search c
    where  a.id        = b.id
    and    a.number    = b.number
    and    a.colid + 1 = b.colid
    and    charindex( c.string, 
                lower( right( a.text, @longest ) + 
/*                     space( 255 - datalength( a.text ) ) +*/
                       substring( b.text, 1, @longest ) ) ) > 0
end
else
begin
    insert #found_objects
    select a.id, c.str_no
    from   syscomments a, #search c
    where  charindex( c.string, a.text ) > 0

    insert #found_objects
    select a.id, c.str_no
    from   syscomments a, syscomments b, #search c
    where  a.id        = b.id
    and    a.number    = b.number
    and    a.colid + 1 = b.colid
    and    charindex( c.string, 
                right( a.text, @longest ) + 
/*              space( 255 - datalength( a.text ) ) +*/
                substring( b.text, 1, @longest ) ) > 0
end
/* ------------------------------------------------------------------ */
select distinct str_no, id into #dist_objects from #found_objects
create unique clustered index obj on #dist_objects  ( str_no, id )

/* Apply one operation at a time */
select @i = 0
while @i < @str_no
begin
    select @i = @i + 1
    select @operation = operation from #search where str_no = @i

    if @operation = '+'
        insert #result
        select id
        from   #dist_objects 
        where  str_no = @i
    else if @operation = '-'
        delete #result
        from   #result a, #dist_objects b
        where  b.str_no = @i
        and    a.id = b.id
    else if @operation = '&'
        delete #result
        where  not exists 
                ( select 1
                  from   #dist_objects b
                  where  b.str_no = @i
                  and    b.id = #result.id )
end

/* Select results */
select distinct id into #dist_result from #result

/* The following select has been borrowed from the sp_help 
** system stored procedure, and modified. */
select  Name        = o.name,
        /* Remove 'convert(char(15)' in the following line 
        ** if user names on your server are longer. */
        Owner       = convert( char(15), user_name(uid) ),
        Object_type = substring(v.name + x.name, 1, 16)
from    #dist_result           d,
        sysobjects             o, 
        master.dbo.spt_values  v,
        master.dbo.spt_values  x
where   d.id = o.id
/* SQL Server version 6.x uses 15, prior versions use 7 in expression below */
and     o.sysstat & ( 7 + 8 * sign( charindex( '6.', @@version ) ) ) = v.number
and     v.type = "O"
and     x.type = "R"
and     o.userstat & -32768 = x.number
order by Object_type desc, Name asc
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在所有存储过程中搜索模式然后打开它进行更改的方法 的相关文章

  • SQLite (Android):使用 ORDER BY 更新查询

    Android SQLite 我想要在 myTable 中的其他行之间插入行在android中使用SQLite 为此 我尝试增加从第 3 行开始的所有行的 id 这样 我就可以在位置 3 处插入新行 myTable 的主键是列 id 表中没
  • TSQL - 生成文字浮点值

    我理解比较浮点数时遇到的许多问题 并对它们在这种情况下的使用感到遗憾 但我不是表格作者 只有一个小障碍需要克服 有人决定使用浮点数 就像您期望使用 GUID 一样 我需要检索具有特定浮点值的所有记录 sp help MyTable Colu
  • ASP SQL Server 连接

  • 如何连续添加起始行和下一行的值

    我只想创建一个 sql 查询 结果就像图片上的那样 类似于 SQL 中的斐波那契数列 Ex Column 1 10 则 Result 列的值为 Result 10 因为这是第一行 然后假设column1第二行的值为50 那么Result第二
  • 自动删除主键序列中的间隙

    我正在创建一个网页 该网页根据用户操作将数据存储到 MySQL 数据库中 数据库有很多行 行的主键是列 rowID 它只是按顺序对行进行编号 例如 1 2 3 4 用户可以选择删除行 问题是当用户删除最后一行以外的行时 rowID 中有一个
  • 在对象数组内的特定 JSON 值上创建索引

    假设我的表中有一个 varchar 列 其结构如下 Response DataArray Type Address Value 123 Fake St Type Name Value John Doe 我想在 DataArray 数组元素的
  • 一个表可以有多个主键吗?

    我现在很困惑 也许你可以帮助我更好地理解这个问题 即一个表可以有两个主键 如果是 那么如何 如果没有 那为什么 您询问是否可以有多个主键field你当然可以 您只能有一个主键 但它可以包含唯一标识行所需的任意数量的列 创建表时使用类似这样的
  • 从字符串中删除某些字符

    我正在尝试删除某些字符 目前我的输出如下cityname district但我想删除cityname SELECT Ort FROM dbo tblOrtsteileGeo WHERE GKZ 06440004 Output B dinge
  • 使用联接更新表?

    我正在尝试使用表 B 中的数据更新表 A 我以为我可以做这样的事情 update A set A DISCOUNT 3 from INVOICE ITEMS A join ITEM PRICE QUNTITY B on A ITEM PRI
  • 当从属文本框中没有输入文本时,如何让 gridview 显示所有表格行?

    下面的代码可以正常工作 并根据文本框中输入的文本过滤我的网格视图 当我的文本框中没有输入任何文本时 我没有得到任何结果 并且无法理解为什么 我的问题 如何让gridview显示all当文本框中没有输入文本时表行 MSSQL Search n
  • 包含列和行总计的 SQL 数据透视表

    我正在尝试将行和列总计添加到该数据透视表中 create table test4 city nvarchar 10 race nvarchar 30 sex nvarchar 10 age int insert into test4 val
  • 选择多列 按一列分组 按计数排序

    我在Oracle中有以下数据集 c1 c2 c3 1A2 cat black 1G2 dog red B11 frog green 1G2 girl red 试图得到以下结果 基本上我首先尝试获取具有重复 c1 的行 c1 c2 c3 1G
  • Snowflake 中的动态 SQL

    当我在雪花中运行动态 SQL 时 遇到以下错误 未完成对 SQL MAIN 的分配 因为值超出了变量的大小限制 它的大小是263 限制为 256 内部存储大小以字节为单位 这是代码 SET v G 1 SET v G1 v G VARCHA
  • H2 SQL 日期比较

    在 H2 数据库中 如何在 TIMESTAMP 类型的列上运行查询 SELECT FROM RECORDS WHERE TRAN DATE lt 2012 07 24 Try 2012 07 24
  • SQL 使用另一列的键和最大值设置列

    我需要根据同一 ID 的 duration 列的最大值更新 max register 列 将值设置为 1 其他值设置为 0 初始表 Id duration max register 1 0 0 1 7 0 1 3 0 2 10 0 2 5
  • 如何用约束标记一大组“传递群”?

    在 NealB解决方案之后进行编辑 与以下解决方案相比 NealB的解决方案非常非常快任何另一个 https stackoverflow com q 18033115 answers and 提出了关于 添加约束以提高性能 的新问题 Nea
  • Oracle SQL 函数中可以有 commit 语句吗

    在 SQL 函数中使用 COMMIT 语句是否可能 有意义 从技术上来说 答案是肯定的 你can请执行下列操作 create or replace function committest return number as begin upd
  • 将布尔参数传递给 SQL Server 存储过程

    我早些时候问过这个问题 我以为我找到了问题所在 但我没有 我在将布尔参数传递给存储过程时遇到问题 这是我的 C 代码 public bool upload false protected void showDate object sende
  • 使用来自另一个数据库的选择查询更新 mysql 表

    我有两个数据库 我想用另一个数据库表中的值更新一个表 我正在使用以下查询 但它不起作用 UPDATE database1 table1 SET field2 database2 table1 field2 WHERE database1 t
  • WHERE NOT EXIST 附近的语法错误

    我在堆栈中搜索 但没有一个达到最终答案 我的查询是这样的 INSERT INTO user username frequence autoSend VALUES feri2 3 1 WHERE NOT EXISTS SELECT FROM

随机推荐

  • 我需要知道如何创建交叉表查询

    我想动态生成状态列 有三张表 资产 资产类型 资产状态 Table assets assetid int assettag varchar 25 assettype int assetstatus int Table assettypes
  • 在 IntelliJ Idea IDE 中禁用单击、拖动、剪切和粘贴

    在我的 IntelliJ Idea 13 1 2 IDE 中 我不断遇到通过笔记本电脑触摸板单击并拖动进行选择的情况 我总是不小心点击并拖动文本和剪切线 我在选项和设置面板中搜索了 单击和拖动 一词 但我没有找到关闭此功能的方法 Intel
  • Python dbfpy 和 FoxPro

    我在这里使用一种古老的数据库格式 dbf 文件 不要问为什么 只知道某个软件决定扩展foxpro支持 因为微软决定扩展foxpro支持 现在 我在特定文件上收到以下错误 我已成功加载另一个文件 我很好奇该数据库是否有问题 我确信您可能需要查
  • xlsxwriter set_column 隐藏不起作用

    我试图用 XlsxWriter 隐藏一个列 但它似乎不起作用 函数返回0表示该列已成功隐藏 我使用的是带有数字的列 而不是带有字母的列 workbook get worksheet by name SENSORS set column 5
  • 更改 Visual Studio 2005 中的应用程序图标?

    我想为我的游戏的演示版本使用不同的图标 并且我正在使用与完整版本不同的构建配置来构建演示 使用预处理器定义来锁定某些内容 使用不同的图形 有没有办法让 Visual Studio 在演示配置中为应用程序图标使用不同的图标 但继续为完整版本的
  • 更改 a:hover 上 li 的背景图像

    我有一个菜单 div div
  • Heroku 上的 Django 部署问题与正在运行的应用程序的精确克隆:PUSH REJECTED ERROR

    我在 Heroku 上有一个 Django 应用程序 我在同一个 Heroku 帐户上设置了另一个应用程序 现在我想要第一个应用程序的另一个实例 我刚刚克隆了第一个应用程序并推送到新创建的应用程序中 但它不起作用 做的时候出现这个错误git
  • 编译时 -pthread 标志的含义

    在各种多线程 C 和 C 项目中 我看到了 pthread标志应用于编译和链接阶段 而其他人根本不使用它 只是通过 lpthread到链接阶段 不编译链接有没有危险 pthread标志 即什么 pthread实际上呢 我主要对 Linux
  • npm 安装后如何引用 Google Material-Design-Icons?

    所以做完之后npm install material design icons 我如何在我的React应用 所包括的方法here https google github io material design icons icon font
  • Laravel - 缓存 Eloquent 并频繁更新

    是否可以对经常修改的对象使用缓存 例如 假设我们有一个 BlogPost 对象 并且有一个经常更改的 num of views 列 以及其他列 是否可以更新缓存和数据库中的 num of views 字段 而不破坏缓存对象并重新创建它 我可
  • 如何通过 UIAutomation 处理“_APPNAME_想使用您当前的位置”警报

    好吧 这让我抓狂 我正在运行一个小型 CI 构建系统 我正在使用 UIAutomation 对我的应用程序进行 UI 测试 由于应用程序使用 CoreLocation 因此第一次启动应用程序时 我会收到一条小警报 要求我确认是否希望跟踪我的
  • Swig (Node.js) 中的 JSON.parse() ?

    当我陷入困境时 我试图从 Jade 切换到 Swig 被 Swig 的疯狂性能所吸引 作为我的 Express 模板引擎 我将一系列序列化 JSON 从 Express 发送到 Swig 并使用此循环检索 Swig 中的数据这里 ul if
  • node.js fs.writeFile 未完全覆盖文件

    我有一个长度为 X 的文件 它正在被长度为 X Y 的字符串覆盖 问题是 该文件仍然保留 X Y 过去的信息 因此它与第一个较长的文件一样长 所以这是我的测试输出 它让我适合 文件开头为 sOption1 String nOption2 2
  • SQL 错误:ORA-00913:值太多

    两个表在表名 列名 数据类型和大小方面相同 这些表位于不同的数据库中 但我习惯于 当前登录 hr 用户 insert into abc employees select from employees where employee id 10
  • Stripe 结账模式的事件或方法

    有什么方法可以在 Stripe Checkout 模式关闭时触发事件吗 Stripe 的模式关闭和响应传递之间存在大约 0 5 1 秒的延迟 那时 用户可能会点击离开页面等 为了解决这个问题 我们可以执行一些操作 例如禁用所有链接或在页面上
  • 如何在现有 Windows 应用程序中获得 ATL 支持

    我正在 Visual Studio 2012 中使用 Qt 5 3 1 构建一个应用程序 我还想使用一个硬件库 这需要我向项目添加一个简单的 ATL 对象 这可以通过使用 Visual Studio 向导来完成 该向导抱怨我的项目既不是 M
  • 如何确定函数特化的主要模板?

    函数模板专业化的主要模板通常是非常直观的 但是 我正在寻找正式的规则来理解更令人惊讶的情况 例如 template
  • com.google.gwt.user.client.rpc.InknownRemoteServiceException

    我的 GWT 应用程序有问题 我部署在 Jetty 服务器上并运行 但是当我执行服务器调用 GWT 服务器包上的类 时 服务器返回错误消息 消息是 7 0 6 http localhost zbapp zb app A31E1254E17F
  • 抑制 make clean 中的消息(Makefile 无提示删除)

    我想知道如何避免 Makefile 中出现一些回声 clean rm fr o 该规则将打印 gt make clean rm fr o gt 我怎样才能避免这种情况 首先 实际的命令必须位于下一行 或者至少 GNU Make 是这样 它可
  • 在所有存储过程中搜索模式然后打开它进行更改的方法

    如何在所有存储过程中搜索某个模式 然后打开要编辑的存储过程 SQL Server 2005 内部有内置的东西吗 或者是否有任何第三方插件可以进行此搜索 我也在使用 Red Gate 的 SQL Prompt 但我没有注意到该选项 目前我正在