天天看點

[轉貼]How do I find a stored procedure containing ?

http://databases.aspfaq.com/database/how-do-i-find-a-stored-procedure-containing-text.html

I see this question at least once a week. Usually, people are trying to find all the stored procedures that reference a specific object. While I think that the best place to do this kind of searching is through your source control tool (you do keep your database objects in source control, don't you?), there are certainly some ways to do this in the database. 

Let's say you are searching for 'foobar' in all your stored procedures. You can do this using the INFORMATION_SCHEMA.ROUTINES view, or syscomments: 

[轉貼]How do I find a stored procedure containing ?

SELECT  ROUTINE_NAME, ROUTINE_DEFINITION 

[轉貼]How do I find a stored procedure containing ?

              FROM  INFORMATION_SCHEMA.ROUTINES 

[轉貼]How do I find a stored procedure containing ?

               WHERE  ROUTINE_DEFINITION  LIKE   ' %foobar% '  

[轉貼]How do I find a stored procedure containing ?

              AND  ROUTINE_TYPE = ' PROCEDURE '  

If you want to present these results in ASP, you can use the following code, which also highlights the searched string in the body (the hW function is based on Article #2344): 

[轉貼]How do I find a stored procedure containing ?
[轉貼]How do I find a stored procedure containing ?

<% ...  

[轉貼]How do I find a stored procedure containing ?

    set conn = CreateObject("ADODB.Connection") 

[轉貼]How do I find a stored procedure containing ?

    conn.Open = "<connection string>" 

[轉貼]How do I find a stored procedure containing ?
[轉貼]How do I find a stored procedure containing ?

    ' String we're looking for: 

[轉貼]How do I find a stored procedure containing ?

    str = "foobar" 

[轉貼]How do I find a stored procedure containing ?
[轉貼]How do I find a stored procedure containing ?

    sql = "SELECT ROUTINE_NAME, ROUTINE_DEFINITION " & _ 

[轉貼]How do I find a stored procedure containing ?

        "FROM INFORMATION_SCHEMA.ROUTINES " & _ 

[轉貼]How do I find a stored procedure containing ?

        "WHERE ROUTINE_DEFINITION LIKE '%" & str & "%' " & _ 

[轉貼]How do I find a stored procedure containing ?

        " AND ROUTINE_TYPE='PROCEDURE'" 

[轉貼]How do I find a stored procedure containing ?
[轉貼]How do I find a stored procedure containing ?

    set rs = conn.execute(sql) 

[轉貼]How do I find a stored procedure containing ?

    if not rs.eof then 

[轉貼]How do I find a stored procedure containing ?

        do while not rs.eof 

[轉貼]How do I find a stored procedure containing ?

            s = hW(str, server.htmlEncode(rs(1))) 

[轉貼]How do I find a stored procedure containing ?

            s = replace(s, vbTab, "&nbsp;&nbsp;&nbsp;&nbsp;") 

[轉貼]How do I find a stored procedure containing ?

            s = replace(s, vbCrLf, "<br>") 

[轉貼]How do I find a stored procedure containing ?

            response.write "<b>" & rs(0) & "</b><p>" & s & "<hr>" 

[轉貼]How do I find a stored procedure containing ?

            rs.movenext 

[轉貼]How do I find a stored procedure containing ?

        loop 

[轉貼]How do I find a stored procedure containing ?

    else 

[轉貼]How do I find a stored procedure containing ?

        response.write "No procedures found." 

[轉貼]How do I find a stored procedure containing ?

    end if 

[轉貼]How do I find a stored procedure containing ?
[轉貼]How do I find a stored procedure containing ?

    function hW(strR, tStr) 

[轉貼]How do I find a stored procedure containing ?

        w = len(strR)  

[轉貼]How do I find a stored procedure containing ?

        do while instr(lcase(tStr), lcase(strR)) > 0  

[轉貼]How do I find a stored procedure containing ?

            cPos = instr(lcase(tStr), lcase(strR))  

[轉貼]How do I find a stored procedure containing ?

            nStr = nStr & _  

[轉貼]How do I find a stored procedure containing ?

                left(tStr, cPos - 1) & _  

[轉貼]How do I find a stored procedure containing ?

                "<b>" & mid(tStr, cPos, w) & "</b>"  

[轉貼]How do I find a stored procedure containing ?

            tStr = right(tStr, len(tStr) - cPos - w + 1)  

[轉貼]How do I find a stored procedure containing ?

        loop  

[轉貼]How do I find a stored procedure containing ?

        hW = nStr & tStr  

[轉貼]How do I find a stored procedure containing ?

    end function 

[轉貼]How do I find a stored procedure containing ?
[轉貼]How do I find a stored procedure containing ?

    rs.close: set rs = nothing 

[轉貼]How do I find a stored procedure containing ?

    conn.close: set conn = nothing 

[轉貼]How do I find a stored procedure containing ?

%>  

Another way to perform a search is through the system table syscomments: 

[轉貼]How do I find a stored procedure containing ?

SELECT   OBJECT_NAME (id) 

[轉貼]How do I find a stored procedure containing ?

     FROM  syscomments 

[轉貼]How do I find a stored procedure containing ?

     WHERE   [ text ]   LIKE   ' %foobar% '  

[轉貼]How do I find a stored procedure containing ?

     AND   OBJECTPROPERTY (id,  ' IsProcedure ' )  =   1  

[轉貼]How do I find a stored procedure containing ?

     GROUP   BY   OBJECT_NAME (id)  

Now, why did I use GROUP BY? Well, there is a curious distribution of the procedure text in system tables if the procedure is greater than 8KB. So, the above makes sure that any procedure name is only returned once, even if multiple rows in or syscomments draw a match. But, that begs the question, what happens when the text you are looking for crosses the boundary between rows? Here is a method to create a simple stored procedure that will do this, by placing the search term (in this case, 'foobar') at around character 7997 in the procedure. This will force the procedure to span more than one row in syscomments, and will break the word 'foobar' up across rows. 

Run the following query in Query Analyzer, with results to text (CTRL+T): 

[轉貼]How do I find a stored procedure containing ?

SET  NOCOUNT  ON  

[轉貼]How do I find a stored procedure containing ?

SELECT   ' SELECT  ''' + REPLICATE ( ' x ' ,  7936 ) + ' foobar '  

[轉貼]How do I find a stored procedure containing ?

SELECT   REPLICATE ( ' x ' ,  500 ) + ''''  

This will yield two results. Copy them and inject them here: 

[轉貼]How do I find a stored procedure containing ?

CREATE   PROCEDURE  dbo.x 

[轉貼]How do I find a stored procedure containing ?

AS  

[轉貼]How do I find a stored procedure containing ?

BEGIN  

[轉貼]How do I find a stored procedure containing ?

     SET  NOCOUNT  ON  

[轉貼]How do I find a stored procedure containing ?

     <<  put both results  on  this line  >>  

[轉貼]How do I find a stored procedure containing ?

END  

[轉貼]How do I find a stored procedure containing ?

GO  

Now, try and find this stored procedure in INFORMATION_SCHEMA.ROUTINES or syscomments using the same search filter as above. The former will be useless, since only the first 8000 characters are stored here. The latter will be a little more useful, but initially, will return 0 results because the word 'foobar' is broken up across rows, and does not appear in a way that LIKE can easily find it. So, we will have to take a slightly more aggressive approach to make sure we find this procedure. Your need to do this, by the way, will depend partly on your desire for thoroughness, but more so on the ratio of stored procedures you have that are greater than 8KB. In all the systems that I manage, I don't have more than a handful that approach this size, so this isn't something I reach for very often. Maybe for you it will be more useful. 

First off, to demonstrate a little better (e.g. by having more than one procedure that exceeds 8KB), let's create a second procedure just like above. Let's call it dbo.y, but this time remove the word 'foobar' from the middle of the SELECT line. 

[轉貼]How do I find a stored procedure containing ?

CREATE   PROCEDURE  dbo.y 

[轉貼]How do I find a stored procedure containing ?

AS  

[轉貼]How do I find a stored procedure containing ?

BEGIN  

[轉貼]How do I find a stored procedure containing ?

     SET  NOCOUNT  ON  

[轉貼]How do I find a stored procedure containing ?

     <<  put both results  on  this line, but  replace   ' foobar '   with  something  else   >>  

[轉貼]How do I find a stored procedure containing ?

END  

[轉貼]How do I find a stored procedure containing ?

GO

Basically, what we're going to do next is loop through a cursor, for all procedures that exceed 8KB. We can get this list as follows: 

[轉貼]How do I find a stored procedure containing ?

SELECT   OBJECT_NAME (id) 

[轉貼]How do I find a stored procedure containing ?

     FROM  syscomments 

[轉貼]How do I find a stored procedure containing ?

     WHERE   OBJECTPROPERTY (id,  ' IsProcedure ' )  =   1  

[轉貼]How do I find a stored procedure containing ?

     GROUP   BY   OBJECT_NAME (id) 

[轉貼]How do I find a stored procedure containing ?

     HAVING   COUNT ( * )  >   1  

We'll need to create a work table to hold the results as we loop through the procedure, and we'll need to use UPDATETEXT to append each row with the new 8000-or-less chunk of the stored procedure code. 

[轉貼]How do I find a stored procedure containing ?

--  create temp table 

[轉貼]How do I find a stored procedure containing ?

CREATE   TABLE  # temp  

[轉貼]How do I find a stored procedure containing ?

[轉貼]How do I find a stored procedure containing ?

    Proc_id  INT , 

[轉貼]How do I find a stored procedure containing ?

    Proc_Name SYSNAME, 

[轉貼]How do I find a stored procedure containing ?

    Definition  NTEXT  

[轉貼]How do I find a stored procedure containing ?

[轉貼]How do I find a stored procedure containing ?
[轉貼]How do I find a stored procedure containing ?

--  get the names of the procedures that meet our criteria 

[轉貼]How do I find a stored procedure containing ?

INSERT  # temp (Proc_id, Proc_Name) 

[轉貼]How do I find a stored procedure containing ?

     SELECT  id,  OBJECT_NAME (id) 

[轉貼]How do I find a stored procedure containing ?

         FROM  syscomments 

[轉貼]How do I find a stored procedure containing ?

         WHERE   OBJECTPROPERTY (id,  ' IsProcedure ' )  =   1  

[轉貼]How do I find a stored procedure containing ?

         GROUP   BY  id,  OBJECT_NAME (id) 

[轉貼]How do I find a stored procedure containing ?

         HAVING   COUNT ( * )  >   1  

[轉貼]How do I find a stored procedure containing ?
[轉貼]How do I find a stored procedure containing ?

--  initialize the NTEXT column so there is a pointer 

[轉貼]How do I find a stored procedure containing ?

UPDATE  # temp   SET  Definition  =   ''  

[轉貼]How do I find a stored procedure containing ?
[轉貼]How do I find a stored procedure containing ?

--  declare local variables 

[轉貼]How do I find a stored procedure containing ?

DECLARE   

[轉貼]How do I find a stored procedure containing ?

     @txtPval   binary ( 16 ),  

[轉貼]How do I find a stored procedure containing ?

     @txtPidx   INT ,  

[轉貼]How do I find a stored procedure containing ?

     @curName  SYSNAME, 

[轉貼]How do I find a stored procedure containing ?

     @curtext   NVARCHAR ( 4000 ) 

[轉貼]How do I find a stored procedure containing ?
[轉貼]How do I find a stored procedure containing ?

--  set up a cursor, we need to be sure this is in the correct order 

[轉貼]How do I find a stored procedure containing ?

--  from syscomments (which orders the 8KB chunks by colid) 

[轉貼]How do I find a stored procedure containing ?
[轉貼]How do I find a stored procedure containing ?

DECLARE  c  CURSOR  

[轉貼]How do I find a stored procedure containing ?

    LOCAL FORWARD_ONLY STATIC READ_ONLY  FOR  

[轉貼]How do I find a stored procedure containing ?

     SELECT   OBJECT_NAME (id),  text  

[轉貼]How do I find a stored procedure containing ?

         FROM  syscomments s 

[轉貼]How do I find a stored procedure containing ?

         INNER   JOIN  # temp  t 

[轉貼]How do I find a stored procedure containing ?

         ON  s.id  =  t.Proc_id 

[轉貼]How do I find a stored procedure containing ?

         ORDER   BY  id, colid 

[轉貼]How do I find a stored procedure containing ?

OPEN  c 

[轉貼]How do I find a stored procedure containing ?

FETCH   NEXT   FROM  c  INTO   @curName ,  @curtext  

[轉貼]How do I find a stored procedure containing ?
[轉貼]How do I find a stored procedure containing ?

--  start the loop 

[轉貼]How do I find a stored procedure containing ?

WHILE  ( @@FETCH_STATUS   =   0 ) 

[轉貼]How do I find a stored procedure containing ?

BEGIN  

[轉貼]How do I find a stored procedure containing ?
[轉貼]How do I find a stored procedure containing ?

     --  get the pointer for the current procedure name / colid 

[轉貼]How do I find a stored procedure containing ?

     SELECT   @txtPval   =   TEXTPTR (Definition) 

[轉貼]How do I find a stored procedure containing ?

         FROM  # temp  

[轉貼]How do I find a stored procedure containing ?

         WHERE  Proc_Name  =   @curName  

[轉貼]How do I find a stored procedure containing ?
[轉貼]How do I find a stored procedure containing ?

     --  find out where to append the #temp table's value 

[轉貼]How do I find a stored procedure containing ?

     SELECT   @txtPidx   =   DATALENGTH (Definition) / 2  

[轉貼]How do I find a stored procedure containing ?

         FROM  # temp  

[轉貼]How do I find a stored procedure containing ?

         WHERE  Proc_Name  =   @curName  

[轉貼]How do I find a stored procedure containing ?
[轉貼]How do I find a stored procedure containing ?

     --  apply the append of the current 8KB chunk 

[轉貼]How do I find a stored procedure containing ?

     UPDATETEXT  # temp .definition  @txtPval   @txtPidx   0   @curtext  

[轉貼]How do I find a stored procedure containing ?
[轉貼]How do I find a stored procedure containing ?

     FETCH   NEXT   FROM  c  INTO   @curName ,  @curtext  

[轉貼]How do I find a stored procedure containing ?

END  

[轉貼]How do I find a stored procedure containing ?
[轉貼]How do I find a stored procedure containing ?

--  check what was produced 

[轉貼]How do I find a stored procedure containing ?

SELECT  Proc_Name, Definition,  DATALENGTH (Definition) / 2  

[轉貼]How do I find a stored procedure containing ?

     FROM  # temp  

[轉貼]How do I find a stored procedure containing ?
[轉貼]How do I find a stored procedure containing ?

--  check our filter 

[轉貼]How do I find a stored procedure containing ?

SELECT  Proc_Name, Definition 

[轉貼]How do I find a stored procedure containing ?

     FROM  # temp  

[轉貼]How do I find a stored procedure containing ?

     WHERE  definition  LIKE   ' %foobar% '  

[轉貼]How do I find a stored procedure containing ?
[轉貼]How do I find a stored procedure containing ?

--  clean up 

[轉貼]How do I find a stored procedure containing ?

DROP   TABLE  # temp  

[轉貼]How do I find a stored procedure containing ?

CLOSE  c 

[轉貼]How do I find a stored procedure containing ?

DEALLOCATE  c  

Adam Machanic had a slightly different approach to the issue of multiple rows in syscomments, though it doesn't solve the problem where a line exceeds 4000 characters *and* your search phrase teeters on the end of such a line. Anyway, it's an interesting function, check it out! 

SQL Server 2005 

Luckily, SQL Server 2005 will get us out of this problem. There are new functions like OBJECT_DEFINITION, which returns the whole text of the procedure. Also, there is a new catalog view, sys.sql_modules, which also holds the entire text, and INFORMATION_SCHEMA.ROUTINES has been updated so that the ROUTINE_DEFINITION column also contains the full text of the procedure. So, any of the following queries will work to perform this search in SQL Server 2005: 

[轉貼]How do I find a stored procedure containing ?

SELECT  Name 

[轉貼]How do I find a stored procedure containing ?

     FROM  sys.procedures 

[轉貼]How do I find a stored procedure containing ?

     WHERE  OBJECT_DEFINITION( object_id )  LIKE   ' %foobar% '  

[轉貼]How do I find a stored procedure containing ?
[轉貼]How do I find a stored procedure containing ?

SELECT   OBJECT_NAME ( object_id ) 

[轉貼]How do I find a stored procedure containing ?

     FROM  sys.sql_modules 

[轉貼]How do I find a stored procedure containing ?

     WHERE  Definition  LIKE   ' %foobar% '  

[轉貼]How do I find a stored procedure containing ?

     AND   OBJECTPROPERTY ( object_id ,  ' IsProcedure ' )  =   1  

[轉貼]How do I find a stored procedure containing ?
[轉貼]How do I find a stored procedure containing ?

SELECT  ROUTINE_NAME 

[轉貼]How do I find a stored procedure containing ?

     FROM  INFORMATION_SCHEMA.ROUTINES 

[轉貼]How do I find a stored procedure containing ?

     WHERE  ROUTINE_DEFINITION  LIKE   ' %foobar% '  

[轉貼]How do I find a stored procedure containing ?

     AND  ROUTINE_TYPE  =   ' PROCEDURE '  

Note that there is no good substitute for documentation around your application. The searching above can provide many irrelevant results if you search for a word that happens to only be included in comments in some procedures, that is part of a larger word that you use, or that should be ignored due to frequency (e.g. SELECT). It can also leave things out if, for example, you are searching for the table name 'Foo_Has_A_Really_Long_Name' and some wise developer has done this: 

[轉貼]How do I find a stored procedure containing ?

EXEC ( ' SELECT * FROM Foo_Has '   + ' _A_Really_Long_Name ' )  

Likewise, sp_depends will leave out any procedure like the above, in addition to any procedure that was created before the dependent objects exist. The latter scenario is allowed due to deferred name resolution—the parser allows you to create a procedure that references invalid objects, and doesn't check that they exist until you actually run the stored procedure.  

So, long story short, you can't be 100% certain that any kind of searching or in-built query is going to be the silver bullet that tracks down every reference to an object. But you can get pretty close. 

Third-party products 

Whenever there is a void, someone is going to come up with a solution, right? I was alerted recently of this tool, which indexes all of your metadata to help you search for words... 

    Gplex Database 

That's the only one I know of that actually indexes the content and metadata of database objects (there are tons of tools that can perform brute force search of files if you have scripted out your objects, but this will roughly equate to the above). If you know of other products, please let us know.

繼續閱讀