|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Friday, November 16, 2012 6:19 AM
Points: 7,
Visits: 13
|
|
I am reading below XML through T-SQL query, please see below and suggest me the solution DECLARE @xmlAttributeRelation XML
SET @xmlAttributeRelation='<Entity> <Entity>1</Entity> <Entity>2</Entity> </Entity> SELECT ISNULL(NULLIF(a.b.query('Entity').value('.', '[hf_id]'),''),0) AS Entity FROM @xmlAttributeRelation.nodes('/attributeRelationRequest/Entity') a(b) Result is: Entity -------- 12 Expected Result is: Entity -------- 1 2 Appriciate your advise
|
|
|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Today @ 4:34 PM
Points: 91,
Visits: 96,934
|
|
venkat.2829 (10/11/2012)
I am reading below XML through T-SQL query, please see below and suggest me the solution DECLARE @xmlAttributeRelation XML
SET @xmlAttributeRelation='<Entity> <Entity>1</Entity> <Entity>2</Entity> </Entity> SELECT ISNULL(NULLIF(a.b.query('Entity').value('.', '[hf_id]'),''),0) AS Entity FROM @xmlAttributeRelation.nodes('/attributeRelationRequest/Entity') a(b) Result is: Entity -------- 12 Expected Result is: Entity -------- 1 2 Appriciate your advise
You could try the following (I edited the XML as I don't think it was valid, you can edit the query below to match your XML)...
DECLARE @xmlAttributeRelation XML
SET @xmlAttributeRelation='<attributeRelationRequest> <Entity>1</Entity> <Entity>2</Entity> </attributeRelationRequest>'
;WITH cte as ( SELECT a.b.query('.') as Entity FROM @xmlAttributeRelation.nodes('/attributeRelationRequest') as a(b) ) SELECT c.d.query('.').value('.', 'INT') ItemID FROM cte CROSS APPLY Entity.nodes('/attributeRelationRequest/Entity') as c(d)
Cheers />L
----------------- ... Then again, I could be totally wrong! Check the answer. Check out posting guidelines here for faster more precise answers.
I believe in Codd ... and Thinknook is my Chamber of Understanding
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: 2 days ago @ 8:08 AM
Points: 342,
Visits: 1,072
|
|
DECLARE @xmlAttributeRelation XML SET @xmlAttributeRelation='<Entity> <Entity>1</Entity> <Entity>2</Entity> </Entity> ' SELECT Result = ent.val.value('.', 'int') FROM @xmlAttributeRelation.nodes('/Entity/Entity') ent(val)
Result ----------- 1 2
(2 row(s) affected)
It would be better to name the outer tag differently, e.g. "ArrayOfEntity" to avoid confusion.
_____________________________________________________ XDetails Addin - for SQL Developers and DBA blog.sqlxdetails.com - Transaction log myths - debunked!
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Friday, November 16, 2012 6:19 AM
Points: 7,
Visits: 13
|
|
| Thank you very much, it is working.
|
|
|
|