Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Instance numbering of a node

 
   Database Help (Home) -> XML RSS
Next:  Text field as hyperlink! - FM pro 10.0  
Author Message
pi

External


Since: May 02, 2011
Posts: 1



(Msg. 1) Posted: Mon May 02, 2011 2:25 pm
Post subject: Instance numbering of a node
Archived from groups: microsoft>public>sqlserver>xml (more info?)

Hi

In the follwing query, I use a very bad way to compute the last field
(Position)

using 1 + (ROW_NUMBER() OVER (ORDER BY att) - 1) /3 as Position

to find the instance number of the nodes/attrbutes.

The final result is what I expect , but Only if I have 3 attributes by
nodes - (which coresspond to 3 field in my MINIRES Table)

Does anybody knows if there is a better way (and more elegant -) to get
this numbering of nodes ?


declare @xml XML
/*
select @xml = (

select
rtrim(code_reseau_minitel) AS code_reseau_minitel ,
service,
rtrim(nom_facture) AS nom_facture
from
LP.dbo.Minires AS Minires where code_reseau_minitel = 'CAPA'
for XML AUTO, root('toto')
)
*/
select @xml = -- replacement for above query
'<toto>
<Minires code_reseau_minitel="CAPA" service="CPRDEVISES"
nom_facture="CAPA" />
<Minires code_reseau_minitel="CAPA" service="REPRISES "
nom_facture="CAPA" />
<Minires code_reseau_minitel="CAPA" service="STCKAGENCE"
nom_facture="CAPA" />
</toto>'

SELECT
T.att.value('local-name(.)', 'varchar(50)') AS Fieldname,
T.att.value('.', 'varchar(100)') AS Field_Value ,
1 + (ROW_NUMBER() OVER (ORDER BY att) - 1) /3 as Position
FROM
@xml.nodes('//@*') T(att);


/* result of above:

code_reseau_minitel CAPA 1
service CPRDEVISES 1
nom_facture CAPA 1
code_reseau_minitel CAPA 2
service REPRISES 2
nom_facture CAPA 2
code_reseau_minitel CAPA 3
service STCKAGENCE 3
nom_facture CAPA 3

*/

 >> Stay informed about: Instance numbering of a node 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
How to specify a root node in FOR XML - I am generating xml using the Select... FOR XML AUTO statement. I'm getting a result in the following form: <registrant p_fname="Clark" p_lname="Kent"/> <registrant p_fname="Lana" p_lname="Lang"/> I t...

replace value of method to an empty node - I am trying to replace the value of an xml node that is empty. I am using the replace value of method in the sql statement. If the node has a value it will replace correctly. If there is nothing in the node it will insert the value into the node. ....

How to retreive parent nodes and values from a child node? - Hello, I have an XML as below. declare @doc2 xml SET @doc2 = '<?xml version="1.0" ?> <p1:Domains_and_Emails xmlns:p1="LDAP"> <Organization>ABCD</Organization> <Radius Suspended="false" ID=&qu...

xml to SQL DB - We have some large existing Xml files that we'd like to store in the SQL server. We don't want to store just the Xml stream, but we rather like to build the hierarchical structure that represents the Xml in SQL and store the Xml that way. We also want...

how to store a word document in sql server - I Need help on how to store a word document in sql server.what datatype and how to convert it to store it. Pleas Help Thank you
   Database Help (Home) -> XML All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]