views:

387

answers:

2

I'm loading an XML in SQL using OpenXML while declaring the variable the max i can go up to is 8000 chars :

DECLARE @xml_text varchar(8000)

Since text, ntext is not allowed to be used with openXML what other alternatives do i have to load the entire XML (over 20000 chars) in SQL ?

+3  A: 

You should be able to use varchar(max) (SQL 2005 and higher)

DECLARE @idoc int
DECLARE @doc varchar(max)
SET @doc = '
<myxml>
<node nodeid="1" nodevalue="value 1">

</node>

</myxml>'
EXEC sp_xml_preparedocument @idoc OUTPUT, @doc
SELECT
    *
FROM
    OPENXML (@idoc, '/myxml/node',1) WITH ( nodeid varchar(10), nodevalue varchar(20) )
Jason Heine
ugh, why the use of sp_xml_prepareddocment here? Don't forget to clean that up with sp_xml_removedocument! The answer by CAbbott is a better one.
ScottE
Because at the time (over a year ago), I was not that familiar with the XML data type in SQL. Yes, CAbbott has the better solution.
Jason Heine
+3  A: 

If you're using SQL 2005 or better you could use the XML data type itself. This way you would be able to avoid using OPENXML:

DECLARE @XDoc XML
SET @XDoc = '<Customer>
                 <FirstName>Fred</FirstName>
                 <LastName>Flinstone</LastName>
             </Customer>
             <Customer>
                 <FirstName>Barney</FirstName>
                 <LastName>Rubble</LastName>
             </Customer>'

SELECT 
    Tbl.Col.value('FirstName[1]', 'VARCHAR(MAX)'),
    Tbl.Col.value('LastName[1]', 'VARCHAR(MAX)')
FROM @XDoc.nodes('/Customer') Tbl(Col)
CAbbott
This is the correct answer. If your doing XML within SQL Server 2005 and you're not using the XML data type. That is like using a hammer to pound in a screw. Sure it works but its not the right tool for the job.
DBAndrew