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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:10:23+00:00 2026-06-17T10:10:23+00:00

work on sql server .Write one pivotal sql ,bellow is my tables and pivotal

  • 0

work on sql server .Write one pivotal sql ,bellow is my tables and pivotal sql syntax.MY problem is failed to put this pivotal value in Temp table .

-----------------Table-1-------------
CREATE TABLE Table1 (ColId INT,ColName VARCHAR(10))
INSERT INTO Table1 VALUES(1, 'Country')
INSERT INTO Table1 VALUES(2, 'MONTH')
INSERT INTO Table1 VALUES(3, 'Day')



----------------Table-2----------------------------

CREATE TABLE Table2 (tID INT,ColID INT,Txt VARCHAR(10))

INSERT INTO Table2 VALUES (1,1, 'US')
INSERT INTO Table2 VALUES (1,2, 'July')
INSERT INTO Table2 VALUES (1,3, '4')
INSERT INTO Table2 VALUES (2,1, 'US')
INSERT INTO Table2 VALUES (2,2, 'Sep')
INSERT INTO Table2 VALUES (2,3, '11')
INSERT INTO Table2 VALUES (3,1, 'US')
INSERT INTO Table2 VALUES (3,2, 'Dec')
INSERT INTO Table2 VALUES (3,3, '25')


--------------
----------------Pivotal sql----------------------------

DECLARE @query NVARCHAR(4000)
DECLARE @cols NVARCHAR(2000)

SELECT  @cols = STUFF(( SELECT DISTINCT TOP 100 PERCENT
                                '],[' + t2.ColName
                        FROM    Table1 AS t2
                        ORDER BY '],[' + t2.ColName
                        FOR XML PATH('')
                      ), 1, 2, '') + ']'                    

SET @query = N'SELECT tID, '+
@cols +'
FROM
(SELECT  t2.tID
      , t1.ColName
      , t2.Txt 
FROM    Table1 AS t1
        JOIN Table2 AS t2 ON t1.ColId = t2.ColID) p
PIVOT
(
MAX([Txt])
FOR ColName IN
( '+
@cols +' )
) AS pvt
ORDER BY tID;'
EXECUTE(@query)

After execute bellow command want to use this value for rest of work ,so I need to put this command result value in temp table

 EXECUTE(@query)
  • 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-17T10:10:24+00:00Added an answer on June 17, 2026 at 10:10 am

    If you are able to do the rest of your procedure by working with EXECUTE(@Querys) you coud do something like that

    Declare @TabName Varchar(40)
    
    Select @TabName='##'+Replace(CAST(NEWID() as Varchar(40)),'-','')
    
    DECLARE @query NVARCHAR(4000)
    DECLARE @cols NVARCHAR(2000)
    
    SELECT  @cols = STUFF(( SELECT DISTINCT TOP 100 PERCENT
                                    '],[' + t2.ColName
                            FROM    Table1 AS t2
                            ORDER BY '],[' + t2.ColName
                            FOR XML PATH('')
                          ), 1, 2, '') + ']'                    
    
    SET @query = N'SELECT tID, '+
    @cols +' into ' +@TabName + '
    FROM
    (SELECT  t2.tID
          , t1.ColName
          , t2.Txt 
    FROM    Table1 AS t1
            JOIN Table2 AS t2 ON t1.ColId = t2.ColID) p
    PIVOT
    (
    MAX([Txt])
    FOR ColName IN
    ( '+
    @cols +' )
    ) AS pvt
    ORDER BY tID;'
    
    Print @Query
    EXECUTE(@query)
    
    Select @query = 'Select * from ' + @Tabname
    EXECUTE(@query)
    
    Select @query = 'Drop Table ' + @Tabname
    EXECUTE(@query)
    

    If you dont want to do that, you could use a “hard” ##temptable, but you would not be able using this save withing a multiuser environment.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The Problem I'm trying to write a stored procedure in SQL Server to find
work on SQL Server 2000. want to Automated Email Notifications using SQL Server Job
I am having problems getting the 'ByAccount' aggregation to work in SQL Server Analysis
My work is running SQL Server 2008 and I spend a lot of time
How does INSERT, UPDATE & DELETE work on a SQL Server table partition? Technical
I am using MS SQL Server 2005 at work to build a database. I
I'm using Microsoft SQL Server Management Studio to work with a database. I'm only
At work, we have several applications with databases in a centralized SQL server. Whenever
I work for a ISV. Our product can use both SQL Server and Oracle
The setup: Winform/ASP.NET MVC projects. Learning NHibernate SQL-Server driven apps I work with clients

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.