开发者

Dealing with XML datatype(SQL SERVER 2005)

I have a table having 2 columns EmployeeId (int) and EmployeeDetails(XMl type)

EmployeeId EmployeeDetails

1 <Employee><Employ开发者_如何学JAVAeeDetails><EmployeeName> Priyanka </EmployeeName><Age> 24 </Age><Address> Argentina</Address></EmployeeDetails></Employee>

2 <Employee><EmployeeDetails><EmployeeName> Sarkar </EmployeeName><Age> 28 </Age><Address> Australia</Address></EmployeeDetails></Employee>

I need to have the output like

EmployeeId EmpName Age Address


1 Priyanka 24 Argentina

2 Sarkar 28 Australia

Please help as I am new to xml


Books Online is a bit indecipherable on the subject of XML; you may want to try this site for help: http://blogs.msdn.com/mrorke/archive/2005/06/01/423965.aspx Here's a snippet of code to help you along.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