I have a query: (show next below):
SELECT XMLElement("PetitionMarreCSV",
XMLAttributes('http://INT010.GPA.Schemas.External.GPA_AllFile' AS "xmlns"),
XMLForest(EVENT as "Event",INSERTDATE as "InsertDate",USER_ID as "USER_ID",FIRSTNAME as "FirstName",NAME as "Name",EMAIL as "Email",LANGUAGE as "Language",COUNTRY as "COUNTRY",USER_PROFILE_PIC as "USER_PROFILE_PIC",USER_PROFILE_VIDEO as "USER_PROFILE_VIDEO",OPTIN as "OPTIN",USER_WISTIA_VIDEO as "USER_WISTIA_VIDEO",USER_ACEPT as "USER_ACEPT",USER_APROVED as "USER_APROVED",STRET as "STRET",CITY as "City",POSTALCODE as "PostalCode",TELEPHONE as "Telephone",USER_INTERESTS as "USER_INTERESTS",USER_VIDEO_VIEWS as "USER_VIDEO_VIEWS",USER_SORT_ORDER as "USER_SORT_ORDER",DALING_VAN_DE_KOPKRACHT as "DALING_VAN_DE_KOPKRACHT",TOEGANG_TOT_DE_GEZONDHEIDSZORG as "TOEGANG_TOT_DE_GEZONDHEIDSZORG",RISICO_OP_EN_BLACK_OUT as "RISICO_OP_EN_BLACK_OUT",GEPLANDE_VEROUDERING as "GEPLANDE_VEROUDERING",VERHOGING_VERZEKERINGSPRIJZEN as "VERHOGING_VERZEKERINGSPRIJZEN",DURE_INTERNET as "DURE_INTERNET",TREINVERTRAGINGEN as "TREINVERTRAGINGEN",TRAGHEID_VAN_JUSTITIE as "TRAGHEID_VAN_JUSTITIE",LAGE_RENDEMENT_SPAREKENING as "LAGE_RENDEMENT_SPAREKENING",ONGEZONDE_JUNKFOD as "ONGEZONDE_JUNKFOD",MINDER_POSTKANTOREN as "MINDER_POSTKANTOREN",OPLICHTERIJ_EN_BEDROG as "OPLICHTERIJ_EN_BEDROG",VERKOPERS_AN_HUIS as "VERKOPERS_AN_HUIS",FILENAME_SOURCE as "FileName_Source")) AS "RESULT"
FROM (SELECT EVENT,INSERTDATE,USER_ID,FIRSTNAME,NAME,EMAIL,LANGUAGE,COUNTRY,USER_PROFILE_PIC,USER_PROFILE_VIDEO,OPTIN,USER_WISTIA_VIDEO,USER_ACEPT,USER_APROVED,STRET,CITY,POSTALCODE,TELEPHONE,USER_INTERESTS,USER_VIDEO_VIEWS,USER_SORT_ORDER,DALING_VAN_DE_KOPKRACHT,TOEGANG_TOT_DE_GEZONDHEIDSZORG,RISICO_OP_EN_BLACK_OUT,GEPLANDE_VEROUDERING,VERHOGING_VERZEKERINGSPRIJZEN,DURE_INTERNET,TREINVERTRAGINGEN,TRAGHEID_VAN_JUSTITIE,LAGE_RENDEMENT_SPAREKENING,ONGEZONDE_JUNKFOD,MINDER_POSTKANTOREN,OPLICHTERIJ_EN_BEDROG,VERKOPERS_AN_HUIS,FILENAME_SOURCE
FROM ops$kli.GPA22_20150130
GROUP BY EVENT,INSERTDATE,USER_ID,FIRSTNAME,NAME,EMAIL,LANGUAGE,COUNTRY,USER_PROFILE_PIC,USER_PROFILE_VIDEO,OPTIN,USER_WISTIA_VIDEO,USER_ACEPT,USER_APROVED,STRET,CITY,POSTALCODE,TELEPHONE,USER_INTERESTS,USER_VIDEO_VIEWS,USER_SORT_ORDER,DALING_VAN_DE_KOPKRACHT,TOEGANG_TOT_DE_GEZONDHEIDSZORG,RISICO_OP_EN_BLACK_OUT,GEPLANDE_VEROUDERING,VERHOGING_VERZEKERINGSPRIJZEN,DURE_INTERNET,TREINVERTRAGINGEN,TRAGHEID_VAN_JUSTITIE,LAGE_RENDEMENT_SPAREKENING,ONGEZONDE_JUNKFOD,MINDER_POSTKANTOREN,OPLICHTERIJ_EN_BEDROG,VERKOPERS_AN_HUIS,FILENAME_SOURCE)
This query create a list of CLOB value in xml format. But, by some reason that I don't know, sometimes retrieve the next error for some CLOB result instead to show the xml value:
Error: XML document must have a top level element.
The strange thing is, if I retrieve (filter the query) only with the row who has the error instead of multiples rows, the value with the error is not anymore.
SELECT XMLElement("PetitionMarreCSV",
XMLAttributes('http://INT010.GPA.Schemas.External.GPA_AllFile' AS "xmlns"),
XMLForest(EVENT as "Event",INSERTDATE as "InsertDate",USER_ID as "USER_ID",FIRSTNAME as "FirstName",NAME as "Name",EMAIL as "Email",LANGUAGE as "Language",COUNTRY as "COUNTRY",USER_PROFILE_PIC as "USER_PROFILE_PIC",USER_PROFILE_VIDEO as "USER_PROFILE_VIDEO",OPTIN as "OPTIN",USER_WISTIA_VIDEO as "USER_WISTIA_VIDEO",USER_ACEPT as "USER_ACEPT",USER_APROVED as "USER_APROVED",STRET as "STRET",CITY as "City",POSTALCODE as "PostalCode",TELEPHONE as "Telephone",USER_INTERESTS as "USER_INTERESTS",USER_VIDEO_VIEWS as "USER_VIDEO_VIEWS",USER_SORT_ORDER as "USER_SORT_ORDER",DALING_VAN_DE_KOPKRACHT as "DALING_VAN_DE_KOPKRACHT",TOEGANG_TOT_DE_GEZONDHEIDSZORG as "TOEGANG_TOT_DE_GEZONDHEIDSZORG",RISICO_OP_EN_BLACK_OUT as "RISICO_OP_EN_BLACK_OUT",GEPLANDE_VEROUDERING as "GEPLANDE_VEROUDERING",VERHOGING_VERZEKERINGSPRIJZEN as "VERHOGING_VERZEKERINGSPRIJZEN",DURE_INTERNET as "DURE_INTERNET",TREINVERTRAGINGEN as "TREINVERTRAGINGEN",TRAGHEID_VAN_JUSTITIE as "TRAGHEID_VAN_JUSTITIE",LAGE_RENDEMENT_SPAREKENING as "LAGE_RENDEMENT_SPAREKENING",ONGEZONDE_JUNKFOD as "ONGEZONDE_JUNKFOD",MINDER_POSTKANTOREN as "MINDER_POSTKANTOREN",OPLICHTERIJ_EN_BEDROG as "OPLICHTERIJ_EN_BEDROG",VERKOPERS_AN_HUIS as "VERKOPERS_AN_HUIS",FILENAME_SOURCE as "FileName_Source")) AS "RESULT"
FROM (SELECT EVENT,INSERTDATE,USER_ID,FIRSTNAME,NAME,EMAIL,LANGUAGE,COUNTRY,USER_PROFILE_PIC,USER_PROFILE_VIDEO,OPTIN,USER_WISTIA_VIDEO,USER_ACEPT,USER_APROVED,STRET,CITY,POSTALCODE,TELEPHONE,USER_INTERESTS,USER_VIDEO_VIEWS,USER_SORT_ORDER,DALING_VAN_DE_KOPKRACHT,TOEGANG_TOT_DE_GEZONDHEIDSZORG,RISICO_OP_EN_BLACK_OUT,GEPLANDE_VEROUDERING,VERHOGING_VERZEKERINGSPRIJZEN,DURE_INTERNET,TREINVERTRAGINGEN,TRAGHEID_VAN_JUSTITIE,LAGE_RENDEMENT_SPAREKENING,ONGEZONDE_JUNKFOD,MINDER_POSTKANTOREN,OPLICHTERIJ_EN_BEDROG,VERKOPERS_AN_HUIS,FILENAME_SOURCE
FROM ops$kli.GPA22_20150130
WHERE user_id = 293
GROUP BY EVENT,INSERTDATE,USER_ID,FIRSTNAME,NAME,EMAIL,LANGUAGE,COUNTRY,USER_PROFILE_PIC,USER_PROFILE_VIDEO,OPTIN,USER_WISTIA_VIDEO,USER_ACEPT,USER_APROVED,STRET,CITY,POSTALCODE,TELEPHONE,USER_INTERESTS,USER_VIDEO_VIEWS,USER_SORT_ORDER,DALING_VAN_DE_KOPKRACHT,TOEGANG_TOT_DE_GEZONDHEIDSZORG,RISICO_OP_EN_BLACK_OUT,GEPLANDE_VEROUDERING,VERHOGING_VERZEKERINGSPRIJZEN,DURE_INTERNET,TREINVERTRAGINGEN,TRAGHEID_VAN_JUSTITIE,LAGE_RENDEMENT_SPAREKENING,ONGEZONDE_JUNKFOD,MINDER_POSTKANTOREN,OPLICHTERIJ_EN_BEDROG,VERKOPERS_AN_HUIS,FILENAME_SOURCE)
Do you know if there is a problem with this function when you try to generate several CLOB columns?
I have a SQL query to get data from SQL Server 2012:
select dbo.TRIM(cfProj.cfProjId) as "cfProjId",
dbo.TRIM(cfProj.cfAcro) as "cfAcro",
(SELECT cfLangCode as "#cfLangCode", cfTrans as "#cfTrans", cfProjTitle.cfTitle
FROM dbo.cfProjTitle
WHERE dbo.cfProjTitle.cfProjId = dbo.cfProj.cfProjId
FOR XML PATH('cfTitle'), type)
from cfProj FOR XML PATH('cfProj')
This SQL returns data structure like:
<cfProj>
<cfProjId>00001111</cfProjId>
<cfAcro>222</cfAcro>
<cfTitle cfLangCode="ru" cfTrans="h">
<cfTitle>some title here</cfTitle>
</cfTitle>
</cfProj>
But I want get XML structure without second nested "cfTitle" element:
<cfProj>
<cfProjId>00001111</cfProjId>
<cfAcro>222</cfAcro>
<cfTitle cfLangCode="ru" cfTrans="h">some title here</cfTitle>
</cfProj>
Difference in this line: <cfTitle cfLangCode="ru" cfTrans="h">some title here</cfTitle>
Any ideas how I can get desirable result?
If you don't need multiple cfTitle elements, you can use marc_s solution, but if you do - you can try this:
select
p.cfProjId,
p.cfAcro,
(
select
pt.cfLangCode as [#cfLangCode],
pt.cfTrans as [#cfTrans],
pt.cfTitle as [text()]
from cfProjTitle as pt
where pt.cfProjId = p.cfProjId
for xml path('cfTitle'), type
)
from cfProj as p
for xml path('cfProj')
sql fiddle demo
I am performing a query against an XML data type column in SQL Server 2012. An example of the data is:
<ns:Resume xmlns:ns="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/Resume">
<ns:Name>
<ns:Name.Prefix></ns:Name.Prefix>
<ns:Name.First>Shai</ns:Name.First>
<ns:Name.Middle></ns:Name.Middle>
<ns:Name.Last>Bassli</ns:Name.Last>
<ns:Name.Suffix></ns:Name.Suffix>
</ns:Name>
...
</ns:Resume>
I am trying to write a query to return the first names.
This query returns a list of first names as expected:
WITH XMLNAMESPACES ('http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/Resume' AS ns)
SELECT [Resume].query('(//ns:Name.First)').value('.[1]', 'nvarchar(100)')
FROM HumanResources.JobCandidate;
However, this query returns an error:
WITH XMLNAMESPACES ('http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/Resume' AS ns)
SELECT [Resume].value('(//ns:Name.First)[1]', 'nvarchar(100)')
FROM HumanResources.JobCandidate;
Error:
Msg 9314, Level 16, State 1, Line 2
XQuery [HumanResources.JobCandidate.Resume.value()]: Cannot implicitly atomize or apply 'fn:data()' to complex content elements, found type 'xs:anyType' within inferred type '(element(ns{http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/Resume}:Name.First,xs:string) | element(ns{http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/Resume}:Name.First,xs:anyType)) ?'.
There's some basic understanding that I'm missing here but I'm not sure what it is. Can someone enlighten me? Why does the second query return an error?
Your XPath expression could lead to multiple rows being returned, for each row in the SQL Server table. You'll need to use a CROSS APPLY and a call to .nodes() to get that information you're after:
WITH XMLNAMESPACES ('http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/Resume' AS ns)
SELECT
JobCandidateID,
ResNames.value('(ns:Name.First)[1]', 'nvarchar(100)')
FROM
HumanResources.JobCandidate
CROSS APPLY
[Resume].nodes('/ns:Resume/ns:Name') AS XTbl(ResNames)
That should return all JobCandidateID values and all first names defined in the Resume XML column for each row in the table.
If you can be sure that there's only ever going to be a single <name> tag in your XML column, then you could also shorten this to:
WITH XMLNAMESPACES ('http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/Resume' AS ns)
SELECT
JobCandidateID,
[Resume].value('(/ns:Resume/ns:Name/ns:Name.First)[1]', 'nvarchar(100)')
FROM
HumanResources.JobCandidate