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

  • SEARCH
  • Home
  • 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 8746889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:15:58+00:00 2026-06-13T12:15:58+00:00

I am using this SQL query DECLARE @cols AS NVARCHAR(MAX), @query AS NVARCHAR(MAX) select

  • 0

I am using this SQL query

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT distinct ',' + QUOTENAME(Animal2) 
                    from animals
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT Animal1, ' + @cols + ' from 
             (
                select animal1, animal2, Corelation
                from animals
            ) x
            pivot 
            (
                min(Corelation)
                for animal2 in (' + @cols + ')
            ) p '

execute(@query)

See SQL Fiddle with demo

When I execute the query I get a table as a return.

How can I select from that table? I tried to use SELECT * FROM (*past here the script*) but it did not work. I just need to use the result of the execute(@query) as a table and select from it (to put it in a new table). How can I do it?

Thanks

NOTE: that query was an answer of this SO question

  • 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-13T12:15:59+00:00Added an answer on June 13, 2026 at 12:15 pm

    Use into and a global temporary table – then you don’t have to define the table columns in advance.

    set @query = 'SELECT Animal1, ' + @cols + 
        +' into ##temp '
        +' from 
                 (
                    select animal1, animal2, Corelation
                    from animals
                ) x
                pivot 
                (
                    min(Corelation)
                    for animal2 in (' + @cols + ')
                ) p '
    
    select * from ##temp
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When using this SQL query: Select * from table_name what happens if the name
I have a query: declare @Code nvarchar(100) select @Code=BMW select name from NewCars where
I'm using this SQL query to create an index: $query = CREATE INDEX id_index2
I have this issue: first, I execute a SQL query using Java and then
I would like to turn this query to regular inline sql without using stored
I'm trying to run a query from ADO.NET using SQL Server 2008R2. I'm using
We are trying to get IdValue from sql query. We are using sql server
I am using this code to search data in SQL Server. But the result
I much prefer using this 'embedded' style inserts in a pl/sql block (opposed to
I'm using a SQL Server database in my program with Linq-to-SQL I'm using this

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.