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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:46:27+00:00 2026-05-16T16:46:27+00:00

Trying to understand why you can’t use sql_variant for the value type when using

  • 0

Trying to understand why you can’t use sql_variant for the value type when using the XML nodes function within SQL Server? I have a scenario where I will be dynamically parsing some XML input and being able to use the sql_variant would be a nice alternative to having to assign a datatype variable or doing a lookup on sys.columns.

Example:

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[XmlSourceTable]') AND type in (N'U'))
DROP TABLE [dbo].[XmlSourceTable]
GO
CREATE TABLE [dbo].[XmlSourceTable](
    [RecordId] [int] IDENTITY(1,1) NOT NULL,
    [XmlData] [xml] NOT NULL,
PRIMARY KEY CLUSTERED 
(
    [RecordId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

DECLARE @XML XML ='<?xml version="1.0" ?>
<Root>
      <Person>
            <Name>Simon</Name>
            <Age>20</Age>
            <Skills>
                  <Skill>Cooking</Skill>
                  <Skill>Cleaning</Skill>
            </Skills>
      </Person>
      <Person>
            <Name>Peter</Name>
            <Age>21</Age>
            <Skills>
                  <Skill>Ironing</Skill>
            </Skills>
      </Person>
</Root>'

INSERT INTO XmlSourceTable(XmlData)
SELECT @XML

GO

SELECT * FROM XmlSourceTable
GO
SELECT
      pref.value('(Name/text())[1]', 'varchar(50)') as PersonName,
      pref.value('(Age/text())[1]', 'int') as PersonAge,
      pref.query('Skills') as PersonSkills
FROM  
      XmlSourceTable CROSS APPLY
      XmlData.nodes('/Root/Person') AS People(pref)

So Instead of:

SELECT * FROM XmlSourceTable
GO
SELECT
      pref.value('(Name/text())[1]', 'varchar(50)') as PersonName,
      pref.value('(Age/text())[1]', 'int') as PersonAge,
      pref.query('Skills') as PersonSkills
FROM  
      XmlSourceTable CROSS APPLY
      XmlData.nodes('/Root/Person') AS People(pref)

It would be nice to use:

SELECT * FROM XmlSourceTable
GO
SELECT
      pref.value('(Name/text())[1]', 'sql_variant') as PersonName,
      pref.value('(Age/text())[1]', 'sql_variantt') as PersonAge,
      pref.query('Skills') as PersonSkills
FROM  
      XmlSourceTable CROSS APPLY
      XmlData.nodes('/Root/Person') AS People(pref)

But I get this error:
Msg 9500, Level 16, State 1, Line 1
The data type ‘sql_variant)’ used in the VALUE method is invalid.

Anyway to use sql_variant in the nodes function?

Thanks,

S

  • 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-05-16T16:46:28+00:00Added an answer on May 16, 2026 at 4:46 pm

    sql_variant is a meta type that stores in the value the actual type of the data (int, char, float, date etc). The problem is that XML expressions do not have a type: what is the type of an XPath expression result like ('Name/text())[1]? I understand you can argue that if the XML has a schema, one could deduce the xs schema type associated with that node/attribute/element, but I’d say that maybe you could deduce the type and you still left with the vast majority of schema-less XML documents out there in use… This is why the .value() XML method needs a type, so it can coerce the text found in the XML into a proper type.

    One thing worth of notice is that most XML values can be extracted as VARCHAR, which would give the original XML text for that element/attribute. You could then convert the text to the appropriate column. But I think doing what you do right now (look up the column type, build the XPath/type dynamically with the proper type) is better.

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

Sidebar

Related Questions

I'm trying to understand how I can use the local server time to quickly
I am trying to understand why any import can be referenced using the importing
I'm trying to understand type patterns and generic classes in Haskell but can't seem
I'm using CakePHP 2.0 and I'm trying to understand how I can echo dynamic
I have been trying to understand function pointers in C++ so that I can
I'm trying to understand how can I use regular expressions in express js, I
I read this snippet, and I am trying to understand how I can use
I am trying to understand why onsubmit at Javascript is using an anonymous function
I'm trying to understand when can I use the enums in Java. So, let's
I'm trying to understand how can a magnetic link work, as I've read they

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.