Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 430799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:54:40+00:00 2026-05-12T19:54:40+00:00

So, I’ve been searching around and I’ve found things similar to my problem, but

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T19:54:40+00:00Added an answer on May 12, 2026 at 7:54 pm

    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:

    BEGIN TRY
    CREATE TABLE YourTestTable
    (RowID       int primary key not null identity(1,1)
    ,col1        int null
    ,col2        varchar(30)
    ,col3        varchar(20)
    ,col4        money
    ,StatusValue char(1)
    ,xyz_123     int
    )
    INSERT INTO YourTestTable (col1,col2,col3,col4,StatusValue,xyz_123) VALUES (1234,'wow wee!','this is a long test!',1234.56,'A',98765)
    INSERT INTO YourTestTable (col1,col2,col3,col4,StatusValue,xyz_123) VALUES (543,'oh no!','short test',0,'I',12)
    
    END TRY BEGIN CATCH END CATCH
    
    select * from YourTestTable
    
    
    DECLARE @YourTableName   varchar(1000)
    DECLARE @YourTableWhere  varchar(1000)
    DECLARE @YourQuery       varchar(max)
    
    SET @YourTableName='YourTestTable'
    set @YourTableWhere='y.RowID=1'
    
    SELECT
        @YourQuery = STUFF(
                           (SELECT
                                ' UNION '
                                + 'SELECT '''+COLUMN_NAME+''', CONVERT(varchar(max),'+COLUMN_NAME+') FROM '+@YourTableName+' y'+ISNULL('  WHERE '+@YourTableWhere,'')
                                FROM INFORMATION_SCHEMA.COLUMNS
                                WHERE table_name = @YourTableName
                                FOR XML PATH('')
                           ), 1, 7, ''
                          )
    
    PRINT @YourQuery  
    
    EXEC (@YourQuery)
    

    OUTPUT:

    RowID       col1        col2                           col3                 col4                  StatusValue xyz_123
    ----------- ----------- ------------------------------ -------------------- --------------------- ----------- -----------
    1           1234        wow wee!                       this is a long test! 1234.56               A           98765
    2           543         oh no!                         short test           0.00                  I           12
    
    SELECT 'RowID', CONVERT(varchar(max),RowID) FROM YourTestTable y  WHERE y.RowID=1 UNION SELECT 'col1', CONVERT(varchar(max),col1) FROM YourTestTable y  WHERE y.RowID=1 UNION SELECT 'col2', CONVERT(varchar(max),col2) FROM YourTestTable y  WHERE y.RowID=1 UNION SELECT 'col3', CONVERT(varchar(max),col3) FROM YourTestTable y  WHERE y.RowID=1 UNION SELECT 'col4', CONVERT(varchar(max),col4) FROM YourTestTable y  WHERE y.RowID=1 UNION SELECT 'StatusValue', CONVERT(varchar(max),StatusValue) FROM YourTestTable y  WHERE y.RowID=1 UNION SELECT 'xyz_123', CONVERT(varchar(max),xyz_123) FROM YourTestTable y  WHERE y.RowID=1
    
    ----------- ------------------------
    col1        1234
    col2        wow wee!
    col3        this is a long test!
    col4        1234.56
    RowID       1
    StatusValue A
    xyz_123     98765
    

    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 @YourQuery query from the code above:

    SELECT
        @YourQuery=ISNULL(@YourQuery+' UNION ','')
            + 'SELECT '''+COLUMN_NAME+''', CONVERT(varchar(max),'+COLUMN_NAME+') FROM '+@YourTableName+' y'+ISNULL('  WHERE '+@YourTableWhere,'')
        FROM INFORMATION_SCHEMA.COLUMNS
        WHERE table_name = @YourTableName
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my XML file chapters tag has more chapter tag.i need to display chapters
I have been unable to fix a problem with Java Unicode and encoding. The
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I need to clean up various Word 'smart' characters in user input, including but
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.