So, I’ve been searching around and I’ve found things similar to my problem, but I need more help to get a real solution.
I’m trying to construct a query that will return 2 columns of data, the first column should be a list of the column names themselves and the second should be the value of that column.
Visually it would look like this
Column1 Column2
------- -------
columnA value_of_columnA
columnB value_of_columnB
... ...
I’m pretty sure that this is going to require dynamic SQL to achieve, but I have no idea how to even begin creating the query.
Any help is appreciated!
This should work for any table, but in my example I just create a test one. You need to set the table name within @YourTableName. Also, you need to set @YourTableWhere to limit the results to one row, otherwise the output looks strange with multiple rows mixed together.
try this:
OUTPUT:
EDIT
For SQL Server 2000 compatibility, you should be able to replace varchar(max) with varchar(8000) and use this in place of the
SELECT @YourQueryquery from the code above: