I have a column in table which contains XML text, but it is not XML type and its type should not be changed.
I have write the following SQL query to get some information from its
DECLARE @TempXML XML=(SELECT TOP 1 XMLColumn FROM TableOne)
SELECT T.c.query('.').value('(/question/@id)[1]','BIGINT') AS [QuestionID]
,T.c.query('.').value('(/question/@comment)[1]','NVARCHAR(100)') AS [Comment]
,T.c.query('.').value('(/question/answer/@pos)[1]','TINYINT') AS [AnswerPosition]
FROM @TempXML.nodes('/submission/question') T(c)
The problem I have net is that I have to create a view in which for each row the query above will be executed, but I am not able to concatenated the query and the xml conversion.
I have try to create CTE in which to do the conversion but then met some difficulties
in implementing my query.
WITH CTEview(CurrentXML) AS
(
SELECT CAST(XMLColumn AS XML) AS [CurrentXML]
FROM TableOne
)
--My SQL goes here
Using “Cross Apply” to the XML columns from the CTE solve the issue.