Almost certainly it's your "xml": '12345' isn't a valid XML document, so this code:
@p_CarId NVARCHAR(MAX) = '12345'
...
EXEC sp_xml_preparedocument @hdoc OUTPUT , @p_CarId
doesn't fit the parameter requirements unless your call to the SP provides valid XML file content.
So check that: look at exactly what you pass to your SP, and check it's XML content carefully. I suspect you don't pass a value at all, or mispelled the parameter name when you tried.
Quote:
Hi sir, thank you for your explanation. Due to some circumstances, i can't debug my codes on the actual input parameter. But based on your suggestion, i have altered my question and tried to execute it and the error is gone now albeit with no results returned. Could you please have a look on my updated question?
If I minimise your updated code:
DECLARE @xmlContent AS NVARCHAR(MAX)
SET @xmlContent = '
<ROOT>
<Vehicles CarId="12345">
</Vehicles>
</ROOT>
';
DECLARE
@hdoc AS INT;
EXEC sp_xml_preparedocument @hdoc OUTPUT , @xmlContent
DECLARE @Car TABLE
(
CarId BIGINT
)
INSERT INTO
@Car
SELECT
*
FROM
OPENXML(@hdoc,'/ROOT/Vehicles/CarId',1) WITH(CarId BIGINT)
SELECT
c.CarId
And run it against SQL Server 2012 SP1, I get no error. I get no output either, but I don't get an error.
Changing to this:
OPENXML(@hdoc,'/ROOT/Vehicles',1) WITH(CarId BIGINT)
Gives me a single row containing "12345" as I'd expect.
So what happens when you "minify" your input to match mine?