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 inserts NULL values in the last column. I know that this ENTITY-ID column is specified as a string in the schema, but I made it an INT in this table because all of the values are numbers. I also tried making this 3rd column a VARCHAR but it too returned NULL values.
This is the table I created in 2008 SQL Server:
CREATE TABLE HOMEROOM(
HOMEROOM_TEACHER INT,
HOMEROOM_NUMBER VARCHAR(10),
ENTITY_ID INT)
And this is the T-SQL code I am trying to execute. Also, how do I remove this prepare_statment?:
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, '<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
INSERT INTO HOMEROOM (HOMEROOM_TEACHER, HOMEROOM_NUMBER, ENTITY_ID)
SELECT HOMEROOM_TEACHER ,HOMEROOM_NUMBER ,ENTITY_ID
FROM OPENXML (@idoc, '/xml/rs:data/z:row',1)
WITH (
HOMEROOM_TEACHER INT '@c0'
,HOMEROOM_NUMBER VARCHAR(10) '@c1'
,ENTITY_ID INT 'c2'
)
--sp_xml_removedocument @idoc
SELECT * FROM HOMEROOM