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 9000447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:18:45+00:00 2026-06-16T00:18:45+00:00

Is there a better (more efficient) way to write this query? It seems like

  • 0

Is there a better (more efficient) way to write this query? It seems like there should be a way to write it with only one UNION.

Query:

SELECT 'Value1' as PropertyKey, PropertyValue=(SELECT Value1 FROM MyTable WITH (NOLOCK))
UNION
SELECT 'Value2' as PropertyKey, PropertyValue=(SELECT Value2 FROM MyTable WITH (NOLOCK))
UNION
SELECT 'Value3' as PropertyKey, PropertyValue=(SELECT Value3 FROM MyTable WITH (NOLOCK))
UNION
...
SELECT 'Value100' as PropertyKey, PropertyValue=(SELECT Value100 FROM MyTable WITH (NOLOCK))

Ultimately, I need my result set to have 2 columns (PropertyKey, and PropertyValue). The values in the PropertyKey column will be the names of the columns in my table, and the values in the PropertyValue column will be the corresponding values.

Is it possible to write this with only one UNION if I’m always selecting from the same table?

  • 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-06-16T00:18:45+00:00Added an answer on June 16, 2026 at 12:18 am

    If you have access to the UNPIVOT function you can use it the following way:

    select PropertyKey, PropertyValue
    from yourtable
    unpivot
    (
      PropertyValue
      for PropertyKey in (Value1, Value2, Value3) -- list other columns here
    ) unpiv
    

    The key piece to keep in mind with an UNPIVOT is the datatypes must be the same, so you might have to convert the datatypes:

    select PropertyKey, PropertyValue
    from
    (
        select CAST(value1 as varchar(10)), 
             CAST(value1 as varchar(10)), ...
        from yourtable
    ) src
    unpivot
    (
      PropertyValue
      for PropertyKey in (Value1, Value2, Value3) -- list other columns here
    ) unpiv
    

    You can even perform this dynamically, which will get the list of columns to transform at run-time:

    DECLARE @colsUnpivot AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)    
    
    select @colsUnpivot = stuff((select ','+quotename(C.name)
             from sys.columns as C
             where C.object_id = object_id('yourtable')
             for xml path('')), 1, 1, '')
    
    set @query 
      = 'select PropertyKey, PropertyValue
         from yourtable
         unpivot
         (
            PropertyValue
            for PropertyKey in ('+ @colsunpivot +')
         ) u'
    
    exec(@query)
    

    If you are stuck, using your current version then I would alter it slightly and use a UNION ALL instead of the UNION:

    SELECT 'Value1' as PropertyKey, Value1 PropertyValue
    FROM MyTable
    UNION ALL
    SELECT 'Value2' as PropertyKey, Value2 PropertyValue
    FROM MyTable
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a better, more efficient way to write this code? It's a make
Is there a better / more efficient / shorter way to write this SQL
Is there better way to enumerate all photos on the device than this one?
Is there a better way to write this spec? This works but I don't
Is there anything more compact (or otherwise better) than this? <x:Array x:Key=titles Type=System:String> <System:String>Mr.</System:String>
Which way is better for removing float decimals places or is there a more
Is there better way to delete a parameter from a query string in a
Is there a better way to code this? def __contains__(self, e): return e in
Is there a better way to write the following? $(function(){ $('.start-here-notice').fadeIn(1000).animate({left:-155px}, slow) $('.start-here-notice').animate({left:-165px}, slow).delay(5000).fadeOut(1000);
Is there a better way to format data uniformly than to store data as

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.