How do I overcome OpenXML's 8000 character limit?
-
06-09-2019 - |
Question
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 ?
Solution
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) )
OTHER TIPS
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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow