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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:02:53+00:00 2026-06-15T21:02:53+00:00

I am trying to run an UNPIVOT statement from a stored procedure, on a

  • 0

I am trying to run an UNPIVOT statement from a stored procedure, on a table in a different database. I can run the UNPIVOT fine locally, but when I run it on a different database/server I get the following error.

The type of column "XXX" conflicts with the type of other columns specified in the UNPIVOT list.

Here is a slimmed down version of my UNPIVOT.

CREATE TABLE temp (
  id nvarchar(100),
  F1 nvarchar(100),
  F2 nvarchar(100),
  F3 decimal(5,0)
)

INSERT INTO temp VALUES(N'100', N'111', N'AAA', 123)
INSERT INTO temp VALUES(N'100', N'222', N'BBB', 456)
INSERT INTO temp VALUES(N'100', N'333', N'CCC', 789) 
INSERT INTO temp VALUES(N'100', N'444', N'DDD', 012)
INSERT INTO temp VALUES(N'100', N'555', N'EEE', 345)

SELECT * FROM (   SELECT
    ID,
    CAST(F1 AS NVARCHAR(MAX)) AS F1,--used in order to normalize the data
    CAST(F2 AS NVARCHAR(MAX)) AS F2,--used in order to normalize the data
    CAST(F3 AS NVARCHAR(MAX)) AS F3--used in order to normalize the data
FROM
    TEMP   ) AS T UNPIVOT(FieldValue for FieldName in (   F1, F2, F3   )) AS UNPVT

SQL Fiddle

I did notice that this error occurs when the table I am UNPIVOTing, has DECIMAL() field types.

As always, any help is appreciated.


Update: It appears that creating a view on the remote server will allow for the CAST to occur properly. But why? I also tried using cte, but that did not work. I wanted to avoid creating views. Hopefully that is not the only solution.

  • 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-15T21:02:54+00:00Added an answer on June 15, 2026 at 9:02 pm

    The error

    The type of column "XXX" conflicts with the type of other columns specified in the UNPIVOT list.

    is very obvious and one of the better messages. It tells you that your UNPIVOT list contains columns of different types. Here’s an example that causes such an error: SQL Fiddle

    SELECT * FROM (
      SELECT
        ID,
        CAST(F1 AS NVARCHAR(MAX)) AS F1,
        CAST(F2 AS NVARCHAR(MAX)) AS F2,
        F3  -- note F3 is a decimal column containing decimal values
      FROM
        TEMP
      ) AS T
    UNPIVOT(FieldValue for FieldName in (
      F1, F2, F3  -- F3 type is different from F1/F2
      )) AS UNPVT
    

    On a local query, given your query as-written in your question SQL Fiddle, it cannot possibly elicit this error since you have already CAST all the columns as NVARCHAR(MAX).

    However, on a remote query, it appears that SQL Server fails the query at compile time. It seems to pre-validate the pivot columns prior to CAST – seemingly because it may be possible to perform the CAST locally and at a different point in the execution flow. The workaround to this appears to be creating a VIEW that will resolve the CASTs on the remote server.

    Workaround

    You can force the query to resolve remotely (tested on 2005 as the remote server as minimum) using FOR XML, which has the benefit that the values will be extracted back using nvarchar(max) anyway for conformity.

    SELECT *
    FROM (
        SELECT n.value('ID[1]','nvarchar(max)') ID
              ,n.value('F1[1]','nvarchar(max)') F1
              ,n.value('F2[1]','nvarchar(max)') F2
              ,n.value('F3[1]','nvarchar(max)') F3
        FROM (   
            SELECT (
                SELECT
                    ID,
                    F1,
                    F2,
                    F3
                FROM
                    [akltrsvrdb\sqllatin].test1.dbo.temp
                FOR XML PATH('a'), type
            ) XmlPacket
        ) x
        cross apply XmlPacket.nodes('/a') n(n)
    ) T UNPIVOT(FieldValue for FieldName in (   F1, F2, F3   )) AS UNPVT
    

    SQLKiwi found that using sql_variant as the CAST target makes the remote query work, as shown in the chat transcript here.

    SELECT
        *
    FROM
        (
            SELECT
                [id],
                CONVERT(sql_variant, [F1]) as F1,
                CONVERT(sql_variant, [F2]) as F2,
                CONVERT(sql_variant, [F3]) as F3,
                CONVERT(sql_variant, CAST([D20] as nvarchar(4000))) as F20--in case the source field is text (Make sure that 4000 is enough characters!)
            FROM
                [server].[database].[scheme].[table]
        ) as t
    UNPIVOT(FieldValue for FieldName in ([F1],[F2],[F3],[D20])) as unpvt
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to run some sql in a pl/sql procedure. Select field from schema.view; I
I am trying run a batch file from my java codes, but unfortunately I
I'm trying run this procedure with generic parameters. If I can't delete because some
I am trying run a program from a qmake .pro file which modifies the
Trying to run Jison unit tests, but the command fails. How do I fix
Trying to run the reportviewer with passing the parameter but receive error: Validation of
Im trying to run launch4j as a build task on macosx but im getting
When trying to run my application on the emulator I get an error from
I wrote this C program for Win32/c compiler but while i'm trying run this
trying to run the following statement in sql mgmt studio declare @rick as decimal(13,3)

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.