SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


XML Column Read


XML Column Read

Author
Message
thbaig
thbaig
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1467 Visits: 504
Hi,
I have a column in table with xml type. I want to extract it row wise but it is giving me only first result

My Table has following data
id ItemId
1 <itemids><itemid>259</itemid><itemid>489</itemid></itemids>
2 <itemids><itemid>6834262</itemid><itemid>489</itemid></itemids>
3 <itemids><itemid>6603537</itemid></itemids>

select
tabId,
itemid.value('(/itemids//itemid/node())[1]','int') AS ItemIDA
from mytable;

query is only returning first ItemId against every Id.
Cadavre
Cadavre
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21788 Visits: 8519
Based on your sample data: -
IF object_id('tempdb..#yourTable') IS NOT NULL
BEGIN
DROP TABLE #yourTable;
END;

SELECT id, CAST(ItemId AS XML) AS ItemId
INTO #yourTable
FROM (VALUES(1, '<itemids><itemid>259</itemid><itemid>489</itemid></itemids>'),
(2, '<itemids><itemid>6834262</itemid><itemid>489</itemid></itemids>'),
(3, '<itemids><itemid>6603537</itemid></itemids>')
)a(id, ItemId);



You'd do it like this: -
SELECT id, ParamValues.ItemId.value('.','INT') AS ItemId
FROM #yourTable
OUTER APPLY ItemId.nodes('/itemids/itemid') ParamValues(ItemId);




Forever trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


Craig Wilkinson - Software Engineer
LinkedIn
thbaig
thbaig
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1467 Visits: 504
thank you. Smile
this is what I have implemented
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum







































































































































































SQLServerCentral


Search