Issue to generate dynamic xml with XMLElement - sql
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?
Related
Transforming JSON data to relational data
I want to display data from SQL Server where the data is in JSON format. But when the select process, the data does not appear: id item_pieces_list 0 [{"id":2,"satuan":"BOX","isi":1,"aktif":true},{"id":4,"satuan":"BOX10","isi":1,"aktif":true}] 1 [{"id":0,"satuan":"AMPUL","isi":1,"aktif":"true"},{"id":4,"satuan":"BOX10","isi":5,"aktif":true}] I've written a query like this, but nothing appears. Can anyone help? Query : SELECT id, JSON_Value(item_pieces_list, '$.satuan') AS Name FROM [cisea.bamedika.co.id-hisys].dbo.medicine_alkes AS medicalkes
Your Path is wrong. Your JSON is an array, and you are trying to retrieve it as a flat object SELECT id, JSON_Value(item_pieces_list,'$[0].satuan') AS Name FROM [cisea.bamedika.co.id-hisys].dbo.medicine_alkes Only in the case of data without the [] (array sign) you could use your original query '$.satuan', but since you are using an array I change it to retrieve only the first element in the array '$[0].satuan'
AWS Athena: Error parsing field value and unexpected query results
I have the following table schema prepared by AWS glue When I query the table using SELECT * FROM "vietnam-property-develop"."sell" limit 10;, it throws an error: HIVE_BAD_DATA: Error parsing field value '{"area":"85 m²","date":"14/01/2020","datetime":"2020-01-18 00:42:28.488576+00:00","address":"Quan Hoa - Cầu Giấy","price":"20 Tỷ","cat":"Bán nhà mặt phố","lon":"105.7976502","avatar":"","id":"24169794","title":"Chính chủ cần bán nhà mặt phố nguyễn văn huyên Quan Hoa Cầu Giấy, 2 tầng, dt 85m2. LH 0903233723","lat":"21.0376771","room":"0"}' for field 4: org.openx.data.jsonserde.json.JSONObject cannot be cast to java.lang.Double Then I tired to just query the title column by using SELECT title FROM "vietnam-property-develop"."sell" limit 10; It returns result which I didn't expect. It seems that the query return the whole json files instead of just the title column. And the number of rows is 4 but not 10 no matter how I modify the query.
TSQL - Need to query a database column which is populated by XML
TSQL - Need to query a database column which is populated by XML. The Database has an iUserID column with an Application ID and VCKey TxtValue is the Column name and the contained Data is similar to this <BasePreferencesDataSet xmlns="http://tempuri.org/BasePreferencesDataSet.xsd"> <ViewModesTable> <iViewID>1</iViewID> </ViewModesTable> <ViewMode_PreferenceData> <iViewID>1</iViewID> <iDataID>0</iDataID> <strValue>False</strValue> </ViewMode_PreferenceData> <ViewMode_PreferenceData> <iViewID>1</iViewID> <iDataID>5</iDataID> <strValue>True</strValue> </ViewMode_PreferenceData> <ViewMode_PreferenceData> <iViewID>1</iViewID> <iDataID>6</iDataID> <strValue>True</strValue> </ViewMode_PreferenceData> <ViewMode_PreferenceData> <iViewID>1</iViewID> <iDataID>4</iDataID> <strValue>False</strValue> I want to be able to identify any iUserID in which the StrValue for iDataID's 5 and 6 are not set to True. I have attempted to use a txtValue Like % statement but even if I copy the contents and query for it verbatim it will not yield a result leading me to believe that the XML data cannot be queried in this manner. Screenshot of Select * query for this DB for reference
You can try XML-method .exist() together with an XPath with predicates: WITH XMLNAMESPACES(DEFAULT 'http://tempuri.org/BasePreferencesDataSet.xsd') SELECT * FROM YourTable WHERE CAST(txtValue AS XML).exist('/BasePreferencesDataSet /ViewMode_PreferenceData[iDataID=5 or iDataID=6] /strValue[text()!="True"]')=1; The namespace-declaration is needed to address the elements without a namespace prefix. The <ViewMode_PreferenceData> is filtered for the fitting IDs, while the <strValue> is filtered for a content !="True". This will return any data row, where there is at least one entry, with an ID of 5 or 6 and a value not equal to "True".
So without sample data (including tags; sorry you're having trouble with that) it's tough to craft the complete query, but what you're looking for is XQuery, specifically the .exists method in T-SQL. Something like SELECT iUserID FROM tblLocalUserPreferences WHERE iApplicationID = 30 AND vcKey='MonitorPreferences' AND (txtValue.exist('//iDataID[text()="5"]/../strValue[text()="True"]') = 0 OR txtValue.exist('//iDataID[text()="6"]/../strValue[text()="True"]')=0) This should return all userID's where either iDataID 5 or 6 do NOT contain True. In other words, if both are true, you won't get that row back.
postgresql : xml parsing : xpath return only one record, where is xml contains 3 records
I have a procedure in postgresql with xml type paramter. I need to parse xml and insert record in a table. I tried this way, but it is inserting only one record in table, where as xml contains three records. the xml code is Select (xpath('/ArrayOfUserGroupModulePermissionBusinessObject/UserGroupModulePermissionBusinessObject/ModuleID/text()', t1))[1]::varchar::BIGINT AS ModuleID from (Select Perms::xml as t1) as t2; the xml is here <ArrayOfUserGroupModulePermissionBusinessObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <UserGroupModulePermissionBusinessObject> <UserGroupModulePermissionID>0</UserGroupModulePermissionID> <UserGroupID>0</UserGroupID> <ModuleID>3</ModuleID> <ModuleName>Administration</ModuleName> <CanRead>false</CanRead> <CanAdd>false</CanAdd> <CanEdit>false</CanEdit> <CanDelete>false</CanDelete> <CanPrint>false</CanPrint> </UserGroupModulePermissionBusinessObject> <UserGroupModulePermissionBusinessObject> <UserGroupModulePermissionID>0</UserGroupModulePermissionID> <UserGroupID>0</UserGroupID> <ModuleID>2</ModuleID> <ModuleName>Data Management</ModuleName> <CanRead>false</CanRead> <CanAdd>false</CanAdd> <CanEdit>false</CanEdit> <CanDelete>false</CanDelete> <CanPrint>false</CanPrint> </UserGroupModulePermissionBusinessObject> <UserGroupModulePermissionBusinessObject> <UserGroupModulePermissionID>0</UserGroupModulePermissionID> <UserGroupID>0</UserGroupID> <ModuleID>1</ModuleID> <ModuleName>Reception</ModuleName> <CanRead>false</CanRead> <CanAdd>false</CanAdd> <CanEdit>false</CanEdit> <CanDelete>false</CanDelete> <CanPrint>false</CanPrint> </UserGroupModulePermissionBusinessObject> </ArrayOfUserGroupModulePermissionBusinessObject> I don't know what is wrong in this sql
The xpath function returns an array with an element for each result. You only use the first element in the array while the array has actually all three elements you expect. Use unnest to convert the array to rows. Select unnest(xpath('/ArrayOfUserGroupModulePermissionBusinessObject/UserGroupModulePermissionBusinessObject/ModuleID/text()', t1))::varchar::BIGINT AS ModuleID from (Select Perms::xml as t1) as t2;
Using Fields[0].Value to get XML from FOR XML RAW, ELEMENTS query is messed up
I have a query that uses FOR XML RAW, ELEMENTS to return a SELECT query as a structured XML document. However, when I get the result using a TSQLDataSet by using Fields[0].Value, the result is different from what I see when I run the query in SQL Server Management Studio. What I see in the result from the TSQLDataSet: customerIdфname၄governmentNumberไdebtorAddress1ไdebtorAddress2ไdebtorAddress3ไdebtorAddress4ࡄpostCodeୄcontactNameՄphonë́faxൄcustomerSinceՄtermsلactiveไcurrentBalanceلDebtorခŁ䄁ഃӤ What I see in the result in SSMS: <Debtor> <customerId>C0E449E5B2C </customerId> <name>New Customer 2 </name> <governmentNumber> </governmentNumber> <debtorAddress1>Address Line 1 </debtorAddress1> <debtorAddress4>Address Line 4 </debtorAddress4> <postCode>1234 </postCode> <phone>1234567890 </phone> <fax>1234567890 </fax> <customerSince>2013-06-10T18:16:06.213</customerSince> <terms>M </terms> <active>true</active> <currentBalance>0.0000</currentBalance> </Debtor> Is there a particular way it should be executed to get the right result?
AFAIK this is a DbExpress limitation. I know how overcome this, but using ADO (the returned data must be requested using a special parametrized object and a set of ADO streams). However you can use a workaround converting the XML data to a string in the server side sorrounding the sentence with a select (subquery) or just using a simple CAST statement. For example if you sentence is like so SELECT Foo, Bar FROM FooTable FOR XML RAW, ELEMENTS you can rewrite to SELECT (SELECT Foo, Bar FROM FooTable FOR XML RAW, ELEMENTS) or you can rewrite to (use a CAST VARCHAR or NVARCHAR) SELECT CAST( (SELECT Foo, Bar FROM FooTable FOR XML RAW, ELEMENTS) AS VARCHAR(MAX)) and finally Retrieve the result like this SQLDataSet1.Fields[0].AsString