{"id":655,"date":"2010-11-30T02:13:27","date_gmt":"2010-11-30T07:13:27","guid":{"rendered":"http:\/\/www.dpriver.com\/blog\/?page_id=655"},"modified":"2015-05-07T03:44:06","modified_gmt":"2015-05-07T08:44:06","slug":"get-columns-and-tables-in-sql-script-net-version","status":"publish","type":"page","link":"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/get-columns-and-tables-in-sql-script-net-version\/","title":{"rendered":"Get columns and tables in SQL script (.NET version)"},"content":{"rendered":"<p><strong>THIS DEMO IS DEPRECIATED SINCE GENERAL SQL PARSER .NET VERSION 2.6.0. <\/strong>please <a href=\"http:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/listing-all-table-and-column-references\/\">check this new version<\/a> instead.<\/p>\n<p>The logic to find all tables and columns in SQL script is quite simple like this:<\/p>\n<p>1. Parse SQL script.<\/p>\n<p>2. Iterate all statements via TGSqlParser.sqlstatements and TCustomSqlStatement.ChildNodes().<\/p>\n<p>3. Once a statement was found, get all table tokens belong to this statement via TCustomSqlStatement.TableTokens.<\/p>\n<p>4. Once a table token was found, get all columns belong to this table via various TSourceToken properties explained belowed.<\/p>\n<p>Input SQL:<\/p>\n<pre>\r\nSELECT e.last_name,\r\n       e.department_id,\r\n       d.department_name\r\nFROM   employees e\r\n       LEFT OUTER JOIN department d\r\n         ON ( e.department_id = d.department_id ); \r\n<\/pre>\n<p>Tables and columns:<\/p>\n<pre>\r\nTables:\r\ndepartment\r\nemployees\r\n\r\nColumns\r\ndepartment.department_id\r\ndepartment.department_name\r\nemployees.department_id\r\nemployees.last_name\r\n<\/pre>\n<p>If you want to print columns in the same order they appear in sql statement, just append &#8220;-s&#8221; parameter at command line of this demo, then it will generate following result:<\/p>\n<pre>\r\npos        column          table         t-alias  schema   DB  Loc                                     \r\n[  1, 10]  last_name       employees     e        \t\t\t\t\t\t\tcolumnResult\/select list                \r\n[  2, 10]  department_id   employees     e        \t\t\t\t\t\t\tcolumnResult\/select list                \r\n[  3, 10]  department_name department    d        \t\t\t\t\t\t\tcolumnResult\/select list                \r\n[  6, 17]  department_id   employees     e        \t\t\t\t\t\t\tjoin condition (e.department_id = d.department_id)\r\n[  6, 35]  department_id   department    d        \t\t\t\t\t\t\tjoin condition (e.department_id = d.department_id)\r\n<\/pre>\n<p>Since .NET version 2.2.1, user can set a callback function to TGSqlParser.OnMetaDatabaseTableColumn to provide detailed meta information from database to help SQL parser to determine relationship between column and tables. <\/p>\n<pre>\r\nSELECT\r\n   Quantity,b.Time,c.Description\r\nFROM\r\n  (SELECT ID,Time FROM bTab) b\r\n  INNER JOIN aTab a on a.ID=b.ID\r\n  INNER JOIN cTab c on a.ID=c.ID\r\n<\/pre>\n<p>General SQL Parser can link column: Quantity to table: cTab automatically if your callback function says table: cTab includes column: Quantity. Without information provided by this callback function, General SQL Parser will link column: Quantity to table: aTab which is the first possible table.<\/p>\n<p>Download this demo: <a href=\"http:\/\/www.dpriver.com\/gsp\/demos\/csharp\/gettablecolumn\/getTableColumn.zip\">C# demo<\/a><\/p>\n<p><a href=\"http:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/get-columns-and-tables-in-sql-script\/\">Java version of this document<\/a><\/p>\n<p><a href=\"http:\/\/www.dpriver.com\/gsp\/demos\/c\/gettablecolumns\/gettablecolumns.zip\">ANSI C demo<\/a><\/p>\n<p>In order to understand this demo listed, here are some conceptions you should know.<\/p>\n<pre>\r\nselect table1.f1, t1.f2\r\nfrom table1 t1\r\nwhere t1.f3 = 1\r\n<\/pre>\n<p>Token <strong>&#8216;table1&#8217;<\/strong> in from clause is a <strong>declared table token<\/strong>.<br \/>\nToken <strong>&#8216;table1&#8217;<\/strong> in select list is a <strong>referenced table token<\/strong>.<br \/>\nToken <strong>&#8216;t1&#8217;<\/strong> in from clause is a <strong>declared table alias token<\/strong>.<br \/>\nToken <strong>&#8216;t1&#8217;<\/strong> in select list and where clause is a <strong>referenced table alias token<\/strong>. <\/p>\n<p>Only declared table token will be listed in TCustomSqlStatement.TableTokens, referenced table token, declared table alias token and referenced table alias token can be found in RelatedToken\/RelatedTokens property of declared table token. <\/p>\n<p>Temp table, table variable and declare CTE table will also be listed in TCustomSqlStatement.TableTokens.<br \/>\nDBObjType property of TSourceToken cab be used to find out those tokens. <\/p>\n<p>Following properties in TSourceToken Class had different meanings according to what&#8217;s kind of table type this token is. <\/p>\n<ul>\n<li>table token is a declared table token\n<ul>\n<li>ParentToken: schema token if any <\/li>\n<li>ChildToken is null<\/li>\n<li>RelatedToken: table alias of this declared table token<\/li>\n<li>RelatedTokens:\n<ul>\n<li>1. referenced table token of this declared table<\/li>\n<li>2. columns attached to this declared table in-directly. (such as field in create table)<\/li>\n<li>3. columns not linked explicit with table like column f in this sql: select f from t<\/li>\n<p>we can distinguish column token(2,3) and referenced token(1) of this declared table in RelatedTokens by using TSourceToken.DBObjType property, column token has vlaue: TDBObjType.ttObjField\n                <\/ul>\n<\/li>\n<li>RelatedSubquery is null.<\/li>\n<\/ul>\n<\/li>\n<li>table token is a referenced table token\n<ul>\n<li>ParentToken: schema token if any.<\/li>\n<li>ChildToken: column attached to this referenced table token<\/li>\n<\/ul>\n<\/li>\n<li>table token is a declared table alias token\n<ul>\n<li>RelatedTokens: referenced tokens of this declared table alias.<\/li>\n<li>RelatedSubquery, subquery if this table alias is linked to a subquery in from clause<\/li>\n<\/ul>\n<\/li>\n<li>table token is a referenced table alias token\n<ul>\n<li>ChildToken: column attached to this referenced table alias token.<\/li>\n<\/ul>\n<\/li>\n<li>table token is a temp table token\n<ul>\n<li>RelatedTokens, column attached to this table<\/li>\n<\/ul>\n<\/li>\n<li>a table variable token:(DBObjType = ttobjtableTemp)\n<ul>\n<li>RelatedTokens, columns attach to this table. <\/li>\n<\/ul>\n<\/li>\n<li>a declared CTE table token:(DBObjType = ttobjtablecte)\n<ul>\n<li>RelatedTokens: referenced table of this cte table<\/li>\n<li>fields attach to this cte table<\/li>\n<\/ul>\n<\/li>\n<li>a referenced CTE table token\n<ul>\n<li>RelatedToken, alias of this refenced cte table<\/li>\n<li>RelatedTokens: columns attached to this referened cte table <\/li>\n<\/ul>\n<\/li>\n<li>an alias of referenced cte table token\n<ul>\n<li>RelatedTokens: columns attach to this ref cte table<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>Download this demo: <a href=\"http:\/\/www.dpriver.com\/gsp\/demos\/csharp\/gettablecolumn\/getTableColumn.zip\">C# demo<\/a>, <a href=\"http:\/\/www.dpriver.com\/gsp\/demos\/java\/gettablecolumns\/gettablecolumns.zip\">Java demo<\/a><br \/>\n, <a href=\"http:\/\/www.dpriver.com\/gsp\/demos\/c\/gettablecolumns\/gettablecolumns.zip\">ANSI C demo<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>THIS DEMO IS DEPRECIATED SINCE GENERAL SQL PARSER .NET VERSION 2.6.0. please check this new version instead. The logic to find all tables and columns in SQL script is quite simple like this: 1. Parse SQL script. 2. Iterate all statements via TGSqlParser.sqlstatements and TCustomSqlStatement.ChildNodes(). 3. Once a statement was found, get all table tokens belong to this statement via TCustomSqlStatement.TableTokens. 4. Once a table token was found, get all columns belong to this table via various TSourceToken properties explained belowed. Input SQL: SELECT e.last_name, e.department_id, d.department_name FROM employees e LEFT OUTER JOIN department d ON ( e.department_id = d.department_id\u2026<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":235,"menu_order":0,"comment_status":"closed","ping_status":"open","template":"gsp_feature_page_tt.php","meta":[],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":5}},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v19.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Help you to make better use of General SQL Parser<\/title>\n<meta name=\"description\" content=\"Get columns and tables in SQL script (.NET version)\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/get-columns-and-tables-in-sql-script-net-version\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Help you to make better use of General SQL Parser\" \/>\n<meta property=\"og:description\" content=\"Get columns and tables in SQL script (.NET version)\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/get-columns-and-tables-in-sql-script-net-version\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL and Data Blog\" \/>\n<meta property=\"article:modified_time\" content=\"2015-05-07T08:44:06+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.dpriver.com\/blog\/#organization\",\"name\":\"SQL and Data Blog\",\"url\":\"https:\/\/www.dpriver.com\/blog\/\",\"sameAs\":[],\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.dpriver.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.dpriver.com\/blog\/wp-content\/uploads\/2022\/07\/sqlpp-character.png\",\"contentUrl\":\"https:\/\/www.dpriver.com\/blog\/wp-content\/uploads\/2022\/07\/sqlpp-character.png\",\"width\":251,\"height\":72,\"caption\":\"SQL and Data Blog\"},\"image\":{\"@id\":\"https:\/\/www.dpriver.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.dpriver.com\/blog\/#website\",\"url\":\"https:\/\/www.dpriver.com\/blog\/\",\"name\":\"SQL and Data Blog\",\"description\":\"SQL related blog for database professional\",\"publisher\":{\"@id\":\"https:\/\/www.dpriver.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.dpriver.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/get-columns-and-tables-in-sql-script-net-version\/\",\"url\":\"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/get-columns-and-tables-in-sql-script-net-version\/\",\"name\":\"Help you to make better use of General SQL Parser\",\"isPartOf\":{\"@id\":\"https:\/\/www.dpriver.com\/blog\/#website\"},\"datePublished\":\"2010-11-30T07:13:27+00:00\",\"dateModified\":\"2015-05-07T08:44:06+00:00\",\"description\":\"Get columns and tables in SQL script (.NET version)\",\"breadcrumb\":{\"@id\":\"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/get-columns-and-tables-in-sql-script-net-version\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/get-columns-and-tables-in-sql-script-net-version\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/get-columns-and-tables-in-sql-script-net-version\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.dpriver.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"List of demos illustrate how to use general sql parser\",\"item\":\"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Get columns and tables in SQL script (.NET version)\"}]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Help you to make better use of General SQL Parser","description":"Get columns and tables in SQL script (.NET version)","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:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/get-columns-and-tables-in-sql-script-net-version\/","og_locale":"en_US","og_type":"article","og_title":"Help you to make better use of General SQL Parser","og_description":"Get columns and tables in SQL script (.NET version)","og_url":"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/get-columns-and-tables-in-sql-script-net-version\/","og_site_name":"SQL and Data Blog","article_modified_time":"2015-05-07T08:44:06+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/www.dpriver.com\/blog\/#organization","name":"SQL and Data Blog","url":"https:\/\/www.dpriver.com\/blog\/","sameAs":[],"logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.dpriver.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.dpriver.com\/blog\/wp-content\/uploads\/2022\/07\/sqlpp-character.png","contentUrl":"https:\/\/www.dpriver.com\/blog\/wp-content\/uploads\/2022\/07\/sqlpp-character.png","width":251,"height":72,"caption":"SQL and Data Blog"},"image":{"@id":"https:\/\/www.dpriver.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"WebSite","@id":"https:\/\/www.dpriver.com\/blog\/#website","url":"https:\/\/www.dpriver.com\/blog\/","name":"SQL and Data Blog","description":"SQL related blog for database professional","publisher":{"@id":"https:\/\/www.dpriver.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.dpriver.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/get-columns-and-tables-in-sql-script-net-version\/","url":"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/get-columns-and-tables-in-sql-script-net-version\/","name":"Help you to make better use of General SQL Parser","isPartOf":{"@id":"https:\/\/www.dpriver.com\/blog\/#website"},"datePublished":"2010-11-30T07:13:27+00:00","dateModified":"2015-05-07T08:44:06+00:00","description":"Get columns and tables in SQL script (.NET version)","breadcrumb":{"@id":"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/get-columns-and-tables-in-sql-script-net-version\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/get-columns-and-tables-in-sql-script-net-version\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/get-columns-and-tables-in-sql-script-net-version\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.dpriver.com\/blog\/"},{"@type":"ListItem","position":2,"name":"List of demos illustrate how to use general sql parser","item":"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/"},{"@type":"ListItem","position":3,"name":"Get columns and tables in SQL script (.NET version)"}]}]}},"_links":{"self":[{"href":"https:\/\/www.dpriver.com\/blog\/wp-json\/wp\/v2\/pages\/655"}],"collection":[{"href":"https:\/\/www.dpriver.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.dpriver.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.dpriver.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dpriver.com\/blog\/wp-json\/wp\/v2\/comments?post=655"}],"version-history":[{"count":35,"href":"https:\/\/www.dpriver.com\/blog\/wp-json\/wp\/v2\/pages\/655\/revisions"}],"predecessor-version":[{"id":1647,"href":"https:\/\/www.dpriver.com\/blog\/wp-json\/wp\/v2\/pages\/655\/revisions\/1647"}],"up":[{"embeddable":true,"href":"https:\/\/www.dpriver.com\/blog\/wp-json\/wp\/v2\/pages\/235"}],"wp:attachment":[{"href":"https:\/\/www.dpriver.com\/blog\/wp-json\/wp\/v2\/media?parent=655"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}