I want to write T-SQL code that combines the use of the XML exists() function with namespaces and an IF-ELSE construct.
From this thread I know that WITH XMLNAMESPACES is used to use namespaces. I also know that the exist() function is used to determine if a node is present in an XML variable.
Until now, I used exist() as follows:
IF ((@MyXML.exist('someNode')) = 0)
BEGIN
-- Do Something
END
ELSE
BEGIN
-- Do Something else
END
However I don't know how to combine this, if I need to use namespaces. I tried as follows, but get an error.
;WITH XMLNAMESPACES('http://www.sdmx.org/resources/sdmxml/schemas/v2_1/message' AS ns)
IF ((@MyXML.exist('ns:someNode')) = 0)
BEGIN
-- Do Something
END
ELSE
BEGIN
-- Do Something else
END
I get the following error:
Incorrect syntax near the keyword 'WITH'.
Hence, my question is: how can I use a T-SQL If-Else construct in combination with XML namespaces and the XML exist() function?