{"id":461,"date":"2021-12-10T23:33:04","date_gmt":"2021-12-11T04:33:04","guid":{"rendered":"http:\/\/techref.camellarry.com\/?p=461"},"modified":"2023-11-28T12:23:49","modified_gmt":"2023-11-28T17:23:49","slug":"get-list-of-database-tables-and-fields","status":"publish","type":"post","link":"http:\/\/techref.camellarry.com\/?p=461","title":{"rendered":"SQL get list of db tables and fields"},"content":{"rendered":"<p>This is how to get a list of all tables and fields in a database.<\/p>\n<blockquote><p>SELECT T.name AS Table_Name ,<br \/>\nC.name AS Column_Name ,<br \/>\nP.name AS Data_Type ,<br \/>\nP.max_length AS Size ,<br \/>\nCAST(P.precision AS VARCHAR) + &#8216;\/&#8217; + CAST(P.scale AS VARCHAR) AS Precision_Scale<br \/>\nFROM sys.objects AS T<br \/>\nJOIN sys.columns AS C ON T.object_id = C.object_id<br \/>\nJOIN sys.types AS P ON C.system_type_id = P.system_type_id<br \/>\nWHERE T.type_desc = &#8216;USER_TABLE&#8217;<br \/>\nand not P.name = &#8216;sysname&#8217;<\/p><\/blockquote>\n<p>Alternatively for a single table:<\/p>\n<blockquote><p>SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH<br \/>\nFROM INFORMATION_SCHEMA.COLUMNS<br \/>\nWHERE TABLE_NAME = &#8216;expense2&#8217;<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>This is how to get a list of all tables and fields in a database. SELECT T.name AS Table_Name , C.name AS Column_Name , P.name AS Data_Type , P.max_length AS Size , CAST(P.precision AS VARCHAR) + &#8216;\/&#8217; + CAST(P.scale AS VARCHAR) AS Precision_Scale FROM sys.objects AS T JOIN sys.columns AS C ON T.object_id = C.object_id [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[8],"tags":[94,29,70],"class_list":["post-461","post","type-post","status-publish","format-standard","hentry","category-sql","tag-database","tag-example","tag-sql"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9t3oE-7r","_links":{"self":[{"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=\/wp\/v2\/posts\/461","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=461"}],"version-history":[{"count":2,"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=\/wp\/v2\/posts\/461\/revisions"}],"predecessor-version":[{"id":505,"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=\/wp\/v2\/posts\/461\/revisions\/505"}],"wp:attachment":[{"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=461"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=461"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=461"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}