SQL Server XQuery with Default Namespace - sql-server

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.

Related

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;

Where EF6 doing where clause at SQL or at client

I am trying to start using EF6 for a project. My database is already filled with millions of records.
I can't find right explanation how does EF send T-SQL to SQL Server? I am afraid that I am going to download bunch of data to user for no reason.
In code below I have found three way to get my data to List<> but I am not sure which is right way to do WHERE clause at SQL.
I do not want to fill client with millions of record and to query (filter) that data at client.
using (rgtBaza baza = new rgtBaza())
{
var t = baza.Database.SqlQuery<CJE_DOC>("select * from cje_doc where datum between #od and #do",new SqlParameter("od", this.dateTimePickerOD.Value.Date ) ,new SqlParameter("do", this.dateTimePickerOD.Value.Date)).ToList();
var t = baza.CJE_DOC.Where(s => s.DATUM.Value >= this.dateTimePickerOD.Value.Date && s.DATUM.Value <= this.dateTimePickerDO.Value.Date).ToList();
var query = from b in baza.CJE_DOC
where b.DATUM >= this.dateTimePickerOD.Value.Date && b.DATUM.Value <= this.dateTimePickerDO.Value.Date
select b;
var t = query.ToList();
this.dataGridViewCJENICI.DataSource = t;
}
In all 3 cases, the filtering will happen on the database side, the filtering (or WHERE clause) will not take place on the client side.
If you want to verify that this is true, especially for your last 2 options, add some logging so that you can see the generated SQL:
baza.Database.Log = s => Console.WriteLine(s);
In this case, since you are using EF already, choose the 2nd or 3rd options, they are both equivalent with different syntax. Pick your favorite syntax.
In all of those examples, EF6 will generate a SQL query including the where clause - it won't perform the where clause on the client.
It won't actually retrieve any data from the database until you iterate through the results, which in the examples above, is when you call .ToList().
EF6 would only run the filter on the client if you called something like:
baza.CJE_DOC.ToList().Where(x => x.Field == value)
In this instance, it would retrieve the entire table when you called ToList(), and then use a client-side Linq query to filter the results in the where clause.
Any of the 3 will run the query on the SQL Server.
EF relies on LINQ's deferred execution model to build up an expression tree. Once you take an action that causes the expression to be enumerated (e.g. calling ToList(), ToArray(), or any of the other To*() methods), it will convert the expression tree to SQL, send the query to the server, and then start returning the results.
One of the side effects of this is that when using the query or lambda syntax, expressions that EF does not understand how to convert to SQL will cause an exception.
If you absolutely need to use some code that EF can't handle, you can break your code into multiple segments -- filtering things down as far as possible via code that can be converted to SQL, using the AsEnumerable() method to "close off" the EF expression, and doing your remaining filtering or transformations using Linq to Objects.

Simple.Data LIKE Operator in Where

How can we use LIKE operator in WHEREusing Simple.Data for SQL Server in ASP.Net C#
I need to run this SQL Query
SELECT MAX(regid) FROM reg_course WHERE(regid LIKE '%2013%')
Finally I achieved this task by aliasing the max(regid) column and querying in this fashion.
var cid=db.course_test.All()
.Select(db.course_test.regid.Max().As("maxcourseid"))
.Where(db.course_test.regid.Like(string.Concat("%",DateTime.Now.Year,"%")))
.FirstOrDefault();
Note
cid=db.course_test.All()
.Select(db.course_test.regid.Max().As("maxcourseid"))
.Where(db.course_test.regid.Like(string.Concat("%",DateTime.Now.Year,"%")))
returns the type Simple.Data.SqlQuery
Using FirstOrDefault returns Top result. If you don't want you can iterate in collection without using FirstOrDefault
Any other Better way Suggestions??

Working with columns having dot(.) in their name using GQL

I use Objectify for datastore operations in my GAE/Java application. I have used Objectify's #Embeded facility in a couple of places in my project. Objectify automatically flattens the nested objects within the entity marked by #Embeded notation using the . separator. Thus I have ended up with column names like entity.embededObject.Field
For example I have an entity 'Person' in my data store with two columns name and address.email.
I want to filter through Person in the datastore viewer by writing a simple GQL query.
But the following query fails with a syntax error:
SELECT * FROM Person where address.email='mail#gmail.com'
whereas the following works as it should
SELECT * FROM Person where name='Joe'
What am I doing wrong?
GQL currently doesn't support this - only 'word' characters are supported. You should definitely file this as a bug in the issue tracker.
Tested today, it is possible to run the following with backquotes
SELECT * FROM `your.kind`
I believe this holds true for any parameter, but please correct me if I am wrong.

How do I search a "Property Bag" table in SQL?

I have a basic "property bag" table that stores attributes about my primary table "Card." So when I want to start doing some advanced searching for cards, I can do something like this:
SELECT dbo.Card.Id, dbo.Card.Name
FROM dbo.Card
INNER JOIN dbo.CardProperty ON dbo.CardProperty.IdCrd = dbo.Card.Id
WHERE dbo.CardProperty.IdPrp = 3 AND dbo.CardProperty.Value = 'Fiend'
INTERSECT
SELECT dbo.Card.Id, dbo.Card.Name
FROM dbo.Card
INNER JOIN dbo.CardProperty ON dbo.CardProperty.IdCrd = dbo.Card.Id
WHERE (dbo.CardProperty.IdPrp = 10 AND (dbo.CardProperty.Value = 'Wind' OR dbo.CardProperty.Value = 'Fire'))
What I need to do is to extract this idea into some kind of stored procedure, so that ideally I can pass in a list of property/value combinations and get the results of the search.
Initially this is going to be a "strict" search meaning that the results must match all elements in the query, but I'd also like to have a "loose" query so that it would match any of the results in the query.
I can't quite seem to wrap my head around this one. My previous version of this was to do generate some massive SQL query to execute with a lot of AND/OR clauses in it, but I'm hoping to do something a little more elegant this time. How do I go about doing this?
it seems to me that you have an EAV model here.
if you're using sql server 2005 and up i'd suggest you use XML datatype for this:
http://weblogs.sqlteam.com/mladenp/archive/2006/10/14/14032.aspx
makes searching and stuff much easier with built in xml querying capabilities.
if you can't change your model then look at this:
http://weblogs.sqlteam.com/davidm/articles/12117.aspx

Resources