|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Saturday, May 11, 2013 8:17 AM
Points: 460,
Visits: 2,521
|
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Friday, November 28, 2008 2:07 AM
Points: 21,
Visits: 21
|
|
Cool One.!! Another way to fetch the XML Element without loop will be by using the XQuery function Text.
For same xml this will work.
SELECT T.c.value('(Name/text())[1]', 'varchar(256)') as Name,T.c.value('(Department/text())[1]', 'varchar(256)') as Department FROM @x.nodes('/Employees/Employee') T(c)
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Thursday, May 16, 2013 5:46 AM
Points: 257,
Visits: 671
|
|
Another way using xquery if you have multiple Name elements as noted in this blog entry http://blogs.msdn.com/mrorke/archive/2005/07/21/441554.aspx
SELECT T.ref.value('.', 'varchar(256)') AS Employee FROM ( select [Xml]=@x.query(' for $i in data(/Employees/Employee/Name) return element temp { $i } ') ) A cross apply A.Xml.nodes('/temp') T(ref)
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Monday, May 13, 2013 2:04 AM
Points: 1,342,
Visits: 1,946
|
|
Does anyone know whether the XML or T-SQL count() functions are equivalent in execution, or is one more efficient than the other.
Is there any significant difference between...
SELECT @max = @x.query(' { count(/Employees/Employee) } ').value('e[1]','int')
andselect @max = count(*) from @x.nodes('/Employees/Employee') e(x)
?
The query plans indicate that the cost of the first is twice the cost of the second. Is this true in practice?
Derek
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Wednesday, June 13, 2012 7:05 AM
Points: 4,
Visits: 41
|
|
just do this to retrieve the element count for the loop - we've had to do this a lot.. select @max = @x.value('fn:count(/Employees/Employee)','int')
or you could even include the xpath in the while loop definition: while @i <= (@x.value('fn:count(/Employees/Employee)','int')) begin print @i set @i = @i + 1 end
there are a lot of fn: xpath functions that work really well for different situations - BOL has a listing of them, just search for "fn:"
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Wednesday, March 27, 2013 3:22 PM
Points: 1,276,
Visits: 1,112
|
|
You can retrieve all names without a CTE and CROSS APPLY as well:
DECLARE @x XML;
SET @x = '<Employees> <Employee ID="101"> <Name>Jacob</Name> <Department>IT</Department> </Employee> <Employee ID="354"> <Name>Steve</Name> <Department>IT</Department> </Employee> <Employee ID="456"> <Name>Bob</Name> <Department>IT</Department> </Employee> <Employee ID="478"> <Name>Joe</Name> <Department>IT</Department> </Employee> <Employee ID="981"> <Name>Louis</Name> <Department>IT</Department> </Employee> </Employees>';
-- select "Name" WITH Num(i) AS ( SELECT 1 UNION ALL SELECT i + 1 FROM Num WHERE i < (SELECT @x.value('count(/Employees/Employee)','int') ) ) SELECT x.value('Name[1]', 'VARCHAR(20)') FROM Num CROSS APPLY @x.nodes('/Employees/Employee[position()=sql:column("i")]') e(x);
That's if you really want to use the position() function. Without using position() it gets a little simpler:
SELECT x.value('Name[1]', 'VARCHAR(20)') FROM @x.nodes('/Employees/Employee') e(x);
|
|
|
|
|
Right there with Babe
      
Group: General Forum Members
Last Login: Today @ 11:40 AM
Points: 768,
Visits: 1,160
|
|
This has been helpful, after I spent 0.5 day yesterday trying to figure out XQuery in T-SQL as DBA in the end, I cast XML to VARCHAR and PATINDEX it.... 
Can anybody show/confirm how I can do this as SET operation on a table T with X as a XML column, instead of working on 1 XML @x at a time
e.g. can I do this to get all Names, say in a department table T that has an Employee XML column X?
SELECT x.value('Name[1]', 'VARCHAR(20)') FROM T.X.nodes('/Employees/Employee') e(x);
Thanks in advance I really need a quick lesson in XQuery
SQLServerNewbie
MCITP: Database Administrator SQL Server 2005
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Tuesday, March 26, 2013 5:32 PM
Points: 125,
Visits: 453
|
|
Sorry to be Lazy or maybe just an american?
Anyway I know there is a meaning behind this in the code.
e(x)
I just can't seem to figure out what. One of the Replies used T(c) instead. I'm sure it's some type of XML formatting paramater I just have no idea and am curious.
Thanks,
M
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Wednesday, March 27, 2013 3:22 PM
Points: 1,276,
Visits: 1,112
|
|
| After the .nodes() method you must create a table and column alias. e is the table alias, c is the column alias.
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 4:31 PM
Points: 58,
Visits: 249
|
|
This is more elegant.
SELECT Table1.Column1.value('.','VARCHAR(20)') AS Employee_Name FROM @XML_Employee_Name.nodes('/Employees/Employee/Name[1]') AS Table1(Column1)
|
|
|
|