Search for keywords in all stored procedures

Source: Internet
Author: User

You can execute the following content in the master database in all databases and search the Stored Procedure content by keywords,

The SP _ before the name sp_findproc seems to be required. Otherwise, if it is executed in other databases, the system will prompt that it cannot be found.

Create Procedure   [ DBO ] . [ Sp_findproc ] ( @ Akey   Varchar ( 255 )) As

-- Search for keywords in all stored procedures. keywords are case insensitive.
Declare   @ Aname   As   Varchar ( 700 )
Declare   @ Atext   As   Varchar ( 8000 )
Declare   @ Acolid   As   Int
Declare   @ POS   As   Int  
Declare   @ Start   As   Int
Declare   @ End   As   Int
Declare   @ Plain text   As   Varchar ( 255 )
Create   Table # Temp_proctext (OID Int   Identity ( 1 , 1 ), Name Varchar ( 700 ), Text   Varchar ( 255 ), POS Int )
Declare Proctext_cursor Cursor Local For
Select A. Name, B. Text , B. colid From Sysobjects A, syscomments B Where A. ID = B. ID And A. Type = ' P '  
And B. Text   Like   ' % ' + @ Akey + ' % '   Order   By A. Name, B. colid
Open Proctext_cursor
Fetch   Next   From Proctext_cursor Into   @ Aname , @ Atext , @ Acolid
While ( @ Fetch_status   =   0 )
Begin
  Set   @ POS   =   Charindex ( @ Akey , @ Atext , 1 )
  While ( @ POS   > 0 )
  Begin  
Set   @ Start   =   @ POS   -   10
If   @ Start   <= 0  
Set   @ Start   = 1
Set   @ End   =   @ POS   +   Len ( @ Akey ) + 20
If   @ End   >   Len ( @ Atext )
Set   @ End   =   Len ( @ Atext )
Set   @ Plain text   =   Substring ( @ Atext , @ Start , @ End   -   @ Start )
Insert   Into # Temp_proctext Values ( @ Aname , @ Plain text ,( @ Acolid - 1 ) * 4000 + @ POS )
Set   @ POS   =   Charindex ( @ Akey , @ Atext , @ End )
  End
  Fetch   Next   From Proctext_cursor Into   @ Aname , @ Atext , @ Acolid
End
Close Proctext_cursor
Deallocate Proctext_cursor
Select Name, POs, Text   From # Temp_proctext Order   By Oid
Drop   Table # Temp_proctext


Go

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.