Search term after the WHERE

I got a request from someone to find a certain word, but it has to be after the last WHERE clause, not in the SELECT list.  This is not perfect – technically you could have a subselect with a where clause, and no actual WHERE clause in the main query.  But most of the time, there will be a final WHERE clause, and this will pick up the search term after that final clause.

;WITH ObjectDefinition AS(

SELECT views.name,
OBJECT_DEFINITION(OBJECT_ID) as Defn
FROM sys.views
WHERE OBJECT_DEFINITION(OBJECT_ID) like ‘%WHERE%SearchTerm%’
)
SELECT name
FROM ObjectDefinition
WHERE SUBSTRING(Defn, LEN(Defn) – PATINDEX(‘%EREHW%’, REVERSE(Defn)), 2000) like ‘%SearchTerm%’

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s