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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:33:44+00:00 2026-05-16T02:33:44+00:00

I am a newbie to OPENXML. But I am trying to load a .XML

  • 0

I am a newbie to OPENXML. But I am trying to load a .XML file into a SQL table that I created for this. I do not receive any errors with this code, but it doesn’t insert any records either. This is the table I created in 2008 SQL Server:

CREATE TABLE HOMEROOM(
HOMEROOM_TEACHER INT,
HOMEROOM_NUMBER INT,
ENTITY_ID INT)

And this is the T-SQL code I am trying to execute:

DECLARE @idoc int
DECLARE @xmlDocument varchar(MAX)
DECLARE @Status INT

SET @xmlDocument ='
<xml xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema">
  <s:Schema id="RowsetSchema">
    <s:ElementType name="row" content="eltOnly">
      <s:AttributeType name="c0" rs:name="HOMEROOM-TEACHER" rs:number="1" rs:nullable="true">
        <s:datatype dt:type="int" dt:maxLength="4" rs:precision="10" rs:fixedlength="true" />
      </s:AttributeType>
      <s:AttributeType name="c1" rs:name="HOMEROOM-NUMBER" rs:number="2">
        <s:datatype dt:type="string" rs:dbtype="str" dt:maxLength="10" rs:maybenull="false" />
      </s:AttributeType>
      <s:AttributeType name="c2" rs:name="ENTITY-ID" rs:number="3">
        <s:datatype dt:type="string" rs:dbtype="str" dt:maxLength="10" rs:maybenull="false" />
      </s:AttributeType>
      <s:extends type="rs:rowbase" />
    </s:ElementType>
  </s:Schema>
  <rs:data>
    <z:row c0="22943" c1="101" c2="055" />
    <z:row c0="22929" c1="102" c2="055" />
    <z:row c0="22854" c1="103" c2="055" />
    <z:row c0="22908" c1="104" c2="055" />
    <z:row c0="22881" c1="105" c2="055" />
<z:row c0="22926" c1="Gym2" c2="055" />
<z:row c0="22935" c1="Gym3" c2="055" />
  </rs:data>
</xml>
'
EXEC @Status = sp_xml_preparedocument @idoc OUTPUT, @xmlDocument
SELECT 'sp_xml_preparedocument status=',@Status

select *
FROM   OPENXML (@idoc, '/xml/',1)
         WITH    (
            HOMEROOM_TEACHER          INT    '@C0'
            ,HOMEROOM_NUMBER          VARCHAR(10) '@C1'
            ,ENTITY_ID          VARCHAR(10) 'C2'
                )

--sp_xml_removedocument @idoc

SELECT * FROM HOMEROOM

But after I execute this, I get 0 rows added to HOMEROOM. Any suggestions for how to make this work?

When I execute above, I get error:
(1 row(s) affected)
Msg 245, Level 16, State 1, Line 627
Conversion failed when converting the nvarchar value ‘Gym2’ to data type int.

  • 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-16T02:33:45+00:00Added an answer on May 16, 2026 at 2:33 am

    Try this:

    EXEC @Status = sp_xml_preparedocument @idoc OUTPUT, 
    @xmlDocument, '<xml xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" 
           xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" 
           xmlns:rs="urn:schemas-microsoft-com:rowset" 
           xmlns:z="#RowsetSchema"/>' 
    SELECT 'sp_xml_preparedocument status=',@Status 
    
    SELECT * 
    FROM OPENXML (@idoc, '/xml/rs:data/z:row',1) 
    WITH ( 
       HOMEROOM_TEACHER   INT    '@c0' 
      ,HOMEROOM_NUMBER    INT    '@c1' 
      ,ENTITY_ID          INT    '@c2' 
    ) 
    

    I did a few things:

    1. Added the namespace declaration as
      the third parameter to
      sp_xml_preparedocument.
    2. Changed the xpath section from ‘/xml/’ to ‘/xml/rs:data/z:row’ to specify the correct position and namespaces in the XML document
    3. Changed the @C variables to lower case (@c)

    Results were:

    HOMEROOM_TEACHER HOMEROOM_NUMBER ENTITY_ID
    ---------------- --------------- -----------
    22943            101             55
    22929            102             55
    22854            103             55
    22908            104             55
    22881            105             55
    

    FYI, information about using OPENXML with namespaces can be found here.

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

Sidebar

Related Questions

Newbie SQL question here --> I've got an Occurrences table that contains a row
Newbie question: I just installed VisualSVN Server and created a repository. I noticed that
Newbie question. I have Django models that look like this: class Video(models.Model): uploaded_by =
Newbie question since I'm not up to speed using maven at all. I'm trying
Newbie C++ programmer here. I'm trying to write a command line application that takes
Newbie to C++ but I am a Java programmer. Trying to learn C++ now.
Newbie :( Tweaking my way through tutorials but 36 hours can't figure this one
Newbie Warning I have a simple but vexing problem trying to disable an NSButton.
newbie here. I am trying to learn OOD/OOP and read on the net that
Newbie question. I have a NSMutableArray that holds multiple objects (objects that stores Bezier

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.