如何从 SQL Server 中的查询中获取列名

2021-10-26 00:00:00 sql alias sql-server

使用 SQL Server.

Using SQL Server.

我有一个非常广泛的查询,有很多别名等...

I have a very extensive query, with a lot of aliasing, etc...

有没有办法,只使用 SQL(存储过程很好,但不是 PHP 等),从这个查询中获取所有列名的列表?(我意识到我可能不得不将我的查询嵌入到这个解决方案中,但这很好.只是一个临时措施.)

Is there a way, using just SQL (stored proc is fine, but not PHP, etc), to get a list of all column names from this query? (I realize I will have to probably embed my query inside of this solution but that is fine. Just a temporary measure.)

谢谢!

推荐答案

如果您使用的是 SQL Server 2012 或更高版本,则可以利用 sys.dm_exec_describe_first_result_set

If you're using SQL Server 2012 or later you can take advantage of sys.dm_exec_describe_first_result_set

SELECT name 
FROM 
sys.dm_exec_describe_first_result_set
('Your Query Here', NULL, 0) ;

演示

相关文章