您可以搜索SQL Server 2005存储过程内容吗?

时间:2009-01-21 17:09:56

标签: sql-server sql-server-2005

SQL Server Server 2005.我正盯着一个拥有500多个存储过程的数据库,并试图收集它们如何与数据交互的复杂性 - 特别是关于它们如何插入/修改数据。我希望找到一个搜索“或”找到“查看实际过程内容的功能。这样,我可以搜索所有使用some_table_name执行任何操作的过程.SQL Management的基本查找功能Studio在打开的文件中查找,如果我已经打开了sproc,那么只有在文件中查找才会找到内容,甚至只能在... \ Local Settings \ Temp \ ~vs1011.sql临时类型文件中找到。

截至目前,我知道进入基础程序的唯一方法是右键单击并选择“修改”(或脚本存储过程为=>>创建或更改)。是否有更快/更简单的方法来搜索/检查所有的sprocs?

16 个答案:

答案 0 :(得分:26)

尝试使用第三方工具,例如ApexSQL Search(免费)或SSMS工具包(免费),除了这里已提到的工具。

过去,当我继承了500多个对象数据库时,我遇到了类似的问题。我的经验是查询还可以,但真正有用的是第三方SSMS插件。

答案 1 :(得分:24)

请勿使用INFORMATION_SCHEMA.ROUTINES。它切断了4000个字符。从sys.sql_modules获取它。

SELECT o.type_desc AS ROUTINE_TYPE
        ,o.[name] AS ROUTINE_NAME
        ,m.definition AS ROUTINE_DEFINITION
FROM sys.sql_modules AS m
INNER JOIN sys.objects AS o
    ON m.object_id = o.object_id
WHERE m.definition LIKE '%search term here%'

(如上所述,这也将返回触发器,视图和标量函数。如果需要,可以按类型排除)

答案 2 :(得分:15)

您可以使用Information_Schema.Routines视图。

select * 
FROM   INFORMATION_SCHEMA.ROUTINES 
WHERE  OBJECTPROPERTY(OBJECT_ID(SPECIFIC_NAME),'IsMSShipped') =0 
       and OBJECT_DEFINITION(OBJECT_ID(SPECIFIC_NAME)) like '%search term here%' 
       AND ROUTINE_TYPE='PROCEDURE'

答案 3 :(得分:4)

RedGate的SQL搜索产品可轻松处理此问题并且是免费的。 http://www.red-gate.com/products/sql-development/sql-search/

答案 4 :(得分:3)

批量导出到文本文件,然后使用Google桌面对其进行索引。

答案 5 :(得分:2)

select top 10 * from syscomments

你也可以找到sp_grep这是一个流行但不包含在内的程序。

答案 6 :(得分:2)

SELECT DISTINCT OBJECT_NAME(id) AS ObjectName, [Text] AS CodeSnippet 
FROM syscomments (nolock) 
WHERE [TEXT] LIKE '%Whatever You Want To Search For%'

答案 7 :(得分:2)

  1. 您可以从sys.syscomments

  2. 中选择*
  3. 如果你有VS for Database Pro。您可以从数据库中新建数据库项目并导入模式,并在项目中进行搜索。

答案 8 :(得分:1)

输入您的proc名称,其中显示Place Proc Name Here

sp_msforeachdb' SELECT DISTINCT o.name,o.xtype FROM?.dbo.syscomments c INNER JOIN?.dbo.sysobjects o ON c.id = o.id WHERE c.TEXT LIKE'&# 39;%Place Proc名称此处%'''

答案 9 :(得分:0)

使用INFORMATION_SCHEMA.ROUTINES表。

在该表中,ROUTINE_DEFINITION字段包含存储过程的文本。

SELECT
  R.SPECIFIC_NAME
 ,R.ROUTINE_DEFINITION
FROM
  MyDatabase.INFORMATION_SCHEMA.ROUTINES R
WHERE UPPER(R.ROUTINE_DEFINITION) LIKE '%' + UPPER('DELETE') + '%'

当然,你可以参数化我硬编码单词“DELETE”的地方。在SQL Server 2005中测试。

答案 10 :(得分:0)

