views:

54

answers:

1

Hi

I am looking at this tutorial

and it confuses me as I don't get the SP

CREATE PROCEDURE [dbo].[spTEST_InsertXMLTEST_TEST](@UpdatedProdData nText)
AS 
 DECLARE @hDoc int   

 exec sp_xml_preparedocument @hDoc OUTPUT,@UpdatedProdData 

 INSERT INTO TBL_TEST_TEST(NAME)
 SELECT XMLProdTable.NAME
    FROM OPENXML(@hDoc, 'ArrayOfTBL_TEST_TEST/TBL_TEST_TEST', 2)   
       WITH (
                ID Int,                 
                NAME varchar(100)
            ) XMLProdTable

EXEC sp_xml_removedocument @hDoc

First I am using SQL 2005 and do I need to install something on the server to get OPENXML to work? Next I don't get what these statements do

// not sure what @hDoc is for and why it is an int
 DECLARE @hDoc int   

 // don't get what this is and where the output is.
 exec sp_xml_preparedocument @hDoc OUTPUT,@UpdatedProdData 

// don't get why it is "XMLProdTable" and if it always has to be like this
 SELECT XMLProdTable.NAME

// pretty muct don't get anything what is happening after OPENXML
 FROM OPENXML(@hDoc, 'ArrayOfTBL_TEST_TEST/TBL_TEST_TEST', 2)   
           WITH (
                    ID Int,                 
                    NAME varchar(100)
                ) XMLProdTable

// Don't know what this is really executing
EXEC sp_xml_removedocument @hDoc

Thanks

A: 

// not sure what @hDoc is for and why it is an int DECLARE @hDoc int

That will be used in the next call, it is an output parameter

// don't get what this is and where the output is. exec sp_xml_preparedocument @hDoc OUTPUT,@UpdatedProdData

It is calling the sp_xml_preparedocument, output just means it is an output parameter, @hDoc will be populated with the value in the proc

// don't get why it is "XMLProdTable" and if it always has to be like this SELECT XMLProdTable.NAME

XMLProdTable is the alias used in the next part, the CTE is named XMLProdTable

// pretty muct don't get anything what is happening after OPENXML FROM OPENXML(@hDoc, 'ArrayOfTBL_TEST_TEST/TBL_TEST_TEST', 2)
WITH ( ID Int,
NAME varchar(100) ) XMLProdTable --alias

It populates the CTE with values from the XML document under the ArrayOfTBL_TEST_TEST/TBL_TEST_TEST tag

// Don't know what this is really executing EXEC sp_xml_removedocument @hDoc

This is releasing the document from memory

Take a look at the samples here http://msdn.microsoft.com/en-us/library/ms186918.aspx

SQLMenace
So what is this ArrayOfTBL_TEST_TEST/TBL_TEST_TEST tag? Like where do I change it? How does it get set? How about SELECT XMLProdTable.NAME, do I need to put one for each column in my table like XMLProdTable.NAME, XMLProdTable.QTY, etc?
chobo2
And all that stuff really isn't needed, if you use the SQL Server 2005 native XQuery support (@xml.query(), @xml.nodes(), @xml.value() etc.)
marc_s