{"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":"

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

Feed this demo with a SQL script file, generate output like this in stdout or in a text file.<\/p>\n

    \r\nscantable test.sql\r\n<\/pre>\n\r\nTable Name: <Table Name1>\r\n   SELECT:        <ColumnName1>, <ColumnName3>\r\n   WHERE:        <ColumnName1>, <ColumnName2>\r\n   ORDER BY:   <ColumnName>\r\n   GROUP BY:  <ColumnName>\r\n   JOIN:            <ColumName>, <ColumnName2>\r\n \r\n \r\nTable Name: <Table Name2>\r\n   SELECT:        <ColumnName>\r\n   WHERE:        <ColumnName1>, <ColumnName2>\r\n   ORDER BY:   <ColumnName>\r\n   GROUP BY:  <ColumnName>\r\n   JOIN:            <ColumName>\r\n<\/pre>\nLet’s take this SQL for example:<\/p>\n\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>\nOutput generated by this tool is:<\/p>\n\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>\nDownload this tool with source code: .NET version<\/a>, Java version<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"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: <Table Name1> SELECT: <ColumnName1>, <ColumnName3> WHERE: <ColumnName1>, <ColumnName2> ORDER BY: <ColumnName> GROUP BY: <ColumnName> JOIN: <ColumName>, <ColumnName2> Table Name: <Table Name2> SELECT: <ColumnName> WHERE: <ColumnName1>, <ColumnName2> ORDER BY: <ColumnName> GROUP BY: <ColumnName> JOIN: <ColumName> Let’s take this SQL for example: CREATE FUNCTION dbo.ufnGetStock(@ProductID int) RETURNS int AS — 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":"\nHelp you to make better use of General SQL Parser<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n