XML AUTO is creating nested element - sql-server

my xml:auto is creating nested element on performing join so i want to remove nested node of other table..
<fo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ss.dbo.FO>
<aa>10</aa>
<bb>120</bb>
<cc>([ads]{})})</cc>
<stds.dbo.IR>
<dd>Upmp</dd>
</ss.dbo.DIR>
</ss.dbo.FO>

use the XML:ROW in place of XML:AUTO to Solve this problem XML:AUTO is generating automatic xml for the sql query if you use xml:r--row it will genrate but in simple format without any nested values-- – PANKAJ 786 Dec 13 at 10:39

Related

Is there a way to return list of 10 rows of data at a time using a where condition from a database using native query?

I'm currently using a database where I'll be sending 10 unique Id's at a time and I'm expecting a list of objects in return.
Is there any way where I can use a native query to get this.
select * from tbl_bank_customer_accounts where uid in ('05b6864b-ad10-4efe-af8d-b556e02dfc75','19226ae6-72f3-40dc-a690-079c979a3044','1a196c99-007f-4edd-a8a1-556f070cc852' ,'1e88f4fb-101c-4f6c-acd0-100bd1d02237' ,'20511b9b-8e7b-44fb-ba79-5c2103679b39' ,'3564b980-59e3-411f-8cc2-1bffa3cf9883' ,'4491b692-b6a4-4581-b150-411ab03151a4' ,'6364b0eb-015c-422f-a5e2-ae00828c70d6' ,'6ed7a6a2-265d-4d59-8764-d7742296aa17' ,'93c99403-d099-401f-a544-44984dcfc925');
the above one is my query can it be implemented using stored procedure

How to filter based on the last element in ArrayField in django

