{"id":441705,"date":"2016-04-05T13:17:10","date_gmt":"2016-04-05T18:17:10","guid":{"rendered":"https:\/\/mkncreations.com\/site\/?p=441705"},"modified":"2017-01-18T10:03:51","modified_gmt":"2017-01-18T15:03:51","slug":"tsql-search-for-tableview-by-column-name","status":"publish","type":"post","link":"https:\/\/mkncreations.com\/site\/2016\/04\/tsql-search-for-tableview-by-column-name\/","title":{"rendered":"TSQL &#8211; Search for table\/view by column name"},"content":{"rendered":"<p>If you are trying to find a table or a view based on a column name here is a piece of code that will be very helpful to you.<br \/>\nYou will get the following:<br \/>\n1) full column name<br \/>\n2) object name (including schema)<br \/>\n3) object type (table\/view)<br \/>\n4) data type (nice format: varchar(6) or numeric(5,2), etc.)<br \/>\n5) null\/not null<br \/>\n6) information on identity, check constraint, and default info<\/p>\n<blockquote><p>\nDECLARE @Search varchar(200)<br \/>\nSET @Search=&#8217;YourColumnName&#8217;  &#8211;can be a partial or a complete name<\/p>\n<p>SELECT<br \/>\n    s.name as ColumnName<br \/>\n        ,sh.name+&#8217;.&#8217;+o.name AS ObjectName<br \/>\n        ,o.type_desc AS ObjectType<br \/>\n        ,CASE<br \/>\n             WHEN t.name IN (&#8216;char&#8217;,&#8217;varchar&#8217;) THEN t.name+'(&#8216;+CASE WHEN s.max_length<0 then 'MAX' ELSE CONVERT(varchar(10),s.max_length) END+')'\n             WHEN t.name IN ('nvarchar','nchar') THEN t.name+'('+CASE WHEN s.max_length<0 then 'MAX' ELSE CONVERT(varchar(10),s.max_length\/2) END+')'\n            WHEN t.name IN ('numeric') THEN t.name+'('+CONVERT(varchar(10),s.precision)+','+CONVERT(varchar(10),s.scale)+')'\n             ELSE t.name\n         END AS DataType\n\n        ,CASE\n             WHEN s.is_nullable=1 THEN 'NULL'\n            ELSE 'NOT NULL'\n        END AS Nullable\n        ,CASE\n             WHEN ic.column_id IS NULL THEN ''\n             ELSE ' identity('+ISNULL(CONVERT(varchar(10),ic.seed_value),'') + ',' + ISNULL(CONVERT(varchar(10),ic.increment_value),'') +') = '+ ISNULL(CONVERT(varchar(10),ic.last_value),'null')\n         END\n        +CASE\n             WHEN sc.column_id IS NULL THEN ''\n             ELSE ' computed('+ISNULL(sc.definition,'')+')'\n         END\n        +CASE\n             WHEN cc.object_id IS NULL THEN ''\n             ELSE ' check('+ISNULL(cc.definition,'')+')'\n         END\n            AS MiscInfo\n    FROM sys.columns                           s\n        INNER JOIN sys.types                   t ON s.system_type_id=t.system_type_id and t.is_user_defined=0\n        INNER JOIN sys.objects                 o ON s.object_id=o.object_id\n        INNER JOIN sys.schemas                sh on o.schema_id=sh.schema_id\n        LEFT OUTER JOIN sys.identity_columns  ic ON s.object_id=ic.object_id AND s.column_id=ic.column_id\n        LEFT OUTER JOIN sys.computed_columns  sc ON s.object_id=sc.object_id AND s.column_id=sc.column_id\n        LEFT OUTER JOIN sys.check_constraints cc ON s.object_id=cc.parent_object_id AND s.column_id=cc.parent_column_id\n    WHERE s.name LIKE '%'+@Search+'%'\n<\/p><\/blockquote>\n<p>h\/t <a href=\"http:\/\/stackoverflow.com\/users\/65223\/km\" target=\"_blank\">KM.<\/a><br \/>\n<a href=\"http:\/\/stackoverflow.com\/questions\/3761673\/sql-server-search-for-a-column-by-name\" target=\"_blank\">source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are trying to find a table or a view based on a column name here is a piece of code that will be&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,12],"tags":[61,79,21,166,165,50,78,49],"class_list":["post-441705","post","type-post","status-publish","format-standard","hentry","category-how_to","category-sql","tag-column","tag-microsoft","tag-ms","tag-name","tag-search","tag-sql-2","tag-sql2008","tag-tsql"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/mkncreations.com\/site\/wp-json\/wp\/v2\/posts\/441705"}],"collection":[{"href":"https:\/\/mkncreations.com\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mkncreations.com\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mkncreations.com\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mkncreations.com\/site\/wp-json\/wp\/v2\/comments?post=441705"}],"version-history":[{"count":8,"href":"https:\/\/mkncreations.com\/site\/wp-json\/wp\/v2\/posts\/441705\/revisions"}],"predecessor-version":[{"id":671887,"href":"https:\/\/mkncreations.com\/site\/wp-json\/wp\/v2\/posts\/441705\/revisions\/671887"}],"wp:attachment":[{"href":"https:\/\/mkncreations.com\/site\/wp-json\/wp\/v2\/media?parent=441705"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mkncreations.com\/site\/wp-json\/wp\/v2\/categories?post=441705"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mkncreations.com\/site\/wp-json\/wp\/v2\/tags?post=441705"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}