{"id":49,"date":"2018-08-18T18:00:28","date_gmt":"2018-08-18T18:00:28","guid":{"rendered":"https:\/\/datablog.roman-halliday.com\/?p=49"},"modified":"2018-06-17T17:57:03","modified_gmt":"2018-06-17T17:57:03","slug":"find-text-in-sql-procedures-and-other-objects","status":"publish","type":"post","link":"https:\/\/datablog.roman-halliday.com\/index.php\/2018\/08\/18\/find-text-in-sql-procedures-and-other-objects\/","title":{"rendered":"Search for text in SQL Procedures &#038; Views etc.."},"content":{"rendered":"<p>A new project, incomplete documentation, and the original author gone. I need to know &#8220;what updates and uses this object?&#8221;. So, how do I search for it?.<\/p>\n<p>If there is good source control and code management, a simple search will work. In many cases, there isn&#8217;t the luxury of code management.<\/p>\n<h1>Search the code behind procedures, views and other objects<\/h1>\n<p>I have cooked up these short scripts to help with the search through through objects. The code which makes up views and stored procedures is accessible through some sys views. This allows us to find what uses a table or other object, or even look for a magic number.<\/p>\n<p>Both scripts dynamically create an SQL statement, which searches through the object definitions. Note, this type of dynamic SQL is not ideal for production code for security reasons. As this isn&#8217;t a production procedure, this is a good time to use dynamic SQL.<\/p>\n<h1>Single database (catalogue)<\/h1>\n<p>Most of the time, we are only interested in a single database\/catalogue in SQL server. The below allows you to change the value for @search_text to anything you&#8217;d like to search for.<\/p>\n<h2>V1<\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">-------------------------------------------------------------------------------\r\n-- Current DB Catalogue\r\n-------------------------------------------------------------------------------\r\nDECLARE @search_text  VARCHAR(200);\r\nSET @search_text = '%reporting_entites%';\r\nSELECT o.[object_id],\r\n       db_name()      AS [db_catalog_name],\r\n       o.[name]       AS [Object_Name],\r\n       m.[definition],\r\n       o.[type_desc],\r\n       s.[name]       AS [schema_name],\r\n       '[' + s.[name] + '].[' + o.[name] + ']' AS [formatted_Name]\r\n  FROM sys.objects o\r\n     LEFT JOIN sys.sql_modules m ON m.[object_id] = o.[object_id]\r\n     LEFT JOIN sys.schemas     s ON o.[schema_id] = s.[schema_id]\r\n WHERE(LOWER(m.definition) LIKE LOWER(@search_text)\r\n    OR LOWER(o.name)       LIKE LOWER(@search_text)\r\n      )\r\n;<\/pre>\n<h2>V2<\/h2>\n<p>Allowing for quotes in the string and dynamically adding the percentages<\/p>\n<div class=\"oembed-gist\"><script src=\"https:\/\/gist.github.com\/d-roman-halliday\/f41ffefdaac1e44c0f6052afdee873ae.js\"><\/script><noscript>View the code on <a href=\"https:\/\/gist.github.com\/d-roman-halliday\/f41ffefdaac1e44c0f6052afdee873ae\">Gist<\/a>.<\/noscript><\/div>\n<h1>All databases (catalogues)<\/h1>\n<p>This extends the single database code to iterate through multiple databases\/catalogues in SQL server. The below allows you to change the value for @search_text to anything you&#8217;d like to search for.<\/p>\n<p>For this I included an error list, this mostly covers cases where the user in question doesn&#8217;t have access to view the metadata tables in the database. Often this will be system databases, or on a large system databases which are relevant to other teams\/departments and probably won&#8217;t contain anything we as the user are interested in anyway. Note without the try\/catch the execution could fail and not cover all databases.<\/p>\n<div class=\"oembed-gist\"><script src=\"https:\/\/gist.github.com\/d-roman-halliday\/46b706c0cea8615d779553a851971825.js\"><\/script><noscript>View the code on <a href=\"https:\/\/gist.github.com\/d-roman-halliday\/46b706c0cea8615d779553a851971825\">Gist<\/a>.<\/noscript><\/div>\n<div class=\"oembed-gist\"><script src=\"https:\/\/gist.github.com\/d-roman-halliday\/24a6eb3dd1e5d5efc4f1635837acb2f2.js?file=hunt-for-sql-reference-single-database.sql\"><\/script><noscript>View the code on <a href=\"https:\/\/gist.github.com\/d-roman-halliday\/24a6eb3dd1e5d5efc4f1635837acb2f2\">Gist<\/a>.<\/noscript><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A new project, incomplete documentation, and the original author gone. I need to know &#8220;what updates and uses this object?&#8221;. So, how do I search for it?. If there is good source control and code management, a simple search will work. In many cases, there isn&#8217;t the luxury of code management. Search the code behind&hellip;<\/p>\n<p class=\"read-more\"><a class=\"readmore-btn\" href=\"https:\/\/datablog.roman-halliday.com\/index.php\/2018\/08\/18\/find-text-in-sql-procedures-and-other-objects\/\">Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[],"class_list":["post-49","post","type-post","status-publish","format-standard","hentry","category-databases","category-sql-server"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Search for text in SQL Procedures &amp; Views etc.. - Rows Across The Lake<\/title>\n<meta name=\"description\" content=\"A new project, incomplete documentation, and the original author gone. I need to know &quot;what updates and uses this object?&quot;. So, how do I search for it?\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/datablog.roman-halliday.com\/index.php\/2018\/08\/18\/find-text-in-sql-procedures-and-other-objects\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Search for text in SQL Procedures &amp; Views etc.. - Rows Across The Lake\" \/>\n<meta property=\"og:description\" content=\"A new project, incomplete documentation, and the original author gone. I need to know &quot;what updates and uses this object?&quot;. So, how do I search for it?\" \/>\n<meta property=\"og:url\" content=\"https:\/\/datablog.roman-halliday.com\/index.php\/2018\/08\/18\/find-text-in-sql-procedures-and-other-objects\/\" \/>\n<meta property=\"og:site_name\" content=\"Rows Across The Lake\" \/>\n<meta property=\"article:published_time\" content=\"2018-08-18T18:00:28+00:00\" \/>\n<meta name=\"author\" content=\"david\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@d_roman_h\" \/>\n<meta name=\"twitter:site\" content=\"@d_roman_h\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"david\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/index.php\\\/2018\\\/08\\\/18\\\/find-text-in-sql-procedures-and-other-objects\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/index.php\\\/2018\\\/08\\\/18\\\/find-text-in-sql-procedures-and-other-objects\\\/\"},\"author\":{\"name\":\"david\",\"@id\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/#\\\/schema\\\/person\\\/575f96d2590c3085923ff9e1b565748b\"},\"headline\":\"Search for text in SQL Procedures &#038; Views etc..\",\"datePublished\":\"2018-08-18T18:00:28+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/index.php\\\/2018\\\/08\\\/18\\\/find-text-in-sql-procedures-and-other-objects\\\/\"},\"wordCount\":362,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/#\\\/schema\\\/person\\\/575f96d2590c3085923ff9e1b565748b\"},\"articleSection\":[\"Databases\",\"SQL Server\"],\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/datablog.roman-halliday.com\\\/index.php\\\/2018\\\/08\\\/18\\\/find-text-in-sql-procedures-and-other-objects\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/index.php\\\/2018\\\/08\\\/18\\\/find-text-in-sql-procedures-and-other-objects\\\/\",\"url\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/index.php\\\/2018\\\/08\\\/18\\\/find-text-in-sql-procedures-and-other-objects\\\/\",\"name\":\"Search for text in SQL Procedures & Views etc.. - Rows Across The Lake\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/#website\"},\"datePublished\":\"2018-08-18T18:00:28+00:00\",\"description\":\"A new project, incomplete documentation, and the original author gone. I need to know \\\"what updates and uses this object?\\\". So, how do I search for it?\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/index.php\\\/2018\\\/08\\\/18\\\/find-text-in-sql-procedures-and-other-objects\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/datablog.roman-halliday.com\\\/index.php\\\/2018\\\/08\\\/18\\\/find-text-in-sql-procedures-and-other-objects\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/index.php\\\/2018\\\/08\\\/18\\\/find-text-in-sql-procedures-and-other-objects\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Search for text in SQL Procedures &#038; Views etc..\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/#website\",\"url\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/\",\"name\":\"Rows Across The Lake\",\"description\":\"Data &amp; Databases\",\"publisher\":{\"@id\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/#\\\/schema\\\/person\\\/575f96d2590c3085923ff9e1b565748b\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/#\\\/schema\\\/person\\\/575f96d2590c3085923ff9e1b565748b\",\"name\":\"david\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/acddbc676a1d5c73795edcf0627ee39e5aa947da9033b58373e03d93122cb3b7?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/acddbc676a1d5c73795edcf0627ee39e5aa947da9033b58373e03d93122cb3b7?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/acddbc676a1d5c73795edcf0627ee39e5aa947da9033b58373e03d93122cb3b7?s=96&d=mm&r=g\",\"caption\":\"david\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/acddbc676a1d5c73795edcf0627ee39e5aa947da9033b58373e03d93122cb3b7?s=96&d=mm&r=g\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Search for text in SQL Procedures & Views etc.. - Rows Across The Lake","description":"A new project, incomplete documentation, and the original author gone. I need to know \"what updates and uses this object?\". So, how do I search for it?","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/datablog.roman-halliday.com\/index.php\/2018\/08\/18\/find-text-in-sql-procedures-and-other-objects\/","og_locale":"en_GB","og_type":"article","og_title":"Search for text in SQL Procedures & Views etc.. - Rows Across The Lake","og_description":"A new project, incomplete documentation, and the original author gone. I need to know \"what updates and uses this object?\". So, how do I search for it?","og_url":"https:\/\/datablog.roman-halliday.com\/index.php\/2018\/08\/18\/find-text-in-sql-procedures-and-other-objects\/","og_site_name":"Rows Across The Lake","article_published_time":"2018-08-18T18:00:28+00:00","author":"david","twitter_card":"summary_large_image","twitter_creator":"@d_roman_h","twitter_site":"@d_roman_h","twitter_misc":{"Written by":"david","Estimated reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/datablog.roman-halliday.com\/index.php\/2018\/08\/18\/find-text-in-sql-procedures-and-other-objects\/#article","isPartOf":{"@id":"https:\/\/datablog.roman-halliday.com\/index.php\/2018\/08\/18\/find-text-in-sql-procedures-and-other-objects\/"},"author":{"name":"david","@id":"https:\/\/datablog.roman-halliday.com\/#\/schema\/person\/575f96d2590c3085923ff9e1b565748b"},"headline":"Search for text in SQL Procedures &#038; Views etc..","datePublished":"2018-08-18T18:00:28+00:00","mainEntityOfPage":{"@id":"https:\/\/datablog.roman-halliday.com\/index.php\/2018\/08\/18\/find-text-in-sql-procedures-and-other-objects\/"},"wordCount":362,"commentCount":0,"publisher":{"@id":"https:\/\/datablog.roman-halliday.com\/#\/schema\/person\/575f96d2590c3085923ff9e1b565748b"},"articleSection":["Databases","SQL Server"],"inLanguage":"en-GB","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/datablog.roman-halliday.com\/index.php\/2018\/08\/18\/find-text-in-sql-procedures-and-other-objects\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/datablog.roman-halliday.com\/index.php\/2018\/08\/18\/find-text-in-sql-procedures-and-other-objects\/","url":"https:\/\/datablog.roman-halliday.com\/index.php\/2018\/08\/18\/find-text-in-sql-procedures-and-other-objects\/","name":"Search for text in SQL Procedures & Views etc.. - Rows Across The Lake","isPartOf":{"@id":"https:\/\/datablog.roman-halliday.com\/#website"},"datePublished":"2018-08-18T18:00:28+00:00","description":"A new project, incomplete documentation, and the original author gone. I need to know \"what updates and uses this object?\". So, how do I search for it?","breadcrumb":{"@id":"https:\/\/datablog.roman-halliday.com\/index.php\/2018\/08\/18\/find-text-in-sql-procedures-and-other-objects\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/datablog.roman-halliday.com\/index.php\/2018\/08\/18\/find-text-in-sql-procedures-and-other-objects\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/datablog.roman-halliday.com\/index.php\/2018\/08\/18\/find-text-in-sql-procedures-and-other-objects\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/datablog.roman-halliday.com\/"},{"@type":"ListItem","position":2,"name":"Search for text in SQL Procedures &#038; Views etc.."}]},{"@type":"WebSite","@id":"https:\/\/datablog.roman-halliday.com\/#website","url":"https:\/\/datablog.roman-halliday.com\/","name":"Rows Across The Lake","description":"Data &amp; Databases","publisher":{"@id":"https:\/\/datablog.roman-halliday.com\/#\/schema\/person\/575f96d2590c3085923ff9e1b565748b"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/datablog.roman-halliday.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":["Person","Organization"],"@id":"https:\/\/datablog.roman-halliday.com\/#\/schema\/person\/575f96d2590c3085923ff9e1b565748b","name":"david","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/secure.gravatar.com\/avatar\/acddbc676a1d5c73795edcf0627ee39e5aa947da9033b58373e03d93122cb3b7?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/acddbc676a1d5c73795edcf0627ee39e5aa947da9033b58373e03d93122cb3b7?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/acddbc676a1d5c73795edcf0627ee39e5aa947da9033b58373e03d93122cb3b7?s=96&d=mm&r=g","caption":"david"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/acddbc676a1d5c73795edcf0627ee39e5aa947da9033b58373e03d93122cb3b7?s=96&d=mm&r=g"}}]}},"_links":{"self":[{"href":"https:\/\/datablog.roman-halliday.com\/index.php\/wp-json\/wp\/v2\/posts\/49","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/datablog.roman-halliday.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/datablog.roman-halliday.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/datablog.roman-halliday.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/datablog.roman-halliday.com\/index.php\/wp-json\/wp\/v2\/comments?post=49"}],"version-history":[{"count":7,"href":"https:\/\/datablog.roman-halliday.com\/index.php\/wp-json\/wp\/v2\/posts\/49\/revisions"}],"predecessor-version":[{"id":168,"href":"https:\/\/datablog.roman-halliday.com\/index.php\/wp-json\/wp\/v2\/posts\/49\/revisions\/168"}],"wp:attachment":[{"href":"https:\/\/datablog.roman-halliday.com\/index.php\/wp-json\/wp\/v2\/media?parent=49"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/datablog.roman-halliday.com\/index.php\/wp-json\/wp\/v2\/categories?post=49"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/datablog.roman-halliday.com\/index.php\/wp-json\/wp\/v2\/tags?post=49"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}