I'm using postgresql database which allows having an array datatype, in addition django provides PostgreSQL specific model fields for that.
My question is how can I filter objects based on the last element of the array?
class Example(models.Model):
tags = ArrayField(models.CharField(...))
example = Example.objects.create(tags=['tag1', 'tag2', 'tag3']
example_tag3 = Example.objects.filter(tags__2='tag3')
I want to filter but don't know what is the size of the tags. Is there any dynamic filtering something like:
example_tag3 = Example.objects.filter(tags__last='tag3')
I don't think there is a way to do that without "killing the performance" other than using raw SQL (see this). But you should avoid doing things like this, from the doc:
Tip: Arrays are not sets; searching for specific array elements can be
a sign of database misdesign. Consider using a separate table with a
row for each item that would be an array element. This will be easier
to search, and is likely to scale better for a large number of
elements.
Adding to the above answer and comment, if changing the table structure isn't an option, you may filter your query based on the first element in an array by using field__0:
example_tag3 = Example.objects.filter(tags__0='tag1')
However, I don't see a way to access the last element directly in the documentation.

T-SQL xquery .modify method using a wildcard

I am working in SQL Server 2014. I have created a stored procedure which does its processing thing, and at the end, takes the final query output and formats it as XML. Due to the nature of the logic in the procedure, sometimes a node must be deleted from the final XML output. Here's a sample of the XML output (for brevity I have not included the root nodes; hopefully they won't be required to answer my question):
<DALink>
<InteractingIngredients>
<InteractingIngredient>
<IngredientID>1156</IngredientID>
<IngredientDesc>Lactobacillus acidophilus</IngredientDesc>
<HICRoot>Lactobacillus acidophilus</HICRoot>
<PotentiallyInactive>Not necessary</PotentiallyInactive>
<StatusCode>Live</StatusCode>
</InteractingIngredient>
</InteractingIngredients>
<ActiveProductsExistenceCode>Exist</ActiveProductsExistenceCode>
<IngredientTypeBasisCode>1</IngredientTypeBasisCode>
<AllergenMatch>Lactobacillus acidophilus</AllergenMatch>
<AllergenMatchType>Ingredient</AllergenMatchType>
</DALink>
<ScreenDrug>
<DrugID>1112894</DrugID>
<DrugConceptType>RxNorm_SemanticClinicalDr</DrugConceptType>
<DrugDesc>RxNorm LACTOBACILLUS ACIDOPHILUS/PECTIN ORAL capsule</DrugDesc>
<Prospective>true</Prospective>
</ScreenDrug>
In the procedure I have code that looks at the XML structure above and deletes a node when it shouldn't be there because it's easier to modify the xml than tweak the query output. Here is a sample of that code:
SET #xml_Out.modify('declare namespace ccxsd="http://schemas.foobar.com/CC/v1_3"; delete //ccxsd:InteractingIngredients[../../../ccxsd:ScreenDrug/ccxsd:DrugConceptType="OneThing"]');
SET #xml_Out.modify('declare namespace ccxsd="http://schemas.foobar.com/CC/v1_3"; delete //ccxsd:InteractingIngredients[../../../ccxsd:ScreenDrug/ccxsd:DrugConceptType="AnotherThing"]');
SET #xml_Out.modify('declare namespace ccxsd="http://schemas.foobar.com/CC/v1_3"; delete //ccxsd:InteractingIngredients[../../../ccxsd:ScreenDrug/ccxsd:DrugConceptType="SomethingElse"]');
SET #xml_Out.modify('declare namespace ccxsd="http://schemas.foobar.com/CC/v1_3"; delete //ccxsd:InteractingIngredients[../../../ccxsd:ScreenDrug/ccxsd:DrugConceptType="SomethingElseAgain"]');
SET #xml_Out.modify('declare namespace ccxsd="http://schemas.foobar.com/CC/v1_3"; delete //ccxsd:InteractingIngredients[../../../ccxsd:ScreenDrug/ccxsd:DrugConceptType="RxNorm*"]');
The final command is the one I can't figure out how to make work. All I need to do is to look for instances where the element "DrugConceptType" starts with the string "RxNorm", because there are multiple versions of the string that can possibly occur.
I have Googled and StackOverFlowed at length, but perhaps because of my inexperience in this area I didn't ask the question correctly.
Is there a relatively easy way to re-write the final .modify statement above to use a wildcard after "RxNorm"?
Your reduction of the root node is a problem acutally, as you are using the namespace "ccxsd" and your XML does not show this.
Anyway, better, than to write the declare namespace ... over and over, was this as first line of your statement:
;WITH XMLNAMESPACES('http://schemas.fdbhealth.com/CC/v1_3' AS ccxsd)
Well, as a .modify() is a one statement call it doesn't make such a difference...
But to your question of a wildcard
This would delete all nodes, where the Element's content starts with "RxNorm":
SET #xml.modify('delete //*[fn:substring(.,1,6)="RxNorm"]');
Be aware of missing namespaces... Cannot test it...
EDIT: A simplified working example:
You have to check this with your actual XML (with root and namespace)
DECLARE #xml_Out XML=
'<DALink>
<InteractingIngredients>
<InteractingIngredient>
<IngredientID>1156</IngredientID>
<IngredientDesc>Lactobacillus acidophilus</IngredientDesc>
<HICRoot>Lactobacillus acidophilus</HICRoot>
<PotentiallyInactive>Not necessary</PotentiallyInactive>
<StatusCode>Live</StatusCode>
</InteractingIngredient>
</InteractingIngredients>
<ActiveProductsExistenceCode>Exist</ActiveProductsExistenceCode>
<IngredientTypeBasisCode>1</IngredientTypeBasisCode>
<AllergenMatch>Lactobacillus acidophilus</AllergenMatch>
<AllergenMatchType>Ingredient</AllergenMatchType>
</DALink>
<ScreenDrug>
<DrugID>1112894</DrugID>
<DrugConceptType>RxNorm_SemanticClinicalDr</DrugConceptType>
<DrugDesc>RxNorm LACTOBACILLUS ACIDOPHILUS/PECTIN ORAL capsule</DrugDesc>
<Prospective>true</Prospective>
</ScreenDrug>';
SET #xml_Out.modify('delete //InteractingIngredients[fn:substring((../../ScreenDrug/DrugConceptType)[1],1,6)="RxNorm"]');
SELECT #xml_Out;

