{"id":304,"date":"2019-06-08T18:00:18","date_gmt":"2019-06-08T18:00:18","guid":{"rendered":"https:\/\/datablog.roman-halliday.com\/?p=304"},"modified":"2019-06-12T06:10:37","modified_gmt":"2019-06-12T06:10:37","slug":"object-catalog-views-table-metadata","status":"publish","type":"post","link":"https:\/\/datablog.roman-halliday.com\/index.php\/2019\/06\/08\/object-catalog-views-table-metadata\/","title":{"rendered":"Object Catalog Views: Table Metadata"},"content":{"rendered":"<p>It&#8217;s handy to programmatically get metadata on tables (column names and datatypes), either to review implementation, or as part of a process automating code creation. This is a quick post, like with my post on <a href=\"https:\/\/datablog.roman-halliday.com\/index.php\/2018\/03\/01\/casual-logging\/\" target=\"_blank\" rel=\"noopener noreferrer\">Casual Logging<\/a> it&#8217;s a handy bit of copy\/paste code.<\/p>\n<p>This uses some of the array of the <a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/relational-databases\/system-catalog-views\/object-catalog-views-transact-sql?view=sql-server-2017\" target=\"_blank\" rel=\"noopener noreferrer\">Object Catalog Views<\/a> in Microsoft SQL Server, which allow us to view metadata about our database in tables.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\">--------------------------------------------------------------------------------\r\n-- View Table Metadata\r\n--------------------------------------------------------------------------------\r\nSELECT t.[object_id],\r\n       SCHEMA_NAME(t.[schema_id]) AS [schema_name],\r\n       t.[name]                   AS [table_name],\r\n       c.[column_id],\r\n       c.[name]                   AS [column_name],\r\n       c.[is_nullable],\r\n       tt.[name]                  AS [data_type],\r\n       c.[max_length],\r\n       c.[precision],\r\n       c.[scale]\r\n  FROM sys.tables t\r\n    INNER JOIN sys.all_columns c\r\n       ON c.[object_id] = t.[object_id]\r\n     LEFT JOIN sys.[types] tt\r\n       ON tt.[user_type_id] = c.[user_type_id]\r\nWHERE 1=1\r\n  AND t.[name] LIKE 'customer%'\r\n--AND t.[object_id] = OBJECT_ID('customers', 'U')\r\nORDER BY t.[name],\r\n       c.[column_id]\r\n;\r\n<\/pre>\n<p>See it in action:\u00a0<a href=\"https:\/\/rextester.com\/JHRR11794\" target=\"_blank\" rel=\"noopener noreferrer\">rextester.com: SQL Server &#8211; View Table Metadata<\/a><\/p>\n<p>There are many other views which can enable us to see extended attributes about tables including:<\/p>\n<ul>\n<li>Partitions (note, see the samples at the bottom of\u00a0<a href=\"https:\/\/datablog.roman-halliday.com\/index.php\/2019\/02\/02\/partitions-in-sql-server-creating-a-partitioned-table\/\">Partitions in SQL Server: Creating a Partitioned Table<\/a>)<\/li>\n<li>Constraints (including primary keys)<\/li>\n<li>Foreign Keys<\/li>\n<li>Triggers<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s handy to programmatically get metadata on tables (column names and datatypes), either to review implementation, or as part of a process automating code creation.<\/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":[7,8],"class_list":["post-304","post","type-post","status-publish","format-standard","hentry","category-databases","category-sql-server","tag-sql","tag-t-sql"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Object Catalog Views: Table Metadata - Rows Across The Lake<\/title>\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\/2019\/06\/08\/object-catalog-views-table-metadata\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Object Catalog Views: Table Metadata - Rows Across The Lake\" \/>\n<meta property=\"og:description\" content=\"It&#039;s handy to programmatically get metadata on tables (column names and datatypes), either to review implementation, or as part of a process automating code creation.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/datablog.roman-halliday.com\/index.php\/2019\/06\/08\/object-catalog-views-table-metadata\/\" \/>\n<meta property=\"og:site_name\" content=\"Rows Across The Lake\" \/>\n<meta property=\"article:published_time\" content=\"2019-06-08T18:00:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-06-12T06:10:37+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=\"1 minute\" \/>\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\\\/2019\\\/06\\\/08\\\/object-catalog-views-table-metadata\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/index.php\\\/2019\\\/06\\\/08\\\/object-catalog-views-table-metadata\\\/\"},\"author\":{\"name\":\"david\",\"@id\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/#\\\/schema\\\/person\\\/575f96d2590c3085923ff9e1b565748b\"},\"headline\":\"Object Catalog Views: Table Metadata\",\"datePublished\":\"2019-06-08T18:00:18+00:00\",\"dateModified\":\"2019-06-12T06:10:37+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/index.php\\\/2019\\\/06\\\/08\\\/object-catalog-views-table-metadata\\\/\"},\"wordCount\":131,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/#\\\/schema\\\/person\\\/575f96d2590c3085923ff9e1b565748b\"},\"keywords\":[\"SQL\",\"T-SQL\"],\"articleSection\":[\"Databases\",\"SQL Server\"],\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/datablog.roman-halliday.com\\\/index.php\\\/2019\\\/06\\\/08\\\/object-catalog-views-table-metadata\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/index.php\\\/2019\\\/06\\\/08\\\/object-catalog-views-table-metadata\\\/\",\"url\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/index.php\\\/2019\\\/06\\\/08\\\/object-catalog-views-table-metadata\\\/\",\"name\":\"Object Catalog Views: Table Metadata - Rows Across The Lake\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/#website\"},\"datePublished\":\"2019-06-08T18:00:18+00:00\",\"dateModified\":\"2019-06-12T06:10:37+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/index.php\\\/2019\\\/06\\\/08\\\/object-catalog-views-table-metadata\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/datablog.roman-halliday.com\\\/index.php\\\/2019\\\/06\\\/08\\\/object-catalog-views-table-metadata\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/index.php\\\/2019\\\/06\\\/08\\\/object-catalog-views-table-metadata\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Object Catalog Views: Table Metadata\"}]},{\"@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":"Object Catalog Views: Table Metadata - Rows Across The Lake","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\/2019\/06\/08\/object-catalog-views-table-metadata\/","og_locale":"en_GB","og_type":"article","og_title":"Object Catalog Views: Table Metadata - Rows Across The Lake","og_description":"It's handy to programmatically get metadata on tables (column names and datatypes), either to review implementation, or as part of a process automating code creation.","og_url":"https:\/\/datablog.roman-halliday.com\/index.php\/2019\/06\/08\/object-catalog-views-table-metadata\/","og_site_name":"Rows Across The Lake","article_published_time":"2019-06-08T18:00:18+00:00","article_modified_time":"2019-06-12T06:10:37+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":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/datablog.roman-halliday.com\/index.php\/2019\/06\/08\/object-catalog-views-table-metadata\/#article","isPartOf":{"@id":"https:\/\/datablog.roman-halliday.com\/index.php\/2019\/06\/08\/object-catalog-views-table-metadata\/"},"author":{"name":"david","@id":"https:\/\/datablog.roman-halliday.com\/#\/schema\/person\/575f96d2590c3085923ff9e1b565748b"},"headline":"Object Catalog Views: Table Metadata","datePublished":"2019-06-08T18:00:18+00:00","dateModified":"2019-06-12T06:10:37+00:00","mainEntityOfPage":{"@id":"https:\/\/datablog.roman-halliday.com\/index.php\/2019\/06\/08\/object-catalog-views-table-metadata\/"},"wordCount":131,"commentCount":0,"publisher":{"@id":"https:\/\/datablog.roman-halliday.com\/#\/schema\/person\/575f96d2590c3085923ff9e1b565748b"},"keywords":["SQL","T-SQL"],"articleSection":["Databases","SQL Server"],"inLanguage":"en-GB","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/datablog.roman-halliday.com\/index.php\/2019\/06\/08\/object-catalog-views-table-metadata\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/datablog.roman-halliday.com\/index.php\/2019\/06\/08\/object-catalog-views-table-metadata\/","url":"https:\/\/datablog.roman-halliday.com\/index.php\/2019\/06\/08\/object-catalog-views-table-metadata\/","name":"Object Catalog Views: Table Metadata - Rows Across The Lake","isPartOf":{"@id":"https:\/\/datablog.roman-halliday.com\/#website"},"datePublished":"2019-06-08T18:00:18+00:00","dateModified":"2019-06-12T06:10:37+00:00","breadcrumb":{"@id":"https:\/\/datablog.roman-halliday.com\/index.php\/2019\/06\/08\/object-catalog-views-table-metadata\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/datablog.roman-halliday.com\/index.php\/2019\/06\/08\/object-catalog-views-table-metadata\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/datablog.roman-halliday.com\/index.php\/2019\/06\/08\/object-catalog-views-table-metadata\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/datablog.roman-halliday.com\/"},{"@type":"ListItem","position":2,"name":"Object Catalog Views: Table Metadata"}]},{"@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\/304","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=304"}],"version-history":[{"count":1,"href":"https:\/\/datablog.roman-halliday.com\/index.php\/wp-json\/wp\/v2\/posts\/304\/revisions"}],"predecessor-version":[{"id":305,"href":"https:\/\/datablog.roman-halliday.com\/index.php\/wp-json\/wp\/v2\/posts\/304\/revisions\/305"}],"wp:attachment":[{"href":"https:\/\/datablog.roman-halliday.com\/index.php\/wp-json\/wp\/v2\/media?parent=304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/datablog.roman-halliday.com\/index.php\/wp-json\/wp\/v2\/categories?post=304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/datablog.roman-halliday.com\/index.php\/wp-json\/wp\/v2\/tags?post=304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}