Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

XML Tag reading Expand / Collapse
Author
Message
Posted Thursday, October 11, 2012 4:52 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum 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
Post #1371351
Posted Thursday, October 11, 2012 5:08 AM


SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC 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
Post #1371357
Posted Thursday, October 11, 2012 5:11 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld 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!
Post #1371362
Posted Thursday, October 11, 2012 6:15 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum 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.
Post #1371406
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse