解析SQL脚本以提取表名和列名

时间:2015-06-26 20:23:02

标签: python mysql sql sql-server parsing

如果我有一个SQL脚本,那么可以通过这种方式提取所使用的列和引用到表中的表,如下所示:

脚本:

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

输出:

Table   Column
owners  first
owners  last
owners  owner_id
cars    car
cars    make
cars    year
cars    owner_id

2 个答案:

答案 0 :(得分:1)

一个老问题,但很有趣,所以就这样-暂时将脚本转换为存储过程,强制SQL Server映射依赖关系,然后可以使用以下方法检索它们:

SELECT referenced_entity_name ,referenced_minor_name FROM sys.dm_sql_referenced_entities('dbo.stp_ObjectsToTrack', 'Object')

答案 1 :(得分:0)

这就是你想要的SQL Server:



select t.name as [Table], c.name as [Column]
from sys.columns c
inner join sys.tables t
on c.object_id = t.object_id