Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse SQL Script to extract table and column names

If I have a SQL script is there a way to parse and extract the columns and tables referenced in the script into a table like structure :

Script:

Select t1.first, t1.last, t2.car, t2.make, t2.year
from owners t1
left join cars t2
on t1.owner_id = t2.owner_id

Output:

Table   Column
owners  first
owners  last
owners  owner_id
cars    car
cars    make
cars    year
cars    owner_id
like image 600
screechOwl Avatar asked Jun 26 '15 20:06

screechOwl


People also ask

How can I get column names and values from a table in SQL?

USE db_name; DESCRIBE table_name; it'll give you column names with the type.

How extract all table names in SQL?

In MySQL, there are two ways to find the names of all tables, either by using the "show" keyword or by query INFORMATION_SCHEMA. In the case of SQL Server or MSSQL, You can either use sys. tables or INFORMATION_SCHEMA to get all table names for a database.


1 Answers

Old question but interesting so here it goes - turn your script temporarily into a stored procedure forcing SQL Server to map the dependencies and then you can retrieve them by using:

SELECT referenced_entity_name ,referenced_minor_name FROM sys.dm_sql_referenced_entities('dbo.stp_ObjectsToTrack', 'Object')
like image 122
Altair Souza Avatar answered Nov 03 '22 00:11

Altair Souza