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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:55:19+00:00 2026-06-10T00:55:19+00:00

I have a table variable, called @t , it is an instance of the

  • 0

I have a table variable, called @t, it is an instance of the specsAndModel type I defined here:

CREATE TYPE specsAndModel AS TABLE
(
    specName VARCHAR(50)
    ,specVal VARCHAR(50)
)

Doing a select specName,specVal from @t gives data of the form:

specName              | specVal
--------------------------------
[modelNumber]         | F00-B4R
[Internal Switch(es)] | 2.00000
[Number of Ports]     | 1.00000
[Color Insulator]     | Yellow

I inserted the first row so that when I transpose the table, there will be a modelNumber field. I intend to join the transposed table to another on that field.

In order to account for a different number of records for different models, I constructed the pivot statement using string concatenation:

DECLARE @cols NVARCHAR(MAX);
DECLARE @query NVARCHAR(MAX);
SELECT @cols = COALESCE(@cols + ',' + specName, specName) FROM @t;

SET @query = N'SELECT ' + @cols 
+ N' FROM @var src' 
+ N' PIVOT (MAX(specVal) FOR specName in (' + @cols + N')) pvt;';

The query string looks like this:

SELECT [modelNumber],[Internal Switch(es)],[Number of Ports],[Color Insulator] 
FROM @var src 
PIVOT 
(
  MAX(specVal) 
  FOR specName in ([modelNumber],[Internal Switch(es)],[Number of Ports],[Color Insulator])
) pvt;

Finally, I use sp_executesql to pass in the @t variable, then execute the @query string:

EXEC sp_executesql @query, N'@var specsAndModel readonly', @t

The result is this:

[modelNumber] | [Internal Switch(es)] | [Number of Ports] | [Color Insulator]
-----------------------------------------------------------------------------
NULL          | NULL                  | NULL              | NULL

I get the null values for all models, I constructed a toy example of this pivot here on sqlfiddle and it works as expected. I am not sure what I’m doing wrong.

How can I transpose this table without getting null values?

  • 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-10T00:55:20+00:00Added an answer on June 10, 2026 at 12:55 am

    A couple of things to fix:

    First, remove the [ from around your specName values on your INSERT statement.

    Second, change the way you get your columns to this. This will add the [ around the specName values:

    DECLARE @cols NVARCHAR(MAX);
    DECLARE @query NVARCHAR(MAX);
    select @cols = STUFF((SELECT distinct ',' + QUOTENAME(specName) 
                        from @t
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    

    If you do that, then your script will work:

    DECLARE @cols NVARCHAR(MAX);
    DECLARE @query NVARCHAR(MAX);
    select @cols = STUFF((SELECT distinct ',' + QUOTENAME(specName) 
                        from @t
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    SET @query = N'SELECT ' + @cols 
    + N' FROM @var src' 
    + N' PIVOT (MAX(specVal) FOR specName in (' + @cols + N')) pvt;';
    
    EXEC sp_executesql @query, N'@var specsAndModel readonly', @t
    

    See SQL Fiddle with Demo

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

Sidebar

Related Questions

I have defined a table type PL/SQL variable and added some data there. create
I have a php variable called $VrHistorySlider , defined like this: $VrHistorySlider.='<table border=0><tr><td>(oldest)</td><td style=padding:0px
I have a table (table variable in-fact) that holds several thousand (50k approx) rows
I have the following table variable in SQL Server 2005: DECLARE @results TABLE (id
I have a classified_id variable which matches one document in a MySql table. I
I have a MySQL database table called submission with a field called points with
I have a variable called $date with a date format like 2012-01-30. On the
I have a Rails app which has 2 databases. Legacy DB with table called
I have a table called replies and it has a column named topic_id .
I have a mysql table with a field called POSTIDS POSTIDS values: 1,2,6,7,15,24,25,63,78,98,120 I

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.