处理XML数据类型(SQL SERVER 2005)

时间:2009-10-22 03:53:00

标签: sql sql-server xml sql-server-2005 tsql

我有一个包含2列EmployeeId(int)和EmployeeDetails(XMl类型)的表

EmployeeId EmployeeDetails

1< Employee>< EmployeeDetails>< EmployeeName> Priyanka< / EmployeeName>< Age> 24< / Age><地址>阿根廷< /地址>< / EmployeeDetails>< /雇员>

2< Employee>< EmployeeDetails>< EmployeeName> Sarkar< / EmployeeName>< Age> 28< / Age><地址>澳大利亚和LT; /地址>< / EmployeeDetails>< /雇员>

我需要输出

EmployeeId EmpName年龄地址


1 Priyanka 24 Argentina

2 Sarkar 28 Australia

请帮助我,因为我是xml的新手

1 个答案:

答案 0 :(得分:5)

联机丛书在XML方面有点难以理解;您可以尝试使用此网站寻求帮助:http://blogs.msdn.com/mrorke/archive/2005/06/01/423965.aspx这里有一段代码可以帮助您。

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>'
        )


SELECT  EmployeeID ,
        EmployeeName = m.value('EmployeeName[1]', 'varchar(50)') ,
        Age = m.value('Age[1]', 'int') ,
        Address = m.value('Address[1]', 'varchar(100)')
FROM    @Employees e
        CROSS APPLY EmployeeDetails.nodes('/Employee/EmployeeDetails') AS e1 ( m )