Click here to Skip to main content
15,912,457 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I could not find out what is 1 in front of 'Id' in value function!!!

SQL
DECLARE @XMLTest XML = 
'
<Person> 
	<Id>6</Id> 
	<Name>AABBc</Name> 
	<Family>AABBc</Family> 
	<IDNumber>120000</IDNumber> 
</Person>
'

SELECT T.Item.value('Id[1]'			, 'varchar(20)'),
       T.Item.value('Name[1]'		, 'varchar(20)'),
       T.Item.value('Family[1]'		, 'varchar(20)'),
       T.Item.value('IDNumber[1]'	, 'varchar(20)')
FROM   @XMLTest.nodes('/Person') AS T(Item)
Posted
Comments
RedDk 17-Feb-13 15:05pm    
Venture do I, question this is; ADO.NET programmer you are.

Suggest you the data one record is. Reply says I. One, one is.
Results TSQL F5:

(No column name) (No column name) (No column name) (No column name)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

6 AABBc AABBc 120000
Behno0o0oD 17-Feb-13 15:10pm    
I don't understand yet !
RedDk 17-Feb-13 15:14pm    
Open SSMS and paste the exact code block you offer here into a new query then hit F5. The return shows the record (I tried to paste into the "comment" box with little success). This is the nature of the XML at present. With respect to TSQL anyway.
Behno0o0oD 17-Feb-13 15:17pm    
Thanks but I have seen the result in ssms, but why when I change one to two, the record becomes null??
RedDk 17-Feb-13 15:20pm    
Or paste this:SELECT T.Item.value('Id[1]' , 'varchar(20)') As [Id],

T.Item.value('Name[1]' , 'varchar(20)') As [Name],

T.Item.value('Family[1]' , 'varchar(20)') As ['Mily],

T.Item.value('IDNumber[1]' , 'varchar(20)') As [IDNumber]

FROM @XMLTest.nodes('/Person') AS T(Item)

That DECLARE is an assignment of an XML string to a variable. By using the SELECT method the variable is being queried as a TABLE that was parsed by the XML parser that lives in a culvert under the SSMS bridge.
[edir]
We're a lttle out of synch here
There's only ONE record ... hang on a minute
[end edit]

1 solution

bO,
DECLARE @XMLTest XML = 
'
<Person> 
	<Id>87</Id> 
	<Name>AbacAba</Name> 
	<Family>wildtype</Family> 
	<IDNumber>110999</IDNumber> 
</person>
<person> 
	<Id>6</Id> 
	<Name>AABBc</Name> 
	<Family>AABBc</Family> 
	<IDNumber>120000</IDNumber> 
</person>
'
SELECT T.Item.value('Id[1]'			, 'varchar(20)') As [Id],
       T.Item.value('Name[1]'		, 'varchar(20)') As [Name],
       T.Item.value('Family[1]'		, 'varchar(20)') As [Family],
       T.Item.value('IDNumber[1]'	, 'varchar(20)') As [IDNumber]
FROM   @XMLTest.nodes('/Person') AS T(Item)

And the return:
Id	Name	Family	        IDNumber
87	AbacAba	wildtype	110999
6	AABBc	AABBc	        120000

Yeah, about that index ... what is it? Possibly some ADO.NET string array identifier? Look at this:
DECLARE @XMLTest XML = 
'
<Person> 
	<Id>87</Id>
	<Id>01</Id>
		<CulvertNo>6</CulvertNo >
	<Name>AbacAba</Name> 
	<Family>wildtype</Family> 
	<IDNumber>110999</IDNumber> 
</Person>
<Person> 
	<Id>6</Id>
	<Id>02</Id> 
		<CulvertNo>7</CulvertNo>
	<Name>AABBc</Name> 
	<Family>AABBc</Family> 
	<IDNumber>120000</IDNumber> 
</Person>
'
SELECT T.Item.value('Id[2]'			, 'varchar(20)') As [Id],
       T.Item.value('Name[1]'		, 'varchar(20)') As [Name],
       T.Item.value('Family[1]'		, 'varchar(20)') As [Family],
       T.Item.value('IDNumber[1]'	, 'varchar(20)') As [IDNumber],
       T.Item.value('CulvertNo[1]'  , 'varchar(20)') As [CulvertNo]
FROM   @XMLTest.nodes('/Person') AS T(Item)

THAT gets me this:
Id	Name	Family	        IDNumber	CulvertNo
01	AbacAba	wildtype	110999	        6
02	AABBc	AABBc	        120000	        7

See the return of the second [Id]. So XQuery has some other nature as well. Less trollish than I imagined.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900