| Author |
Topic |
|
arkiboys
Master Smack Fu Yak Hacker
1433 Posts |
Posted - 2008-11-21 : 08:14:37
|
| I have learned a new way to query an xml field using .query('data(/)')The good thing about this is that you do not have to use the namespaceThe only problem is that if in the xml field there are several values, then the returned result concatenates them first and then display the resultHow is it possible to alter this query so that for each value inside the xml field, I get one row.i.edeclare @myTable table(ProfileID int, Characteristic xml)insert @myTableselect 1, '<stringList xmlns="http://schemas.myCompany.com/myProject/[app]/stringList/1.0"><value>Programmes</value></stringList>'UNION ALLselect 2, '<stringList xmlns="http://schemas.myCompany.com/myProject/[app]/stringList/1.0"><value>Programmes</value></stringList>'UNION ALLselect 3, '<stringList xmlns="http://schemas.myCompany.com/myProject/[app]/stringList/1.0"><value>VOn</value></stringList>'UNION ALLselect 4, '<stringList xmlns="http://schemas.myCompany.com/myProject/[app]/stringList/1.0"> <value>Securities</value><value>VOn</value><value>Notes</value> </stringList>'select convert(nvarchar(max), Characteristic.query('data(/)')) Characteristic, convert(nvarchar(max), SecurityType.query('data(/)')) SecurityTypefrom myTableRetrieved RESULTSProfileID Characteristic1 Programmes2 Programmes3 VOn4 SecuritiesVOnNotes......Correct RESULTS shouold beProfileID Characteristic1 Programmes2 Programmes3 VOn4 Securities4 VOn4 Notes......Thank you |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-21 : 08:51:08
|
| for that i think you need to write FLWOR expressions. see below for examplehttp://searchsqlserver.techtarget.com/tip/0,289483,sid87_gci1309059,00.html |
 |
|
|
arkiboys
Master Smack Fu Yak Hacker
1433 Posts |
Posted - 2008-11-21 : 16:02:03
|
| If still using 'data(/)' as before,Do you know how to modify the query to separate the concatenated values using a comma in the field of the row.ThanksSo that the result shows something like:ProfileID Characteristic1 Programmes2 Programmes3 VOn4 Securities, VOn, Notes |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-22 : 01:35:32
|
| Any luck trying the solution i gave herehttp://www.sqlteam.com/forums/topic.asp?TOPIC_ID=114935 |
 |
|
|
arkiboys
Master Smack Fu Yak Hacker
1433 Posts |
Posted - 2008-11-22 : 15:19:32
|
| Hello,I think you are referring to my other post which is to do with NULL issue. Am I right?If so, I found that technology difficult to implement.Thanks |
 |
