-
Notifications
You must be signed in to change notification settings - Fork 13
SQL XML related query
Tako Lee edited this page Feb 22, 2014
·
1 revision
When setting up your formatting standards, you should also take into account XML-related queries. For instance, the following select statement uses the XML query() method to retrieve the job candidate’s name:
SELECT JobCandidateID, EmployeeID, Resume.query(
'declare namespace ns=
"http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/Resume";
data(/ns:Resume/ns:Name/ns:Name.Last)') AS CandidateName
FROM HumanResources.JobCandidate
WHERE EmployeeID IS NOT NULL
Notice how the method’s argument is set off from the rest of the SELECT list. Also notice that the namespace wraps to a second line. Another approach you can take is to isolate the method’s argument even further:
SELECT JobCandidateID, EmployeeID,
Resume.query
(
‘declare namespace ns=
“http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/Resume”;
data(/ns:Resume/ns:Name/ns:Name.Last)’
) AS CandidateName
FROM HumanResources.JobCandidate
WHERE EmployeeID IS NOT NULL
XML can be tricky when trying to implement formatting standards because the XQuery elements can become quite long, particularly when the namespace is being referenced, as in the examples above.
Reference: https://www.simple-talk.com/sql/t-sql-programming/transact-sql-formatting-standards-(coding-styles)/