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.