0

给定这样的表结构:

DECLARE @Employees TABLE(EmployeeID INT ,EmployeeDetails XML)

INSERT INTO @Employees( EmployeeID ,EmployeeDetails )
VALUES ( 1 ,'<Employee><EmployeeDetails><EmployeeName> Priyanka </EmployeeName><Age> 24 </Age><Address> Argentina</Address></EmployeeDetails></Employee>')

INSERT INTO @Employees
( EmployeeID ,EmployeeDetails )
VALUES ( 2 ,'<Employee><EmployeeDetails><EmployeeName> Sarkar </EmployeeName><Age> 28 </Age><Address> Australia</Address></EmployeeDetails></Employee>')

如何创建查询以获取 EmployeeDetails 标记下的所有标记名称(不是值)的列表?(假设所有记录的结构都相同)。

因此,期望的结果是:

EmployeeName
Age
Address

山姆

4

1 回答 1

4

尝试这样的事情:

SELECT
    DISTINCT T.C.value('local-name(.)[1]', 'varchar(100)')
FROM @Employees
CROSS APPLY EmployeeDetails.nodes('Employee/EmployeeDetails/*') AS T(C)
于 2012-07-20T14:22:23.217 回答