I'm trying to select the column headers/names from an EXEC statement in SQL.
For example, if I run the code
SET @ls_SQL = 'EXEC dbo.Generic_Proc ' + @Param
EXEC(@ls_SQL)
and it returns:
|---------------------|------------------|
| ColumnName1 | ColumnName2 |
|---------------------|------------------|
| 12 | 34 |
|---------------------|------------------|
How can I get the strings 'ColumnName1' and 'ColumnName2' into a temporary table? Something like:
|---------------------|------------------|
| Row | Header |
|---------------------|------------------|
| 1 | ColumnName1 |
|---------------------|------------------|
| 2 | ColumnName2 |
|---------------------|------------------|
I tried using sp_describe_first_result_set, but 'Generic_Proc' is using dynamic SQL, so I get an error. The error states that I can explicitly describe the result set, but unfortunately the returned columns will be different depending on the parameter sent to it. Is there any way around this?