30.06.2013 Views

SQL Server Team-based Development - Red Gate Software

SQL Server Team-based Development - Red Gate Software

SQL Server Team-based Development - Red Gate Software

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

267<br />

Chapter 9: Searching DDL and Build Scripts<br />

Listing 9-1 shows a procedure that does what it can to search your database, using<br />

Information_Schema.<br />

IF EXISTS ( SELECT *<br />

FROM information_Schema.routines<br />

WHERE specific_name = 'FindStringInInformationSchema' )<br />

DROP PROCEDURE FindStringInInformationSchema<br />

go<br />

CREATE PROCEDURE FindStringInInformationSchema<br />

/**<br />

summary: ><br />

This finds the string that you specify within the name of many database objects<br />

including indexes and parameters of routines. It searches within the text<br />

(definition) for every routine. It displays the full path of the database object<br />

and the object type. This cannot find the text (Definition) or names of triggers,<br />

and knows nothing of extended properties.<br />

example:<br />

– code: FindStringInInformationSchema '' – -list every object, along with<br />

creation date etc<br />

– code: FindStringInInformationSchema 'getdate'--find where the string<br />

'getdate' appears!<br />

– code: FindStringInInformationSchema 'gender'--find where the string<br />

'gender' appears!<br />

returns: ><br />

result<br />

**/ @SearchString VARCHAR(2000)<br />

AS<br />

IF CHARINDEX('%', @SearchString) = 0<br />

SELECT @SearchString = '%' + @SearchString + '%'<br />

SELECT--report on the routines first, name and definition<br />

Specific_Catalog + '.' + Specific_Schema + '.' + Specific_Name<br />

AS Qualified_Name ,<br />

LOWER(Routine_Type) + ' '<br />

+ CASE WHEN specific_name LIKE @SearchString THEN 'name'<br />

ELSE 'definition'<br />

END AS Object_Type<br />

FROM information_Schema.routines<br />

WHERE specific_name LIKE @SearchString<br />

OR routine_Definition LIKE @SearchString

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!