Important alert: (current site time 7/15/2013 8:05:31 PM EDT)
 

VB icon

Find a string in Procedures, Triggers, Constraints, Defaults, Functions, and Views

Email
Submitted on: 2/20/2002 8:05:10 AM
By: James Travis  
Level: Intermediate
User Rating: By 8 Users
Compatibility: SQL Server 7.0, Other
Views: 21682
author picture
(About the author)
 
     Allow users to search thru objects for a value they are looking. This may be items that touch a specific table/view or items that need to be replaced.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
--**************************************
-- Name: Find a string in Procedures, Triggers, Constraints, Defaults, Functions,and Views
-- Description:Allow users to search thru objects for a value they are looking. This may be items that touch a specific table/view or items that need to be replaced.
-- By: James Travis
--
-- Inputs:@find = the value(s) to search for
@type = the type of objects to search
--
-- Returns:The names of objects that containt th search criteria.
--
-- Assumes:First off this will not work for any items that have the WITH ENCRYPTION remark in them. With this is can pose a string such as 'INSERT' against all the 'P'rocedures to get a return of which Procedures have an INSERT statment in them or you can do word strings such as 'FROM TABLE1 WHERE COL1 =' or string list searches such as 'INSERT%TABLE1%COL1 ='. You can use any valid like strings you wish, but you don't need leading and ending wildcards as general most items start with a specific item such as CREATE which will be contained in the majority of code items. This is compatible with SQL 7/2000.
--
--This code is copyrighted and has-- limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=414&lngWId=5--for details.--**************************************

CREATE PROCEDURE sp_FindStringInCode
/* Input variables, default null for custom error output. */
@find VARCHAR(50) = NULL,
@type VARCHAR(2) = NULL
AS
/* Check for null or invalid input and show custom error. */
IF @find IS NULL AND @type IS NULL
BEGIN
	RAISERROR ('This procedure has two required parameters @find and @type',16,-1)
	RETURN
END
ELSE IF @find IS NULL
BEGIN
	RAISERROR ('You must enter a valid like criteria for @find without the leading/ending % wildcard.',16,-1)
	RETURN
END
ELSE IF @type IS NULL OR @type NOT IN ('C','D','FN','P','TR','V')
BEGIN
	RAISERROR('No value was entered for @type.
Valid values for @type are
	C = Check Constraint
	D = Default
	FN = Function
	P = Procedure
	TR = Trigger
	V = View',16,-1)
	RETURN
END
/* Set wildcards on end of find value. */
SET @find = '%' + @find + '%'
/* Output object names which contain find value. */
SELECT DISTINCT OBJECT_NAME([id]) FROM syscomments
WHERE [id] IN (SELECT [id] FROM sysobjects WHERE xtype = @type AND status >= 0) AND [text] LIKE @find


Other 20 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

2/21/2002 4:28:19 AMPeter Wilkinson

Very nice procedure James. I have been looking for something like this for a while. However, I found that some stored procedures appeared more than once in the final select, so have modified it to a 'DISTINCT' select.
Keep putting up your procedures, they are always useful.
(If this comment was disrespectful, please report it.)

 
2/21/2002 6:05:45 AMJames Travis

Thanks, I went ahead and made the change. Unfortunately in my environment this is not happening so I had no examples of this when testing.
(If this comment was disrespectful, please report it.)

 
2/21/2002 10:02:45 AMRasputin

Hey James,

Excellent! 5*'s from me.

...wish more PSC submissions were like yours - high quality!

Thanks for sharing your high caliber work!

- Ras
(If this comment was disrespectful, please report it.)

 
2/21/2004 8:29:01 AMJoseph Gama

James,
Your code is great, 5 *
However, the search will fail if the word is between two 8kb blocks. I changed it and post it here:
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=812&lngWId= 5

Peace,

Joseph
Gama
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.