如果你想要一个友好的界面,我可以推荐Idera的廉价SQL admin toolset。除了其他工具之外,它还有一个很好的SQL搜索实用程序,可以在sprocs(或其他任何地方)中查找字符串并帮助您导航它们。

答案 11 :(得分:0)

select o.name 
from syscomments c
    inner join sysobjects o on c.id = o.id
where text like '%tableName%'

答案 12 :(得分:0)

我使用了以前遇到的以下存储过程:

 CREATE PROC dbo.sp_search_code  
(  
@SearchStr  varchar(100),  
@RowsReturned int = NULL OUT  
)  
AS  
/*************************************************************************************************  
  Copyright © 1997 - 2002 Narayana Vyas Kondreddi. All rights reserved.  

Purpose: To search the stored proceudre, UDF, trigger code for a given keyword.  

Written by: Narayana Vyas Kondreddi  
  http://vyaskn.tripod.com  

Tested on:  SQL Server 7.0, SQL Server 2000  

Date created: January-22-2002 21:37 GMT  

Date modified: February-17-2002 19:31 GMT  

Email:   vyaskn@hotmail.com  

Examples:  

To search your database code for the keyword 'unauthorized':  
EXEC sp_search_code 'unauthorized'  

To search your database code for the keyword 'FlowerOrders' and also find out the number of hits:  
DECLARE @Hits int  
EXEC sp_search_code 'FlowerOrders', @Hits OUT  
SELECT 'Found ' + LTRIM(STR(@Hits)) + ' object(s) containing this keyword' AS Result  
*************************************************************************************************/  
BEGIN  
 SET NOCOUNT ON  

 SELECT DISTINCT USER_NAME(o.uid) + '.' + OBJECT_NAME(c.id) AS 'Object name',  
  CASE   
    WHEN OBJECTPROPERTY(c.id, 'IsReplProc') = 1   
    THEN 'Replication stored procedure'  
    WHEN OBJECTPROPERTY(c.id, 'IsExtendedProc') = 1   
    THEN 'Extended stored procedure'      
   WHEN OBJECTPROPERTY(c.id, 'IsProcedure') = 1   
    THEN 'Stored Procedure'   
   WHEN OBJECTPROPERTY(c.id, 'IsTrigger') = 1   
    THEN 'Trigger'   
   WHEN OBJECTPROPERTY(c.id, 'IsTableFunction') = 1   
    THEN 'Table-valued function'   
   WHEN OBJECTPROPERTY(c.id, 'IsScalarFunction') = 1   
    THEN 'Scalar-valued function'  
    WHEN OBJECTPROPERTY(c.id, 'IsInlineFunction') = 1   
    THEN 'Inline function'   
  END AS 'Object type',  
  'EXEC sp_helptext ''' + USER_NAME(o.uid) + '.' + OBJECT_NAME(c.id) + '''' AS 'Run this command to see the object text'  
 FROM syscomments c  
  INNER JOIN  
  sysobjects o  
  ON c.id = o.id  
 WHERE c.text LIKE '%' + @SearchStr + '%' AND  
  encrypted = 0    AND  
  (  
  OBJECTPROPERTY(c.id, 'IsReplProc') = 1  OR  
  OBJECTPROPERTY(c.id, 'IsExtendedProc') = 1 OR  
  OBJECTPROPERTY(c.id, 'IsProcedure') = 1  OR  
  OBJECTPROPERTY(c.id, 'IsTrigger') = 1  OR  
  OBJECTPROPERTY(c.id, 'IsTableFunction') = 1 OR  
  OBJECTPROPERTY(c.id, 'IsScalarFunction') = 1 OR  
  OBJECTPROPERTY(c.id, 'IsInlineFunction') = 1   
  )  

 ORDER BY 'Object type', 'Object name'  

 SET @RowsReturned = @@ROWCOUNT  
END  

答案 13 :(得分:0)

与上述答案完全不同的是编写一个简单的程序,它可以使用Microsoft.SqlServer.Management.Smo和Microsoft.SqlServer.Management.Common命名空间。使用这些可以迭代所有存储过程并从中读取文本。我用这种方式编写了一个程序来比较两个不同数据库(开发和阶段或阶段和生产)中的存储过程,并更新不同的数据库。

以下是一个例子:

using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;

namespace SqlObjectChecker
{
    internal class ProcedureUpdater
    {
        internal string UpdateProcedure(string spName, string devConnString, string prodConnString, bool updateDev)
        {
            string returnMessage = "";

            ServerConnection devConnection = new ServerConnection();
            devConnection.ConnectionString = devConnString;
            ServerConnection prodConnection = new ServerConnection();
            prodConnection.ConnectionString = prodConnString;

            try
            {
                devConnection.Connect();
                prodConnection.Connect();

                Server devServer = new Server(devConnection);
                Server prodServer = new Server(prodConnection);

                Database devDatabase = devServer.Databases["Dbname"];
                Database prodDatabase = prodServer.Databases["Dbname"];

                StoredProcedure devStoredProcedure = devDatabase.StoredProcedures[spName];
                if (devStoredProcedure != null)
                {
                    StoredProcedure prodStoredProcedure = prodDatabase.StoredProcedures[spName];
                    if (prodStoredProcedure != null)
                    {
                        if (updateDev)
                        {
                            devStoredProcedure.TextHeader = prodStoredProcedure.TextHeader;
                            devStoredProcedure.TextBody = prodStoredProcedure.TextBody;
                            devStoredProcedure.Alter();
                            returnMessage = "Dev updated";
                        }
                        else
                        {
                            prodStoredProcedure.TextHeader = devStoredProcedure.TextHeader;
                            prodStoredProcedure.TextBody = devStoredProcedure.TextBody;
                            prodStoredProcedure.Alter();
                            returnMessage = "Prod updated.";
                        }
                    }
                    else
                    {
                        returnMessage = "Prod Stored Procedure Name Found.";
                    }
                }
                else
                {
                    returnMessage = "Dev Stored Procedure Name Found.";
                }

                devConnection.Disconnect();
                prodConnection.Disconnect();
            }
            catch (Exception exception)
            {
                returnMessage = exception.Message;
            }

            return returnMessage;
        }
    }
}

答案 14 :(得分:0)

我写了一个Perl模块,允许我这样做以及更多。有了它,我可以使用SQL语句操作视图/ sprocs(和Per​​l代码)。#!/ usr / bin / perl

# 使用Codebase;

使用严格; 使用警告;

Codebase::CreateFunctions(change=>\&change);

exit;

sub change {     我的$ string = shift;

my %H=(
        23 => 30,
        25 => 26,
        27 => 30,
        28 => 30,
        29 => 30,
        31 => 24,
        32 => 24
         );
$string =~ s/InstallStatus *(<>|==|>=|<=|>|=|<) *(23|25|27|28|29|31|32)(\W)/"iNstallsTatus $1 $H{$2}$3"/iges;
return $string;
 } # change

<强> END     选择名称(entries.fullname)|| extension(entries.fullname)作为名称,grep(m / \ W(23 | 25 | 27 | 28 | 29 | 31 | 32 | InstallStatus | InstallStatusNew | InstallStatusOld)\ W /,对象.definition,3)作为列表         从条目         entries上的内连接对象.OId = objects.OId         where('(BETA:/')中的路径(entries.whatlname)和entries.kind ='view')             和(((objects.definition如m / \ W(TRACKING)\ W /)和(objects.definition如m / \ WInstallStatus(\ W | \ s)/))             或((objects.definition如m / \ W(TRACKING_LOG)\ W /)和(objects.definition如m / \ WInstallStatus(New | OLD)(\ W | \ s)/)))     限制10

select name(entries.fullname)||extension(entries.fullname) as Name, change(objects.definition) as FILE
    from entries
    inner join objects on entries.OId = objects.OId
    where (path(entries.fullname) in ('BETA:/') and entries.kind = 'view')
        and (((objects.definition like m/\W(TRACKING)\W/) and (objects.definition like m/\WInstallStatus(\W|\s)/))
        or ((objects.definition like m/\W(TRACKING_LOG)\W/) and (objects.definition like m/\WInstallStatus(New|OLD)(\W|\s)/)))
        and (change(objects.definition) <> objects.definition)
limit 10

`

答案 15 :(得分:-1)

使用MS SQL Server Management Studio;

在TreeView中查找表或SP,右键单击它并选择“查看依赖关系”

通过这种方式,您可以看到所有依赖对象。