我需要输出类似的东西
<Employee EmpID="4">
<FirstName>Rob</FirstName>
<LastName>Walters</LastName>
</Employee>
<Employee EmpID="168">
<FirstName>Rob</FirstName>
<MiddleName>T</MiddleName>
<LastName>Caron</LastName>
</Employee>
但SQL要做到这一点:
SELECT e.EmployeeID AS "@EmpID",
c.FirstName,
c.MiddleName, c.LastName
FROM Employee AS e
INNER JOIN Contact AS c
ON c.ContactID = e.ContactID
WHERE c.FirstName = 'Ross'
FOR XML PATH (''), ROOT ('Employees');
返回此错误:
SQL Server Database Error: Row tag omission (empty row tag name) cannot be used with attribute-centric FOR XML serialization.
参见SQLFiddle:
答案 0 :(得分:1)
最好的方法是使用正确的代码:
SELECT
e.EmployeeID AS "@EmpID",
c.FirstName,
c.MiddleName, c.LastName
FROM Employee AS e
INNER JOIN Contact AS c
ON c.ContactID = e.ContactID
WHERE c.FirstName = 'Ross'
FOR XML PATH ('Employee')
;
与正确的SQLFiddle一样:http://www.sqlfiddle.com/#!6/3a159/8/0