{"id":80,"date":"2011-12-16T11:09:50","date_gmt":"2011-12-16T11:09:50","guid":{"rendered":"https:\/\/sqlserverlearner.azurewebsites.net\/2011\/12\/16\/search-for-a-string-in-definitions-of-all-objects-in-all-databases\/"},"modified":"2011-12-16T11:09:50","modified_gmt":"2011-12-16T11:09:50","slug":"search-for-a-string-in-definitions-of-all-objects-in-all-databases","status":"publish","type":"post","link":"https:\/\/sqlserverlearner.com\/2011\/12\/16\/search-for-a-string-in-definitions-of-all-objects-in-all-databases\/","title":{"rendered":"search for a string in definitions of all objects in all databases"},"content":{"rendered":"

Many times we face a typical problem of searching for a string the definitions of all objects in all the databases.<\/p>\n

Real time scenario:<\/strong>
\nYou wanted to search for all the objects in all databases that have used the word test in its definition.<\/p>\n

Solution:<\/strong><\/p>\n

TSQL Query:
\n[sql]
\nDECLARE @vQuery VARCHAR(500)<\/p>\n

SET @vQuery = ‘
\nUSE ?
\nSELECT name as ObjectName FROM [?].sys.objects O
\n WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE ”%test%”
\n ‘<\/p>\n

exec sp_MSforeachdb @vQuery
\n[\/sql]<\/p>\n

The above query returns the names of all objects that have a string\/text as ‘test’.<\/p>\n

<\/p>\n

If you wanted to display the type of the object also, then below is the query:<\/p>\n

[sql]
\nDECLARE @vQuery VARCHAR(500)<\/p>\n

SET @vQuery = ‘
\nUSE ?
\nSELECT name as ObjectName,O.type_desc as ObjectType FROM [?].sys.objects O
\n WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE ”%test%”’<\/p>\n

exec sp_MSforeachdb @vQuery
\n[\/sql]<\/p>\n

And now, if you wanted to display the database name also in the output.
\nBelow is the query for that:
\n[sql]
\nDECLARE @vQuery VARCHAR(500)<\/p>\n

SET @vQuery = ‘
\nUSE ?
\nSELECT ”?” as DatabaseName,name ObjectName,O.type_desc as ObjectType FROM [?].sys.objects O
\n WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE ”%a%”’<\/p>\n

exec sp_MSforeachdb @vQuery
\n[\/sql]<\/p>\n

Making the query mode advanced by adding raiserror to check the databases scanned.
\n[sql]
\nDECLARE @vQuery VARCHAR(500)<\/p>\n

SET @vQuery = ‘
\nUSE ?
\nraiserror(”Searching Database ?”,1,1)
\nSELECT ”?” as DatabaseName,name ObjectName,O.type_desc as ObjectType FROM [?].sys.objects O
\n WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE ”%a%”’<\/p>\n

exec sp_MSforeachdb @vQuery
\n[\/sql]<\/p>\n","protected":false},"excerpt":{"rendered":"

Many times we face a typical problem of searching for a string the definitions of all objects in all the databases. Real time scenario: You wanted to search for all the objects in all databases that have used the word test in its definition. Solution: TSQL Query: [sql] DECLARE @vQuery VARCHAR(500) SET @vQuery = ‘…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[411,412,413,414,415,416,417,418],"_links":{"self":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts\/80"}],"collection":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/comments?post=80"}],"version-history":[{"count":0,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts\/80\/revisions"}],"wp:attachment":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/media?parent=80"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/categories?post=80"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/tags?post=80"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}