{"id":795,"date":"2010-02-04T00:00:00","date_gmt":"2010-02-04T00:00:00","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/finding-stuff-in-sql-server-database-ddl\/"},"modified":"2021-09-29T16:22:01","modified_gmt":"2021-09-29T16:22:01","slug":"finding-stuff-in-sql-server-database-ddl","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/databases\/sql-server\/t-sql-programming-sql-server\/finding-stuff-in-sql-server-database-ddl\/","title":{"rendered":"Finding Stuff in SQL Server Database DDL"},"content":{"rendered":"<div id=\"pretty\">\n<p>Have you ever wanted a search button in SQL Server Management Studio (SSMS)?&#160; Of course, there is a &#8216;find&#8217; menu item (Cntl F)&#160; but it won&#8217;t work in the Object Explorer pane of SSMS to find things in your database schema.&#160; You also have&#160; an object Search feature in&#160; SSMS 2008 within the Object Explorer &#8216;Details&#8217; window, but that only searches the names of objects, not their definitions. It also doesn&#8217;t search all objects, not even columns.&#160;&#160;Obviously, you can find strings within the current query window, but this is only a small fraction of what you actually need when you&#8217;re developing a database. You want to search for all your database objects, their names, their definitions and their comments.<\/p>\n<p>How difficult can it be to&#160;search through the code (definition) &#160;as well as the name of a&#160;database object? After all, the object explorer must have a great deal of information about the objects in it. The answer, it seems, is &#8216;very difficult&#8217;.<\/p>\n<h1>Summary<\/h1>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/935-image002.jpg\" width=\"624\" height=\"341\" alt=\"935-image002.jpg\" \/><\/p>\n<h1>Why isn&#8217;t it there in SSMS?<\/h1>\n<p>I don&#8217;t know for sure, but I suspect that the reason there is no way of searching&#160; through the code as well as the name&#160;of structural metadata by&#160; code in SSMS is that it isn&#8217;t straightforward within the architecture that Microsoft has chosen to use, without breaking a few rules.<\/p>\n<p>One problem is that there could be quite a few database objects around, such as the CLR functions, check constraints, defaults, default constraints, foreign keys , scalar functions, CLR scalar functions, CLR table-valued functions, inline table-functions, internal tables, stored procedures, CLR stored-procedures, plan guides, primary keys, rules, replication filters, service queues, CLR DML triggers, table functions , indexes, default constraints, table types ,user tables , unique constraints, views, XML Schema collections and extended stored procedures. Quite a few of these objects will have code in them.<\/p>\n<p>The code ,or definition, of objects will be found in user-defined rules, defaults, unencrypted Transact-SQL stored procedures, user-defined Transact-SQL functions, triggers, computed columns, CHECK constraints, views, or system objects such as a system stored procedure. Even if you can search through all the code, you&#8217;d also probably need to look at the extended properties too. However, that&#8217;s just scraping the surface in terms of what is there or potentially there. .<\/p>\n<p>Another problem is that SSMS is wedded to SMO, which provides an object-oriented programmatic model in place of the real way that SQL Server stores information about its objects such as tables, procedures and columns, and the hierarchical structure of objects. It is clever, it is useful, but it is dead slow to navigate when you&#8217;re looking at a large database, and trying to extract the contents of&#160; routines.<\/p>\n<h1>Why do you need to search your structural metadata?<\/h1>\n<p>There are a number of reasons why you might want to peruse your DDL. If you are used to glancing at, or clicking away at, the Object Explorer or using SQL Prompt, then you are probably scanning parts of the metadata. Most searching goes on when you are refactoring or maintaining an existing database that was written by someone else. An especially irksome task is renaming a view, table or a column. A dependency tracker will find the dependent objects but will, even if working perfectly, miss anything that is in dynamic code, embedded in strings. You may think that code only lurks in stored procedures or functions. Oh no. What about constraints, computed columns, defaults, rules, triggers or views?&#160; What of code in a CLR that accesses a database table?&#160; Code appears in a lot of places. Even if you are familiar with the database, it is easy, for example, to forget about a trigger on a table, miss-spell a column-name or overlook an index.&#160; For high-speed programming, the &#8216;point, click-and-curse&#8217; technique isn&#8217;t really an option.<\/p>\n<h1>How do you do it?<\/h1>\n<p>We&#8217;ll go through the various alternative approaches to searching for stuff in your database definition.<\/p>\n<h2>Get the free tool<\/h2>\n<p>If you don&#8217;t want to get immersed in SQL code to do this, <a href=\"http:\/\/www.red-gate.com\/products\/SQL_Search\/index.htm?utm_source=simpletalk&amp;utm_medium=article&amp;utm_content=philsearch\">SQL Search <\/a>is free and you don&#8217;t even have to give anyone your email address to get it.&#160; <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/935-image004.jpg\" width=\"574\" height=\"637\" alt=\"935-image004.jpg\" \/><\/p>\n<p>This is perfect for the majority of requirements. At the moment, <a href=\"http:\/\/www.red-gate.com\/products\/SQL_Search\/index.htm?utm_source=simpletalk&amp;utm_medium=article&amp;utm_content=philsearch\">SQL Search<\/a> is slightly limited in that the current version will search only the main database objects, but promised soon is the ability to search such things as index names and the contents of extended properties. It doesn&#8217;t yet entirely replace the requirement for a TSQL-based solution. However, you can see that it does more than the &#8216;Object-Explorer-Details&#8217; Search of SSMS since it has found the search term in the text or definition of the stored procedure as well as finding it in the name.<\/p>\n<h2>Searching the entire build-script<\/h2>\n<p>Let&#8217;s start with the very simplest, but one of the most reliable methods of searching. You get out your build script. No build script? You generate it. If you like clicking at things, then use SSMS; otherwise use an automated procedure with PowerShell and SMO(remember that you have to regenerate the build script every time someone else makes an alteration). I still use <a href=\"http:\/\/www.simple-talk.com\/sql\/database-administration\/more-database-administration-and-development-automation-using-dmo\/\">a slightly retro stored procedure with DMO<\/a>; it works very well for me. Then, you read it into your favorite text editor and use the search facilities in it. This sounds clunky, but if your editor uses RegEx search, then a lot can be done, including the automatic generation of lists. Personally, I am pretty happy with this approach, but it isn&#8217;t always convenient.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/935-image006.jpg\" width=\"621\" height=\"429\" alt=\"935-image006.jpg\" \/><\/p>\n<p>The use of a query pane with the build script in it is almost as good, but you don&#8217;t have the regular expressions, or the performance of a programmers&#8217; text editor. It also doesn&#8217;t help with some of the more advanced operations that you might need. I like making lists, with the help of a RegEx string, of the lines, or context, where each match happened. I can then scan them quickly to find a particular occurrence. (see <a href=\"http:\/\/www.simple-talk.com\/sql\/t-sql-programming\/tsql-regular-expression-workbench\/\">TSQL Regular Expression Workbench<\/a> &#160;for an introduction to RegEx). With a <b>Grep<\/b> tool, and <b>SQLCMD,<\/b> you can get lists of &#160;lines containing your search string.<\/p>\n<h2>The toe-in-the-water. The &#8216;Help&#8217; system-procedures<\/h2>\n<p>There are a number of &#8216;traditional&#8217; approaches to looking at your structural metadata, but they aren&#8217;t going to help much. If you just want a list of the most important objects, then just use&#8230; <\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">Execute sp_help --list all objects in the sys.sysobjects table<\/pre>\n<p>&#8230;but you will miss out on columns and indexes, and quite a few of the less important objects as well. There are other similar stored procedures, some of which are listed below, which are fine for getting specific information, but not much use for answering questions like &#8216;Where, in the database, is a date conversion used with a German (104) date format?&#8217; The famous <strong>sp_helptext<\/strong> is fine for getting the text of a particular object but no more than that. There is also&#8230;<\/p>\n<ul>\n<li><b>sp_helpconstraint<\/b> &#8212; all constraint types, their user-defined or system-supplied name, the columns on which they have been defined, and the expression that defines the constraint  <\/li>\n<li><b>sp_helpdb<\/b> &#8212; Reports information about a specified database  <\/li>\n<li><b>sp_helpextendedproc<\/b> &#8211;currently defined extended stored procedures  <\/li>\n<li><b>sp_helpfile<\/b> &#8212; the physical names and attributes of files associated with the current database  <\/li>\n<li><b>sp_helpfilegroup<\/b> -lists file groups  <\/li>\n<li><b>sp_helpgroup<\/b> -lists roles  <\/li>\n<li><b>sp_helpindex<\/b> -lists indexes, and the columns on which the index is built  <\/li>\n<li><b>sp_helptext<\/b> -displays the definition of a routine  <\/li>\n<li><b>sp_helptrigger<\/b> list the triggers and their types <\/li>\n<\/ul>\n<h2>Going Standard: Using Information Schema<\/h2>\n<p>Edgar Codd&#8217;s fourth rule&#160;for a relational database management system is that there should be an Active online catalog based on the relational model that is accessible to authorized users by means of their regular query language:&#160; This means that&#160; users must be able to access the data about the database&#8217;s structural metadata (catalog) just as easily as they can access data, and&#160;using the same query language that they use to access the database&#8217;s data.<\/p>\n<p>The Information_Schema views are a standard way of doing this. You can use them&#160; for searching if you don&#8217;t want too much. The queries you use for ad-hoc work, such as &#8216;which index covers this column?&#8217;, are best put into templates and dragged\/dropped onto your workspace, or held as some form of snippet. Typing them out laboriously was never an option. &#160;You can, of course, use a stored procedure if you want to do a general search, or run a utility that queries via ODBC.<\/p>\n<p>However, before we get too excited about the information Schema views, note that they have one or two big drawbacks. The first is that they only store the first 4000 characters of the definition of an object. If you write long stored procedures, you can move on to a later part of this article, or stick cosily to the idea of using a traditional programmers&#8217; text editor as I&#8217;ve already described. The second drawback is that if you are interested in anything out of the ordinary, such as triggers or extended properties, I&#8217;m afraid you&#8217;ll be disappointed. Information Schema are provided only to be compatible with the standard, it seems. <\/p>\n<p>Here is a procedure that does what it can to search your database, using Information schema.<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">IF EXISTS ( SELECT&#160; *\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; FROM&#160;&#160;&#160; information_Schema.routines\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; WHERE&#160;&#160; specific_name = 'FindStringInInformationSchema' ) \n&#160; DROP PROCEDURE FindStringInInformationSchema\ngo\nCREATE PROCEDURE FindStringInInformationSchema\n\/**\n&#160;summary:&#160;&#160; &gt;\nThis finds the string that you specify within the name of many database objects including indexes&#160; and parameters of routines. It searches within the text (definition) for every routine. it displays the full path of the database object and the object type\n&#160;\nThis cannot find the text (Definition) or names of triggers, and knows nothing of extended properties\nexample:\n&#160;&#160;&#160;&#160; - code:&#160;&#160;&#160; FindStringInInformationSchema '' --list every object, along with creation date etc\n&#160;&#160;&#160;&#160; - code:&#160;&#160;&#160; FindStringInInformationSchema 'getdate'--find where the string 'getdate' appears!\n&#160;&#160;&#160;&#160; - code:&#160;&#160;&#160; FindStringInInformationSchema 'gender'--find where the string 'gender' appears!\nreturns:&#160;&#160; &gt;\n&#160;\nresult \n**\/\n&#160; @SearchString VARCHAR(2000)\nAS \n&#160; IF CHARINDEX('%', @SearchString) = 0 \n&#160;&#160;&#160; SELECT&#160; @SearchString = '%' + @SearchString + '%'\n&#160; SELECT--report on the routines first, name and definition\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Specific_Catalog + '.' + Specific_Schema + '.' + Specific_Name\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; AS Qualified_Name,\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; LOWER(Routine_Type) + ' ' + CASE WHEN specific_name LIKE @SearchString\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; THEN 'name'\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ELSE 'definition'\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; END AS Object_Type\n&#160; FROM&#160;&#160;&#160; information_Schema.routines\n&#160; WHERE&#160;&#160; specific_name LIKE @SearchString OR routine_Definition LIKE @SearchString\n&#160; UNION ALL\n&#160; SELECT--and search view definitions\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Table_Catalog + '.' + Table_Schema + '.' + Table_Name,\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 'view definition'\n&#160; FROM&#160;&#160;&#160; information_Schema.views\n&#160; WHERE&#160;&#160; View_Definition LIKE @SearchString\n&#160; UNION ALL\n&#160; SELECT&#160; Table_Catalog + '.' + Table_Schema + '.' + Table_Name,\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; LOWER(table_type) + ' Name'\n&#160; FROM&#160;&#160;&#160; information_Schema.tables\n&#160; WHERE&#160;&#160; Table_name LIKE @SearchString\n&#160; UNION ALL\n&#160; SELECT&#160; Table_Catalog + '.' + Table_Schema + '.' + Table_Name + '.' + constraint_name,\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; LOWER(Constraint_type) + ' constraint Name'\n&#160; FROM&#160;&#160;&#160; information_Schema.table_constraints\n&#160; WHERE&#160;&#160; constraint_name LIKE @SearchString\n&#160; UNION ALL\n&#160; SELECT&#160; catalog_name + '.' + Schema_name, 'Schema'\n&#160; FROM&#160;&#160;&#160; information_Schema.schemata\n&#160; WHERE&#160;&#160; schema_name LIKE @SearchString AND schema_name NOT LIKE 'db_%' AND schema_name NOT LIKE 'information_Schema%'\n&#160; UNION ALL\n&#160; SELECT&#160; Table_Catalog + '.' + Table_Schema + '.' + Table_Name + '.' + column_name,\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 'TVF Column name'\n&#160; FROM&#160;&#160;&#160; information_Schema.ROUTINE_COLUMNS\n&#160; WHERE&#160;&#160; column_name LIKE @SearchString\n&#160; UNION ALL\n&#160; SELECT DISTINCT\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Constraint_Catalog + '.' + constraint_Schema + '.' + constraint_Name,\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 'Foregn Key constraint'\n&#160; FROM&#160;&#160;&#160; information_Schema.Referential_constraints\n&#160; WHERE&#160;&#160; constraint_name LIKE @SearchString\n&#160; UNION ALL\n&#160; SELECT DISTINCT\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Unique_Constraint_Catalog + '.' + Unique_constraint_Schema + '.' + Unique_constraint_Name,\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 'Unique constraint'\n&#160; FROM&#160;&#160;&#160; information_Schema.Referential_constraints\n&#160; WHERE&#160;&#160; Unique_constraint_name LIKE @SearchString\n&#160; UNION ALL\n&#160; SELECT&#160; Specific_Catalog + '.' + Specific_Schema + '.' + Specific_Name + '(' + Parameter_name + ')',\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 'routine parameter'\n&#160; FROM&#160;&#160;&#160; information_schema.parameters\n&#160; WHERE&#160;&#160; parameter_name &lt;&gt; '' AND parameter_name LIKE @SearchString\n&#160; UNION ALL\n&#160; SELECT&#160; Table_Catalog + '.' + Table_Schema + '.' + Table_Name + '.' + Column_Name,\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 'column Name'\n&#160; FROM&#160;&#160;&#160; INFORMATION_SCHEMA.COLUMNS\n&#160; WHERE&#160;&#160; column_name LIKE @SearchString\n&#160; UNION ALL\n&#160; SELECT&#160; Table_Catalog + '.' + Table_Schema + '.' + Table_Name + '.' + Column_Name + '.default',\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 'default constraint text'\n&#160; FROM&#160;&#160;&#160; INFORMATION_SCHEMA.COLUMNS\n&#160; WHERE&#160;&#160; column_default LIKE @SearchString\n<\/pre>\n<p>&#8230;Which will give you something like this&#8230;.<\/p>\n<p class=\"illustration\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/935-resultinfoschema.jpg\" width=\"639\" height=\"576\" alt=\"935-resultinfoschema.jpg\" \/><\/p>\n<h2>Learning patience: Using SMO<\/h2>\n<p>The most potentially powerful means of searching your database objects is by use of Server Management Objects (SMO). You can get a huge range of database and server objects with SMO, as long as you don&#8217;t mind waiting, and if you like coding in a .NET language. The problems come when you try to tackle a large database. Because you have to walk the hierarchy of SMOs objects, rather than do a set-oriented operation, you are soon in trouble if you have a lot of tables and columns. SMO provides a logical and consistent interface into a database, but it does not represent the reality of the way that this information is actually stored in the database.<\/p>\n<p>SMO comes into its own as a way of quickly finding out how to access a &#8216;difficult&#8217; database object. If you use SMO, and run Profiler at the same time to see what SQL is being executed, you can quickly achieve SQL&#160; MegaStar status in your development team. <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/935-image009.jpg\" width=\"624\" height=\"446\" alt=\"935-image009.jpg\" \/><\/p>\n<h2>Using the Object Catalog views<\/h2>\n<p>I love the Object Catalog views. Even Edgar Codd might have smiled. &#160;I wouldn&#8217;t suggest using them &#8216;raw&#8217;, as they are not as&#160; &#8216;unwrapped&#8217; or denormalised as the&#160; Information Schema views.&#160; Really, the only way to survive is to have all the information tucked into a stored procedure that you&#8217;d call up to find the things you wanted. Here is the routine I use to search through as much as possible of the structure of databases. I&#8217;ve written it like a Lego tower, with a series of UNION ALLs so as to allow you to break it up and create your own Lego tower.<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">IF EXISTS ( SELECT&#160; *\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; FROM&#160;&#160;&#160; information_Schema.routines\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; WHERE&#160;&#160; specific_name = 'FindString' ) \n&#160; DROP PROCEDURE FindString\ngo\nCREATE PROCEDURE FindString\n\/**\n&#160;summary:&#160;&#160; &gt;\nThis finds the string that you specify withing the name of every database object including indices, agent jobs, extended properties and parameters of routines. It&#160; searches within the text (definition) for every routine, and within the value of every extended property. it displays the full path of the database object, the object type the dates of creation and modification (sometimes that of the parent if this information isn't available) as well as the name of the parent of the object.\n&#160;\nexample:\n&#160;&#160;&#160;&#160; - code: findstring '' --list every object, along with creation date etc\nexample:\n&#160;&#160;&#160;&#160; - code: findString 'GetDate'--find where the string 'getdate' appears!\nexample:\n&#160;&#160;&#160;&#160; - code: findstring 'b[aeiou]t'--find but, bot,bit,bet and bat!\nreturns:&#160;&#160; &gt;\n&#160;&#160; Qualified_Name&#160; Varchar \nThis is the fully-qualified name of the object so that you know where it lurks. Sometimes, the path is a bit artificial, but it should be obvious how the convention works.\n&#160;&#160; Object_Type&#160; Varchar\nA description of the type of object the string was found in. 'Definition' refers to the code within the routine.\n&#160;&#160; created DateTime\nThe date that either the object or its' parent was created.\n&#160;&#160; Last_Modified DateTime\nThe date that either the object or its' parent was last modified\n&#160;&#160; Parent Varchar\nThe name of the parent of the object if known\n**\/\n&#160; @SearchString VARCHAR(2000)\nAS \n-------------------------------------------------------------------------------------\n&#160; IF CHARINDEX('%', @SearchString) = 0 --if he hasn't done in the SQL wildcard format.\n&#160;&#160;&#160; SELECT&#160; @SearchString = '%' + @SearchString + '%' --add it\n&#160;\n&#160; SELECT --firstly, we'll search the names of the basic objects\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; DB_NAME() + '.' + Object_Schema_name(s.[object_ID]) + '.' \n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; + COALESCE(p.name + '.', '') + s.name\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; AS [Qualified_Name],\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; replace(SUBSTRING(v.name, 5, 31),'cns','constraint') \n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; + ' name' AS Object_Type,\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; s.create_date AS 'Created',\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; s.modify_date AS 'Last_Modified',\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; COALESCE(p.name, '-') AS 'parent'\n&#160; FROM&#160;&#160;&#160; sys.objects S --to get the objects\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; LEFT OUTER JOIN master.dbo.spt_values v--to get the type of object\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ON s.type = SUBSTRING(v.name, 1, 2) COLLATE database_default \n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; AND v.type = 'O9T' LEFT OUTER JOIN sys.objects p\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; --to get any parent object\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ON s.parent_Object_ID = p.[object_ID]\n&#160; WHERE&#160;&#160; s.name LIKE @SearchString--string you want to search for\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; AND Object_Schema_name(s.object_ID) NOT LIKE 'sys%'\n&#160; UNION ALL\n&#160; SELECT--and search all the names of the&#160; columns too\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; DB_NAME() + '.' + Object_Schema_name(s.object_ID) + '.' + '.' \n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; + s.name + '.' + c.name\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; AS [name], 'Column name' AS [object_type],\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; s.create_date AS 'created',\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; s.modify_date AS 'Last Modified',\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; COALESCE(s.name, '-') AS 'parent'\n&#160; FROM&#160;&#160;&#160; sys.columns c INNER JOIN sys.objects S --get table data\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ON c.object_ID = s.object_ID\n&#160; WHERE&#160;&#160; c.name LIKE @SearchString--string you want to search for\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; AND Object_Schema_name(s.object_ID) NOT LIKE 'sys%'\n&#160; UNION ALL\n&#160; SELECT--and search all the definitions of the computed columns too\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; DB_NAME() + '.' + Object_Schema_name(s.object_ID) \n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; + '.' + s.name + ',' + c.name\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; AS [name],\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 'computed Column definition' AS [object_type],\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; s.create_date AS 'created',\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; s.modify_date AS 'Last Modified',\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; COALESCE(s.name, '-') AS 'parent'\n&#160; FROM&#160;&#160;&#160; sys.computed_columns c INNER JOIN sys.objects S\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ON c.object_ID = s.object_ID\n&#160; WHERE&#160;&#160; c.definition LIKE @SearchString--string you want to search for\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; AND Object_Schema_name(s.object_ID) NOT LIKE 'sys$'\n&#160; UNION ALL --now search the XML schema collection names\n&#160; SELECT&#160; DB_NAME() + '.' + name,\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 'XML Schema Collection name',\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; create_date AS 'created',\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; modify_date AS 'Last Modified', '-' AS 'parent'\n&#160; FROM&#160;&#160;&#160; sys.xml_schema_collections\n&#160; WHERE&#160;&#160; name LIKE @SearchString\n&#160; UNION ALL --and now search the names of the DDL triggers (they arent in sys.objects)\n&#160; SELECT&#160; DB_NAME() + '.' + name,\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; LOWER(type_desc)&#160; COLLATE database_default,\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; create_date AS 'created',\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; modify_date AS 'Last Modified', '-' AS 'parent'\n&#160; FROM&#160;&#160;&#160; sys.triggers\n&#160; WHERE&#160;&#160; name LIKE @SearchString--string you want to search for\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; AND parent_class = 0--only DDL triggers\n&#160; UNION ALL --and search the names of all the indexes\n&#160; SELECT&#160; DB_NAME() + '.' + Object_Schema_name(p.object_ID) + '.' + p.name + '.' + i.name,\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; LOWER(i.type_desc) + ' index name'&#160; COLLATE database_default,\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; create_date AS 'created',\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; modify_date AS 'Last Modified', p.name AS 'parent'\n&#160; FROM&#160;&#160;&#160; sys.indexes i INNER JOIN sys.objects p\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ON i.object_ID = p.object_ID\n&#160; WHERE&#160;&#160; i.name LIKE @SearchString--string you want to search for\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; AND Object_Schema_name(i.object_ID) NOT LIKE 'sys%' \n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; AND is_primary_key = 0 AND i.type_desc &lt;&gt; 'HEAP'\n&#160; UNION ALL--and we want to know the parameters to the routines\n&#160; SELECT&#160; DB_NAME() + '.' + Object_Schema_name(s.[object_ID]) + +s.name + '(' + pa.name + ')'\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; AS [name],\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; SUBSTRING(v.name, 5, 31) + ' parameter name' AS [object_type],\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; s.create_date AS 'created',\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; s.modify_date AS 'Last Modified',\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; COALESCE(s.name, '-') AS 'parent'\n&#160; FROM&#160;&#160;&#160; sys.parameters pa INNER JOIN sys.objects S\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; --to get the objects\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ON pa.object_ID = S.object_ID LEFT OUTER JOIN master.dbo.spt_values v\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ON s.type = SUBSTRING(v.name, 1, 2) COLLATE database_default AND v.type = 'O9T'\n&#160; WHERE&#160;&#160; pa.name &lt;&gt; '' AND pa.name LIKE @SearchString--string you want to search for\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; AND Object_Schema_name(s.object_ID) NOT LIKE 'sys%'\n&#160; UNION ALL--and the text (definition) of the default constraints\n&#160; SELECT&#160; DB_NAME() + '.' + Object_Schema_name(d.parent_object_id) \n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; + '.' + OBJECT_NAME(d.parent_object_id) + '.' +c.name+ '.' + d.name,\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 'default constraint definition', NULL AS 'created',\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; NULL AS 'Last Modified',\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; OBJECT_NAME(d.parent_object_id) AS 'parent'\nFROM&#160;&#160;&#160; sys.default_constraints d INNER JOIN sys.columns c\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ON d.parent_column_id = c.column_id and d.parent_object_ID=c.object_ID\n&#160; WHERE&#160;&#160; definition LIKE @SearchString\n&#160; UNION ALL --the text of other table objects\n&#160; SELECT&#160; DB_NAME() + '.' + Object_Schema_name(p.object_id) + '.' + OBJECT_NAME(p.object_id),\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; SUBSTRING(v.name, 5, 31) + ' definition' AS [object_type],\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; p.create_date AS 'created',\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; p.modify_date AS 'Last Modified', '-' AS 'parent'\n&#160; FROM&#160;&#160;&#160; sys.sql_modules m INNER JOIN sys.objects p\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ON m.object_ID = p.object_ID LEFT OUTER JOIN master.dbo.spt_values v\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ON p.type = SUBSTRING(v.name, 1, 2) COLLATE database_default AND v.type = 'O9T'\n&#160; WHERE&#160;&#160; definition LIKE @SearchString\n&#160; UNION ALL--and the text of the check constraints\n&#160; SELECT&#160; DB_NAME() + '.' + Object_Schema_name(d.parent_object_id) + '.' \n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; + OBJECT_NAME(d.parent_object_id) + '.' + c.name + '.' + d.name,\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 'check constraint definition', create_date AS 'created',\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; modify_date AS 'Last Modified',\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; OBJECT_NAME(d.parent_object_id) + '.' + c.name AS 'parent'\n&#160; FROM&#160;&#160;&#160; sys.check_constraints d INNER JOIN sys.columns c\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ON d.parent_column_id = c.column_id AND d.parent_object_ID = c.Object_ID\n&#160; WHERE&#160;&#160; definition LIKE @SearchString\n&#160; UNION ALL --what about the extended properties? Let's check them\n&#160; SELECT&#160; DB_NAME() + '.' + Object_Schema_name(s.[object_ID]) + '.' \n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; + COALESCE(p.name + '.', '') + s.name\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; AS [name],\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; SUBSTRING(v.name, 5, 31) + ' Extended property' AS [object_type],\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; s.create_date AS 'created',\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; s.modify_date AS 'Last Modified',\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; COALESCE(p.name, '-') AS 'parent'\n&#160; FROM&#160;&#160;&#160; sys.extended_properties ep INNER JOIN sys.objects s\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ON s.object_ID = ep.major_ID LEFT OUTER JOIN master.dbo.spt_values v\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ON s.type = SUBSTRING(v.name, 1, 2) COLLATE database_default \n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; AND v.type = 'O9T' LEFT OUTER JOIN sys.objects p\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; --to get any parent object\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ON s.parent_Object_ID = p.[object_ID]\n&#160; WHERE&#160;&#160; CONVERT(VARCHAR(MAX), ep.value) LIKE @SearchString--string you want to search for\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; AND Object_Schema_name(s.object_ID) NOT LIKE 'sys%' \n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; AND class = 1 AND minor_ID = 0--object \n&#160; UNION ALL --and extended property comments on columns\n&#160; SELECT&#160; DB_NAME() + '.' + Object_Schema_name(s.[object_ID]) + '.' + s.name + '.' + c.name\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; AS [name],\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 'Column Extended property' AS [object_type],\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; s.create_date AS 'created',\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; s.modify_date AS 'Last Modified',\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; COALESCE(c.name, '-') AS 'parent'\n&#160; FROM&#160;&#160;&#160; sys.extended_properties ep INNER JOIN sys.objects s\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ON s.object_ID = ep.major_ID LEFT OUTER JOIN sys.columns c\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ON c.column_ID = minor_ID AND c.object_ID = ep.major_ID\n&#160; WHERE&#160;&#160; CONVERT(VARCHAR(MAX), ep.value) LIKE @SearchString \n&#160;&#160;&#160;&#160;&#160;&#160; AND Object_Schema_name(s.object_ID) NOT LIKE 'sys%' \n&#160;&#160;&#160;&#160;&#160;&#160; AND class = 1 AND minor_ID &lt;&gt; 0--object \n&#160; UNION ALL\n&#160;--and we need to search the properties of parameters to routines\n&#160; SELECT&#160; DB_NAME() + '.' + Object_Schema_name(s.[object_ID]) + '.' \n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; + s.name + '(' + p.name + ')'\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; AS [name],\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; SUBSTRING(v.name, 5, 31) + ' parameter x Property' AS [object_type],\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; s.create_date AS 'created',\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; s.modify_date AS 'Last Modified',\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; COALESCE(s.name, '-') AS 'parent'\n&#160; FROM&#160;&#160;&#160; sys.extended_properties ep INNER JOIN sys.objects s\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ON s.object_ID = ep.major_ID LEFT OUTER JOIN master.dbo.spt_values v\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ON s.type = SUBSTRING(v.name, 1, 2) COLLATE database_default \n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; AND v.type = 'O9T' LEFT OUTER JOIN sys.parameters p\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ON p.parameter_ID = minor_ID AND p.object_ID = ep.major_ID\n&#160; WHERE&#160;&#160; CONVERT(VARCHAR(MAX), ep.value) LIKE @SearchString \n&#160;&#160;&#160;&#160; AND Object_Schema_name(s.object_ID) NOT LIKE 'sys%' \n&#160;&#160;&#160;&#160; AND class = 2--it is a parameter\n&#160; UNION ALL\n&#160;--and we need to search the properties of indexes\n&#160; SELECT&#160; DB_NAME() + '.' + Object_Schema_name(s.[object_ID]) + '.' \n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; + s.name + '.' + i.name\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; AS [name],\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; SUBSTRING(v.name, 5, 31) + ' index' AS [object_type],\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; s.create_date AS 'created',\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; s.modify_date AS 'Last Modified',\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; COALESCE(s.name, '-') AS 'parent'\n&#160; FROM&#160;&#160;&#160; sys.extended_properties ep INNER JOIN sys.objects s\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ON s.object_ID = ep.major_ID LEFT OUTER JOIN master.dbo.spt_values v\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ON s.type = SUBSTRING(v.name, 1, 2) COLLATE database_default \n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; AND v.type = 'O9T' LEFT OUTER JOIN sys.indexes i\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ON i.index_ID = minor_ID AND i.object_ID = ep.major_ID\n&#160; WHERE&#160;&#160; CONVERT(VARCHAR(MAX), ep.value) LIKE @SearchString \n&#160;&#160;&#160;&#160;&#160;&#160;&#160; AND Object_Schema_name(s.object_ID) NOT LIKE 'sys%' \n&#160;&#160;&#160;&#160;&#160;&#160;&#160; AND class = 7--it is an index\n&#160; UNION ALL -- we get XML Schema Collection extended properties\n&#160; SELECT&#160; DB_NAME() + '.' + sc.name + '.' + xsc.name + '.' + ep.name\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; AS [name], 'XML Schema Collection X property',\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; xsc.create_date AS 'created',\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; xsc.modify_date AS 'Last Modified',\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; xsc.name AS 'parent'\n&#160; FROM&#160;&#160;&#160; sys.extended_properties ep INNER JOIN sys.xml_schema_collections xsc\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ON xsc.xml_collection_id = ep.major_id INNER JOIN sys.schemas sc\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ON sc.schema_ID = xsc.schema_ID\n&#160; WHERE&#160;&#160; CONVERT(VARCHAR(MAX), ep.value) LIKE @SearchString \n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; AND class = 10--all the other properties\n&#160; UNION ALL --now scoop up all other extended properties (loadsa comments)\n&#160; SELECT&#160; DB_NAME() + '.' + sc.name + '.' + ep.name AS [name],\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; LOWER(class_desc) + ' X Property',\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; NULL AS 'created', NULL AS 'Last Modified',\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; '-' AS 'parent'\n&#160; FROM&#160;&#160;&#160; sys.extended_properties ep INNER JOIN sys.schemas sc\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ON sc.schema_ID = ep.major_ID\n&#160; WHERE&#160;&#160; CONVERT(VARCHAR(MAX), ep.value) LIKE @SearchString \n&#160;&#160;&#160;&#160;&#160;&#160;&#160; AND class NOT IN (1, 2, 7, 10)--all the other properties\n&#160; UNION ALL --names of CLR assemblies\n&#160; SELECT&#160; DB_NAME() + '.' + name, 'CLR Assembly',\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; create_date AS 'created',\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; modify_date AS 'Last Modified', '-' AS 'parent'\n&#160; FROM&#160;&#160;&#160; sys.assemblies\n&#160; WHERE&#160;&#160; clr_name LIKE @SearchString OR name LIKE @SearchString \nunion all --almost done. We do the agent jobs too here\nSELECT&#160; 'Agent' + '.' + DB_NAME() + '.' + [name] + '.' \n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; + step_name&#160; COLLATE database_default ,\n&#160;&#160;&#160;&#160;&#160;&#160;&#160; 'Agent ' + CASE TypeOfHit\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; WHEN 1 THEN 'job description'\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; WHEN 2 THEN 'job name'\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; WHEN 3 THEN 'step name'\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; WHEN 4 THEN 'job contents'\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ELSE 'impossible'\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; END, \n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; date_created, date_modified,\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [name]&#160; COLLATE database_default \nFROM \n&#160; (SELECT \n&#160;&#160;&#160; Job.name, step_name, date_created,\n&#160;&#160;&#160; Date_modified,\n&#160;&#160;&#160;&#160;&#160; CASE WHEN Job.Description LIKE&#160; @SearchString COLLATE database_default \n&#160;&#160;&#160;&#160;&#160;&#160;&#160; THEN 1 --job description\n&#160;&#160;&#160;&#160;&#160;&#160;&#160; WHEN Job.name LIKE&#160; @SearchString COLLATE database_default THEN 2--jpb name\n&#160;&#160;&#160;&#160;&#160;&#160;&#160; WHEN step_name LIKE&#160; @SearchString COLLATE database_default THEN 3--step name \n&#160;&#160;&#160;&#160;&#160;&#160;&#160; WHEN Step.command LIKE&#160; @SearchString COLLATE database_default&#160; THEN 4 --job contents\n&#160;&#160;&#160;&#160;&#160;&#160;&#160; ELSE 0&#160; END AS TypeOfHit\n&#160;&#160;&#160; FROM&#160;&#160; MSDB.dbo.sysJobs Job INNER JOIN MSDB.dbo.sysJobSteps Step\n&#160;&#160;&#160;&#160;&#160;&#160; ON Job.Job_Id = Step.Job_Id\n&#160;&#160;&#160; WHERE&#160; Database_name LIKE DB_NAME() COLLATE database_default \n&#160;&#160; ) Oursteps\nWHERE&#160;&#160; typeOfHit &gt; 0&#160; \n<\/pre>\n<p>&#8230;which will give you something like&#8230;<\/p>\n<p class=\"illustration\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/935-result.jpg\" width=\"630\" height=\"493\" alt=\"935-result.jpg\" \/><\/p>\n<\/p>\n<h1>Using System Tables<\/h1>\n<p>Now, you&#8217;ll see that this will only work with SQL Server 2005 or 2008. This shouldn&#8217;t stop you dead in your tracks if you are stuck on SQL Server 2000 or older, because there are still ways to do it. Instead of using the catalog views, you have to use the system tables. The mapping between the System tables and the System catalog views are all&#160; listed for you on Books on Line here. The big problem is that the code of each routine is stored in as many NVARCHAR(4000) chunks as is required to hold the entire definition, which complicates the code. &#160;&#160;A solution that I use for searching through the definitions of routines in SQL Server 2000 database is given here <a href=\"http:\/\/www.simple-talk.com\/community\/blogs\/philfactor\/archive\/2006\/06\/03\/854.aspx\">http:\/\/www.simple-talk.com\/community\/blogs\/philfactor\/archive\/2006\/06\/03\/854.aspx<\/a>&#160; but I wouldn&#8217;t want to bulk up this article with a SQL Server 2000 version of the <b>FindString <\/b>code. <\/p>\n<h1>In Conclusion<\/h1>\n<p>I use all the methods I&#8217;ve described for perusing the structures in databases. If I want quick information in the course of writing a database routine, I use <a href=\"http:\/\/www.red-gate.com\/products\/SQL_Search\/index.htm?utm_source=simpletalk&amp;utm_medium=article&amp;utm_content=philsearch\">SQL Search<\/a>. When I&#8217;m doing some intensive refactoring, I&#8217;ll use the full Build script, but always in a Programmers Text Editor rather than SSMS.&#160; (I once hit Execute instead of &#8216;open file&#8217;, and deleted a database). &#160;I use Information Schema Views wherever possible as they are reasonably future-proof and open-standard. &#160;I use&#160; Object Catalog views when I can&#8217;t get what I want from Information_Schema views, and use System tables when I&#8217;m having to use older versions of SQL Server. <\/p>\n<p>I realise, with a guilty start, that this article has been slightly self-centred in that I&#8217;ve talked about my own preferences and work practices. If you use a different approach to searching your database DDL scripts that I haven&#8217;t mentioned, then I&#8217;d be fascinated to hear about in it a comment on this article.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>You&#8217;d have thought that nothing would be easier than using  SQL Server Management Studio (SSMS) for searching through the DDL for both the names and definitions of the structural metadata of your databases, for the occurrence of a particular string of letters.  Not so easy, it turns out, though Phil Factor is able to come up with various methods for various purposes.&hellip;<\/p>\n","protected":false},"author":154613,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[143531],"tags":[4168,4150,4151,4252],"coauthors":[],"class_list":["post-795","post","type-post","status-publish","format-standard","hentry","category-t-sql-programming-sql-server","tag-database","tag-sql","tag-sql-server","tag-t-sql-programming"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/795","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/154613"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=795"}],"version-history":[{"count":5,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/795\/revisions"}],"predecessor-version":[{"id":92553,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/795\/revisions\/92553"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=795"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=795"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=795"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=795"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}