{"id":1548,"date":"2013-01-25T01:34:02","date_gmt":"2013-01-25T06:34:02","guid":{"rendered":"http:\/\/www.dpriver.com\/blog\/?page_id=1548"},"modified":"2013-08-02T02:43:04","modified_gmt":"2013-08-02T07:43:04","slug":"scan-sql-stored-proceduresudfsviews-return-the-table-name-with-columns","status":"publish","type":"page","link":"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/scan-sql-stored-proceduresudfsviews-return-the-table-name-with-columns\/","title":{"rendered":"Scan SQL stored procedures\/udfs\/views, return the table name with columns"},"content":{"rendered":"<p>Iterate through SQL stored procedures, udfs, and views returning the table name with column names referenced by SELECT, WHERE, ORDER BY, GROUP BY, and JOIN.<\/p>\n<p>Feed this demo with a SQL script file, generate output like this in stdout or in a text file.<\/p>\n<pre>\r\nscantable test.sql\r\n<\/pre>\n<pre>\r\nTable Name: &lt;Table Name1>\r\n   SELECT:        &lt;ColumnName1>, &lt;ColumnName3>\r\n   WHERE:        &lt;ColumnName1>, &lt;ColumnName2>\r\n   ORDER BY:   &lt;ColumnName>\r\n   GROUP BY:  &lt;ColumnName>\r\n   JOIN:            &lt;ColumName>, &lt;ColumnName2>\r\n \r\n \r\nTable Name: &lt;Table Name2>\r\n   SELECT:        &lt;ColumnName>\r\n   WHERE:        &lt;ColumnName1>, &lt;ColumnName2>\r\n   ORDER BY:   &lt;ColumnName>\r\n   GROUP BY:  &lt;ColumnName>\r\n   JOIN:            &lt;ColumName>\r\n<\/pre>\n<p>Let&#8217;s take this SQL for example:<\/p>\n<pre>\r\nCREATE FUNCTION dbo.ufnGetStock(@ProductID int)\r\nRETURNS int \r\nAS \r\n-- Returns the stock level for the product.\r\nBEGIN\r\n    DECLARE @ret int;\r\n    SELECT @ret = SUM(p.Quantity) \r\n    FROM Production.ProductInventory p \r\n    WHERE p.ProductID = @ProductID \r\n        AND p.LocationID = '6';\r\n     IF (@ret IS NULL) \r\n        SET @ret = 0\r\n    RETURN @ret\r\nEND;\r\nGO\r\n\r\nUSE AdventureWorks;\r\nGO\r\nSELECT ProductModelID, Name, dbo.ufnGetStock(ProductID)AS CurrentSupply\r\nFROM Production.Product\r\nWHERE ProductModelID BETWEEN 75 and 80;\r\nGO\r\n\r\n-- Table-Valued Functions\r\nUSE AdventureWorks;\r\nGO\r\nCREATE FUNCTION Sales.fn_SalesByStore (@storeid int)\r\nRETURNS TABLE\r\nAS\r\nRETURN \r\n(\r\n    SELECT P.ProductID, P.Name, SUM(SD.LineTotal) AS 'YTD Total'\r\n    FROM Production.Product AS P \r\n      JOIN Sales.SalesOrderDetail AS SD ON SD.ProductID = P.ProductID\r\n      JOIN Sales.SalesOrderHeader AS SH ON SH.SalesOrderID = SD.SalesOrderID\r\n    WHERE SH.CustomerID = @storeid\r\n    GROUP BY P.ProductID, P.Name\r\n);\r\nGO\r\n\r\nSELECT * FROM Sales.fn_SalesByStore (602);\r\n\r\nUSE AdventureWorks;\r\nGO\r\nCREATE FUNCTION dbo.fn_FindReports (@InEmpID INTEGER)\r\nRETURNS @retFindReports TABLE \r\n(\r\n    EmployeeID int primary key NOT NULL,\r\n    Name nvarchar(255) NOT NULL,\r\n    Title nvarchar(50) NOT NULL,\r\n    EmployeeLevel int NOT NULL,\r\n    Sort nvarchar (255) NOT NULL\r\n)\r\n--Returns a result set that lists all the employees who report to the \r\n--specific employee directly or indirectly.*\/\r\nAS\r\nBEGIN\r\n   WITH DirectReports(Name, Title, EmployeeID, EmployeeLevel, Sort) AS\r\n    (SELECT CONVERT(Varchar(255), c.FirstName + ' ' + c.LastName),\r\n        e.Title,\r\n        e.EmployeeID,\r\n        1,\r\n        CONVERT(Varchar(255), c.FirstName + ' ' + c.LastName)\r\n     FROM HumanResources.Employee AS e\r\n          JOIN Person.Contact AS c ON e.ContactID = c.ContactID \r\n     WHERE e.EmployeeID = @InEmpID\r\n   UNION ALL\r\n     SELECT CONVERT(Varchar(255), REPLICATE ('| ' , EmployeeLevel) +\r\n        c.FirstName + ' ' + c.LastName),\r\n        e.Title,\r\n        e.EmployeeID,\r\n        EmployeeLevel + 1,\r\n        CONVERT (Varchar(255), RTRIM(Sort) + '| ' + FirstName + ' ' + \r\n                 LastName)\r\n     FROM HumanResources.Employee as e\r\n          JOIN Person.Contact AS c ON e.ContactID = c.ContactID\r\n          JOIN DirectReports AS d ON e.ManagerID = d.EmployeeID\r\n    )\r\n-- copy the required columns to the result of the function \r\n   INSERT @retFindReports\r\n   SELECT EmployeeID, Name, Title, EmployeeLevel, Sort\r\n   FROM DirectReports \r\n   RETURN\r\nEND;\r\nGO\r\n\r\n-- Example invocation\r\nSELECT EmployeeID, Name, Title, EmployeeLevel\r\nFROM dbo.fn_FindReports(109)\r\nORDER BY Sort;\r\n<\/pre>\n<p>Output generated by this tool is:<\/p>\n<pre>\r\nTable Name:\tSales.SalesOrderDetail\r\nSELECT LIST:\tLineTotal\r\nJOIN:\t\tProductID, SalesOrderID\r\n\r\nTable Name:\tSales.SalesOrderHeader\r\nWHERE:\t\tCustomerID\r\nJOIN:\t\tSalesOrderID\r\n\r\nTable Name:\tDirectReports\r\nSELECT LIST:\tEmployeeID, Name, Title, EmployeeLevel, Sort\r\nJOIN:\t\tEmployeeID\r\n\r\nTable Name:\tHumanResources.Employee\r\nSELECT LIST:\tTitle, EmployeeID, EmployeeLevel, Sort, FirstName, LastName\r\nWHERE:\t\tEmployeeID\r\nJOIN:\t\tContactID, ManagerID\r\n\r\nTable Name:\tProduction.ProductInventory\r\nSELECT LIST:\tQuantity\r\nWHERE:\t\tProductID, LocationID\r\n\r\nTable Name:\tProduction.Product\r\nSELECT LIST:\tProductModelID, Name, ProductID\r\nWHERE:\t\tProductModelID\r\nGROUP BY:\tProductID, Name\r\nJOIN:\t\tProductID\r\n\r\nTable Name:\tPerson.Contact\r\nSELECT LIST:\tFirstName, LastName\r\nJOIN:\t\tContactID\r\n<\/pre>\n<p>Download this tool with source code: <a href=\"http:\/\/www.dpriver.com\/gsp\/demos\/csharp\/scantable\/scantable.zip\">.NET version<\/a>, <a href=\"http:\/\/www.dpriver.com\/gsp\/demos\/java\/scantable\/scantable.java\">Java version<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Iterate through SQL stored procedures, udfs, and views returning the table name with column names referenced by SELECT, WHERE, ORDER BY, GROUP BY, and JOIN. Feed this demo with a SQL script file, generate output like this in stdout or in a text file. scantable test.sql Table Name: &lt;Table Name1> SELECT: &lt;ColumnName1>, &lt;ColumnName3> WHERE: &lt;ColumnName1>, &lt;ColumnName2> ORDER BY: &lt;ColumnName> GROUP BY: &lt;ColumnName> JOIN: &lt;ColumName>, &lt;ColumnName2> Table Name: &lt;Table Name2> SELECT: &lt;ColumnName> WHERE: &lt;ColumnName1>, &lt;ColumnName2> ORDER BY: &lt;ColumnName> GROUP BY: &lt;ColumnName> JOIN: &lt;ColumName> Let&#8217;s take this SQL for example: CREATE FUNCTION dbo.ufnGetStock(@ProductID int) RETURNS int AS &#8212; Returns the stock level\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=\"Scan SQL stored procedures\/udfs\/views, return the table name with columns\" \/>\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\/scan-sql-stored-proceduresudfsviews-return-the-table-name-with-columns\/\" \/>\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=\"Scan SQL stored procedures\/udfs\/views, return the table name with columns\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/scan-sql-stored-proceduresudfsviews-return-the-table-name-with-columns\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL and Data Blog\" \/>\n<meta property=\"article:modified_time\" content=\"2013-08-02T07:43:04+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=\"3 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\/scan-sql-stored-proceduresudfsviews-return-the-table-name-with-columns\/\",\"url\":\"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/scan-sql-stored-proceduresudfsviews-return-the-table-name-with-columns\/\",\"name\":\"Help you to make better use of General SQL Parser\",\"isPartOf\":{\"@id\":\"https:\/\/www.dpriver.com\/blog\/#website\"},\"datePublished\":\"2013-01-25T06:34:02+00:00\",\"dateModified\":\"2013-08-02T07:43:04+00:00\",\"description\":\"Scan SQL stored procedures\/udfs\/views, return the table name with columns\",\"breadcrumb\":{\"@id\":\"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/scan-sql-stored-proceduresudfsviews-return-the-table-name-with-columns\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/scan-sql-stored-proceduresudfsviews-return-the-table-name-with-columns\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/scan-sql-stored-proceduresudfsviews-return-the-table-name-with-columns\/#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\":\"Scan SQL stored procedures\/udfs\/views, return the table name with columns\"}]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Help you to make better use of General SQL Parser","description":"Scan SQL stored procedures\/udfs\/views, return the table name with columns","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\/scan-sql-stored-proceduresudfsviews-return-the-table-name-with-columns\/","og_locale":"en_US","og_type":"article","og_title":"Help you to make better use of General SQL Parser","og_description":"Scan SQL stored procedures\/udfs\/views, return the table name with columns","og_url":"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/scan-sql-stored-proceduresudfsviews-return-the-table-name-with-columns\/","og_site_name":"SQL and Data Blog","article_modified_time":"2013-08-02T07:43:04+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"3 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\/scan-sql-stored-proceduresudfsviews-return-the-table-name-with-columns\/","url":"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/scan-sql-stored-proceduresudfsviews-return-the-table-name-with-columns\/","name":"Help you to make better use of General SQL Parser","isPartOf":{"@id":"https:\/\/www.dpriver.com\/blog\/#website"},"datePublished":"2013-01-25T06:34:02+00:00","dateModified":"2013-08-02T07:43:04+00:00","description":"Scan SQL stored procedures\/udfs\/views, return the table name with columns","breadcrumb":{"@id":"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/scan-sql-stored-proceduresudfsviews-return-the-table-name-with-columns\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/scan-sql-stored-proceduresudfsviews-return-the-table-name-with-columns\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/scan-sql-stored-proceduresudfsviews-return-the-table-name-with-columns\/#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":"Scan SQL stored procedures\/udfs\/views, return the table name with columns"}]}]}},"_links":{"self":[{"href":"https:\/\/www.dpriver.com\/blog\/wp-json\/wp\/v2\/pages\/1548"}],"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=1548"}],"version-history":[{"count":10,"href":"https:\/\/www.dpriver.com\/blog\/wp-json\/wp\/v2\/pages\/1548\/revisions"}],"predecessor-version":[{"id":1599,"href":"https:\/\/www.dpriver.com\/blog\/wp-json\/wp\/v2\/pages\/1548\/revisions\/1599"}],"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=1548"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}