|
|
arkiboys
Master Smack Fu Yak Hacker
1433 Posts |
Posted - 2008-11-25 : 16:38:50
|
| Hello,I can not get this query to work for what I want.This is what I have now:declare @myTable table(ProfileID int, Characteristic xml)insert @myTableselect 186990, '<stringList xmlns="http://schemas.myCompany.com/myProject/[app]/stringList/1.0"><value>Programmes</value></stringList>', NULLUNION ALLselect 186991, '<stringList xmlns="http://schemas.myCompany.com/myProject/[app]/stringList/1.0"><value>Programmes</value></stringList>', NULLUNION ALLselect 186992, '<stringList xmlns="http://schemas.myCompany.com/myProject/[app]/stringList/1.0"><value>Straights</value></stringList>', '<stringList xmlns="http://schemas.myCompany.com/myProject/[app]/stringList/1.0"><value>MTN</value></stringList>'UNION ALLselect 186993, '<stringList xmlns="http://schemas.myCompany.com/myProject/[app]/stringList/1.0"><value>Straights</value><value>Von</value><value>Winner</value></stringList>', '<stringList xmlns="http://schemas.myCompany.com/myProject/[app]/stringList/1.0"><value>MTN</value><value>something</value></stringList>'select * from @myTableSELECT ProfileID ,Characteristic.query('declare namespace ns="http://schemas.myCompany.com/myProject/[app]/stringList/1.0";for $ed in /stringList/valuereturn $ed') AS CharacteristicFROM @myTableERROR:Msg 213, Level 16, State 1, Line 3Insert Error: Column name or number of supplied values does not match table definition. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-11-25 : 16:52:19
|
1. You are inserting three columns in a two-column table2. You are declaring the namespace as ns, right? Then your query must also include the namespace name.But you surely knew this because we have helped you numerous times with this problem now...declare @myTable table(ProfileID int, Characteristic xml)insert @myTableselect 186990, '<stringList xmlns="http://schemas.myCompany.com/myProject/[app]/stringList/1.0"><value>Programmes</value></stringList>'UNION ALLselect 186991, '<stringList xmlns="http://schemas.myCompany.com/myProject/[app]/stringList/1.0"><value>Programmes</value></stringList>'UNION ALLselect 186992, '<stringList xmlns="http://schemas.myCompany.com/myProject/[app]/stringList/1.0"><value>Straights</value></stringList>' UNION ALLselect 186993, '<stringList xmlns="http://schemas.myCompany.com/myProject/[app]/stringList/1.0"><value>Straights</value><value>Von</value><value>Winner</value></stringList>'select * from @myTableSELECT ProfileID ,Characteristic.query('declare namespace ns="http://schemas.myCompany.com/myProject/[app]/stringList/1.0";for $ed in /ns:stringList/ns:valuereturn $ed') AS CharacteristicFROM @myTable E 12°55'05.63"N 56°04'39.26" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-11-25 : 17:04:35
|
[code]declare @myTable table(ProfileID int, Characteristic xml)insert @myTableselect 186990, '<stringList xmlns="http://schemas.myCompany.com/myProject/[app]/stringList/1.0"><value>Programmes</value></stringList>'UNION ALLselect 186991, '<stringList xmlns="http://schemas.myCompany.com/myProject/[app]/stringList/1.0"><value>Programmes</value></stringList>'UNION ALLselect 186992, '<stringList xmlns="http://schemas.myCompany.com/myProject/[app]/stringList/1.0"><value>Straights</value></stringList>' UNION ALLselect 186993, '<stringList xmlns="http://schemas.myCompany.com/myProject/[app]/stringList/1.0"><value>Straights</value><value>Von</value><value>Winner</value></stringList>'declare @stage table (profileid int, x varchar(50));WITH XMLNAMESPACES('http://schemas.myCompany.com/myProject/[app]/stringList/1.0' AS yak)insert @stage (profileid, x)SELECT s.ProfileID, q.c.value('.', 'VARCHAR(50)') AS issuerFROM @myTable AS sCROSS APPLY s.Characteristic.nodes('/yak:stringList/yak:value') AS q(c)select s.profileid, stuff(r.g, 1, 2, '') AS xfrom (select profileid from @stage group by profileid) AS scross apply (select ', ' + e.x from @stage AS e where e.profileid = s.profileid for xml path('')) AS r(g)[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-11-25 : 17:09:15
|
I don't know how we can get through to you.You ignore most of our working advices and keep trying for other ways.And when those don't work you come back for more help. E 12°55'05.63"N 56°04'39.26" |
 |
|
|
arkiboys
Master Smack Fu Yak Hacker
1433 Posts |
Posted - 2008-11-26 : 03:12:34
|
| Hi,Believe it or not I do not try to ignore your help.Thank you for your help as always. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-26 : 03:26:18
|
quote: Originally posted by arkiboys Hi,Believe it or not I do not try to ignore your help.Thank you for your help as always.
still not sure why Pesos suggestion not works for you |
 |
|
|
arkiboys
Master Smack Fu Yak Hacker
1433 Posts |
Posted - 2008-11-26 : 03:34:13
|
| All ok now guys.Sorry to have made you spend more time than perhaps required on this.Many thanks |
 |
|
|
|