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

时间:2022-09-23 22:37:08

If I have a SQL script is there a way to go thru and extract the columns used and tables referenced into a table like this:

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

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

1 个解决方案

#1


0  

This is what you want in SQL Server:

这是您在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

#1


0  

This is what you want in SQL Server:

这是您在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