Compound sort not working in Spring mongodb

I have a requirement where the records will be sorted based on created date first and if created dates are same, we will sort on another field called as ratings.
In my Spring mongo project I am doing the following thing:
Query query = new Query();
query.with(new Sort(Direction.DESC, "crDate")).with(new Sort(Direction.DESC, "ratings"));
For some reasons its only sorting on the first field ie crDate. And if both dates are same, sort by ratings never work.
When i try to check the value of sort object it shows me this:
{"crDate":-1,"ratings":-1}
Another finding is, mongo takes in the following syntax for compound sorts:
db.abc.find({..some criteria..}).sort([["crDate",-1],["ratings",-1]]);
Is this a bug in spring mongodb implementation or I missed something?
Looking at the Spring API Documentation it shows you can specify multiple strings to the sort object you are creating in a list. From you snippet above I would suggest you need to only apply the one sort object that takes the two fields, something like
query.with(new Sort(Direction.DESC, Arrays.asList("crDate", "ratings")));
There was another constructor that took the List of Order objects. Strange but I tried it with that now and it seems to be working.
I am now using a single with clause and passing in a List of Order

SQL Server XQuery with Default Namespace

I've got some XML Data in a SQL Server Table in an XML Column as follows:
<AffordabilityResults>
<matchlevel xmlns="urn:callcredit.co.uk/soap:affordabilityapi2">IndividualMatch</matchlevel>
<searchdate xmlns="urn:callcredit.co.uk/soap:affordabilityapi2">2013-07-29T11:20:53</searchdate>
<searchid xmlns="urn:callcredit.co.uk/soap:affordabilityapi2">{E40603B5-B59C-4A6A-92AB-98DE83DB46E7}</searchid>
<calculatedgrossannual xmlns="urn:callcredit.co.uk/soap:affordabilityapi2">13503</calculatedgrossannual>
<debtstress xmlns="urn:callcredit.co.uk/soap:affordabilityapi2">
<incomedebtratio>
<totpaynetincome>0.02</totpaynetincome>
<totamtunsecured>0.53</totamtunsecured>
<totamtincsec>0.53</totamtincsec>
</incomedebtratio>
</debtstress>
</AffordabilityResults>
You'll note that some of the elements have an xmlns attribute and some don't...
I need to write queries to return the data - and more importantly show a business analyst how to write her own queries to get the data she needs so I want it to be as simple as possible.
I can query the data easily using the WITH XMLNAMESPACES element as follows:
WITH XMLNAMESPACES (N'urn:callcredit.co.uk/soap:affordabilityapi2' as x )
SELECT
ResponseXDoc.value('(/AffordabilityResults/x:matchlevel)[1]','varchar(max)' ) AS MatchLevel
, ResponseXDoc.value('(/AffordabilityResults/x:debtstress/x:incomedebtratio/x:totamtunsecured)[1]','nvarchar(max)' ) AS UnsecuredDebt
FROM [NewBusiness].[dbo].[t_TacResults]
But adding the x: part to the query makes it look overly complicated, and I want to keep it simple for the business analyst.
I tried adding:
WITH XMLNAMESPACES (DEFAULT 'urn:callcredit.co.uk/soap:affordabilityapi2' )
and removing the x: from the XQuery - but this returns null (possibly because of the lack of the xmlns on the root element?)
Is there any way I can simplify these queries either with or without the default namespace?
If namespaces are not important in your use case, you could use the namespace wildcard selector *:, which both selects nodes without and with arbitrary namespaces.
An example query could be
(/*:AffordabilityResults/*:matchlevel)[1]
The business analyst will still have to add the selector in front of every node test, but it's the same "prefix" all the time and the only error to be expected is forgetting to use it somewhere.

Resources