I am trying to write a query that pulls out the names of all of the parameters in my report server. The parameters data is stored as XML in an ntext field so I don’t think I can use xquery on it.
What I initially started doing was making case statements for each parameter name I expect to find but, this is going to be tedious and I don’t think the report developers were totally consistent with parameter names (and in fact that is one of my reasons for wanting the query).
Here is what i have so far, which is pretty simplistic:
SELECT [Path], [Name], CreationDate, ModifiedDate, Parameter,
CASE WHEN Parameter LIKE '%<Name>UserId</Name>%' THEN 'Yes' ELSE 'No' END AS 'UserId',
CASE WHEN Parameter LIKE '%<Name>Country</Name>%' THEN 'Yes' ELSE 'No' END AS 'Country',
CASE WHEN Parameter LIKE '%<Name>Office</Name>%' THEN 'Yes' ELSE 'No' END AS 'Office'
FROM dbo.Catalog
WHERE type IN (2, 4)
AND [path] NOT LIKE '/Enterprise Reports%'
AND [Path] NOT LIKE '/Email Subscription%'
ORDER BY [Path]
What I think I want is something more like this:
SELECT [Path], [Name], CreationDate, ModifiedDate, Parameter.query(/Parameters/Parameter/Name)
FROM dbo.Catalog
WHERE type IN (2, 4)
AND [path] NOT LIKE '/Enterprise Reports%'
AND [Path] NOT LIKE '/Email Subscription%'
ORDER BY [Path]
The problem is, it’s ntext, not xml. Can I just use convert on that?
The other problem is, I don’t know the xquery syntax to pull out the name field of the many parameters within the XML.
The document’s schema is very simple:
<Parameters>
<Parameter>
<Name>Some name</Name>
...some more fields i don't care about...
</Parameter>
<Parameter>
<Name>Another name</Name>
</Parameter>
...more parameters, etc.
</Parameters>
I just want a list of what is in the names.
There can be many nodes and each one has a single node as a child.
UPDATE:
It seems like Nodes() should help me but I am struggling to take the examples on Books Online and a Simple Talk article and apply it to my situation. Here is what I have so far:
SELECT [Path], [Name], CreationDate, ModifiedDate, Parameter,
CASE WHEN Parameter LIKE '%<Name>UserId</Name>%' THEN 'Yes' ELSE 'No' END AS 'UserId',
CASE WHEN Parameter LIKE '%<Name>Country</Name>%' THEN 'Yes' ELSE 'No' END AS 'Country',
CASE WHEN Parameter LIKE '%<Name>Office</Name>%' THEN 'Yes' ELSE 'No' END AS 'Office'
, CAST(Parameter AS XML).nodes('/Parameters/Parameter/Name')
FROM dbo.Catalog
WHERE type IN (2, 4)
AND [path] NOT LIKE '/Enterprise Reports%'
AND [Path] NOT LIKE '/Email Subscription%'
ORDER BY [Path]
The answer to your first question is: yes, it’s as easy as a CONVERT(xml, Parameter). Assuming that it’s well formed XML, it’ll convert just fine
The second question is going to involve the nodes() method of the XML data type. Read all about it: nodes().
Edit:
Now that I’m in a position to test, here’s what I came up with: