SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


T- SQL Query


T- SQL Query

Author
Message
greeshatu
greeshatu
SSC-Enthusiastic
SSC-Enthusiastic (185 reputation)SSC-Enthusiastic (185 reputation)SSC-Enthusiastic (185 reputation)SSC-Enthusiastic (185 reputation)SSC-Enthusiastic (185 reputation)SSC-Enthusiastic (185 reputation)SSC-Enthusiastic (185 reputation)SSC-Enthusiastic (185 reputation)

Group: General Forum Members
Points: 185 Visits: 184
Hi


I have a Table 'A' where column X is of type XML.
Following is Column X value


<qsdsBigGrinetails Xmlns:qsds="http://www.abc.com/Details">
<Category>
<Employee EmployeeID="01">
<Name>
<Title>Mr</Title>
<Forenames>XXX</Forenames>
<Surname>YYY</Surname>
</Name>
</Employee>

<Employee EmployeeID="02">
<Name>
<Title>Mr</Title>
<Forenames>MMM</Forenames>
<Surname>ZZZ</Surname>
</Name>
</Employee>

<Employee EmployeeID="03">
<Name>
<Title>Mr</Title>
<Forenames>Caron</Forenames>
<Surname>Cobb</Surname>
</Name>
</Employee>
</Category>
</qsdsBigGrinetails>





I need to get number of employees of above XML data Using T-SQL.

This is sample XML for a row of column X . Employee count changes per row.

Please help me how to write a T-sql Query to get no of employees in sample XML provided.
gbritton1
gbritton1
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1341 Visits: 879
This worked for me:


declare @xml as xml = '

<Category>
<Employee EmployeeID="01">
<Name>
<Title>Mr</Title>
<Forenames>XXX</Forenames>
<Surname>YYY</Surname>
</Name>
</Employee>

<Employee EmployeeID="02">
<Name>
<Title>Mr</Title>
<Forenames>MMM</Forenames>
<Surname>ZZZ</Surname>
</Name>
</Employee>

<Employee EmployeeID="03">
<Name>
<Title>Mr</Title>
<Forenames>Caron</Forenames>
<Surname>Cobb</Surname>
</Name>
</Employee>
</Category>
'
select sum(cast(employee.exist('@EmployeeID') as int))
from @xml.nodes('/Category/Employee') xmlnode(employee)


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search