Sql-server – Capturing Column Names in Dynamic SQL Statement

sql serversql-server-2008-r2sql-server-2016

I'm in the process of updating some procs made for SQL 2008R2 to 2016 that are using a dynamic result set.

Assuming there is a procedure that always has exactly one result set, but the result set has a dynamic column return each time, is there a way to capture the column names at runtime in tsql without doing something janky?
I'm not worried about data types, just column names.

Thanks!

Best Answer

There's no TSQL api to examine the resultset shape of a procedure that returns multiple different resultset shapes. For normal procs you can use sp_describe_first_result_set.

If you can afford to actually run the procedure and examine the results, the least-janky solution is to use SQL CLR. In CLR you get a SqlDataReader that you can use to examine the resultset shape.