selectStmt=(select +
('*' |delimitedList(functionbody|column)).setResultsName("Columns") + Optional((_as+table.setResultsName("Aliases"))|table.setResultsName("**Aliases1**"),'')+
Optional(_into+('*' |columns).setResultsName("Columns"),'')+
_from +
table.setResultsName("Tables",listAllMatches=True)+Optional((_as+table.setResultsName("Aliases"))|table.setResultsName(**Aliases2**),'')+
Optional(where + Group(whereExpr), '').setResultsName("where") +
Each([Optional(groupby + columns("groupby")+Optional(_asc|_desc,''),'').setDebug(False),
Optional(orderby + columns("orderby"),'').setDebug(False),Optional(_limit+columnVal,'')
])).setResultsName("Select",listAllMatches=True)+Optional((_union_all|_union)+selectStmt,'').setResultsName("Union")
so if i am matching query like
select count(id) sellercount
into v_photocount
from carphotos photos
where inquiryid = v_inquiryid
and isdealer = (
case
when v_sellertype = 1 then 1
else 0
end
)
and isactive = 1
and isapproved = 1;
Then sellercount is matching with _into and photos with where.
How can I avoid This
Related
I have a View in AX with a computed column:
private static server str qty()
{
#define.THEN(" THEN ")
#define.SINGLE_QUOTE("'")
return 'CASE T2.ReturnStatus ' +
' WHEN ' + int2str(enum2int(ReturnStatusLine::None)) + #THEN + '-1 * T3.UnitValue' +
' WHEN ' + int2str(enum2int(ReturnStatusLine::Awaiting)) + #THEN + '-1 * T3.UnitValue' +
' WHEN ' + int2str(enum2int(ReturnStatusLine::Registered)) + #THEN + '-1 * T3.UnitValue' +
' ELSE ' + "(T3.UnitValue / T2.ExpectedRetQty * (SELECT TOP 1 SUM(cpst.Qty) as RcvQty from custPackingSlipTrans as cpst where cpst.InventTransId = T2.InventTransId and cpst.dataAreaId='" + curext() + "')) * -1" +
' END';
}
It works great, except the past week or so the column is returning NULL when it should not be. This is fixed simply by going into the AOT and syncing this view, after that the column has a valid value. But we're having to do this almost daily.
Any ideas?
Have a query like this,
select *
from job_profile
where case
when exists (
select 1
from job_profile
where screening_type_id = 2 and job_category_id = 4
)
then screening_type_id = 2 and job_category_id = 4
else screening_type_id =4
end;
which I need to write this in JPA.
String GET_JOB_VACCINATIONS_BY_JOB_CAT_ID_AND_SCRN_TYPE_ID = "SELECT jobvacc FROM JobVaccination jobvacc WHERE CASE"
+ " WHEN EXISTS ( SELECT 1 FROM JobVaccination jobvacc2 WHERE jobvacc2.jobCategoryMast.jobCategoryId=:jobCategoryId AND jobvacc2.screeningTypeMast.screeningTypeId=:screeningTypeId )"
+ " THEN jobvacc.jobCategoryMast.jobCategoryId=4 AND jobvacc.screeningTypeMast.screeningTypeId=2 "
+ " ELSE jobvacc.screeningTypeMast.screeningTypeId=2"
+ " END";
Tried this way which throws exception like "Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: = near line 1, column 260 [SELECT............."
Cant we write the case queries in JPA ?
Refereed this link https://en.wikibooks.org/wiki/Java_Persistence/JPQL_BNF#New_in_JPA_2.0 which says yes.
This is the query which is update single record in a table.
Where FriendTable.tablename and other column name is define a constant file.
Values which is to update is store in Friend variable.
FriendTable.updateValues=function(Friend,successCallback,errorCallback){
var query = 'update '+ FriendTable.tablename + ' set ' + FriendTable.lat + '=?,' + FriendTable.lon + '=?,' + FriendTable.locaddr + '=?,' + FriendTable.locts + '=? where ' + FriendTable.phonenumber + " =?";
app.dbobj.transaction(function (tx) {
console.log('Update Friend record :: ' + query);
console.log('Friend data :: ' + Friend.toString());
tx.executeSql(query,[Friend.lat,Friend.lon,Friend.locaddr,Friend.locts,Friend.phonenumber],successCallback,errorCallback);
});
}
I am an ASP.Net developer & using sql server CE 4.0 I want to know how to use place holder for this code, As this query is currently vulnerable to sql injection. Place holder can prevent this but the problem is for example query = "SELECT * FROM TABLE WHERE TITLE = #0" but in my query the value of #0 is dynamically added to query how do i use place holder
this is the code
if (Request["search"] != "" && Request["search"] != null)
{
var search = Request["search"].Trim();
string[] querynew = search.Split(' ');
var searchquery = "and ";
foreach (string word in querynew)
{
searchquery += "response_table.adtitle LIKE '%" + word + "%' OR ";
}
sql += searchquery.Remove(searchquery.Length - 4);
}
if (Request["min"] != "" && Request["min"] != null && Request["max"] != null && Request["max"] != "")
{
sql = sql + " and (CAST(response_table.price AS Float)) between " + Request["min"].Trim() + " AND " + Request["max"].Trim();
}
// 3. the order clause
switch (Request["sort"])
{
case "recent":
sql = sql + "ORDER BY response_table.response_ID DESC OFFSET " + offset + " ROWS FETCH NEXT " + pageSize + " ROWS ONLY";
break;
case "hightolow":
sql = sql + "ORDER BY CAST(response_table.price AS Float) Desc OFFSET " + offset + " ROWS FETCH NEXT " + pageSize + " ROWS ONLY";
break;
case "lowtohigh":
sql = sql + "ORDER BY CAST(response_table.price AS Float) ASC OFFSET " + offset + " ROWS FETCH NEXT " + pageSize + " ROWS ONLY";
break;
default:
break;
}
result = db.Query(sql);
Thank You
Using parameters (instead of concatenating strings) allows you to optimize performance of your queries.
You can use a SqlCeCommand. It has a collection of parameters and you can find a sample here regarding how to use them.
It seem to me that this was hard...
var MostRated = (from p in db.Posts
let AverageRating = CalculateAverageRatingFromPost(p)
where p.PostStatus == Convert.ToInt32(PostStatusEnum.Published.Value) && p.IsDeleted == false
orderby p.PublishedDate descending
select new
{
PostUrl = Common.PostUrl(p.Section.Name, p.Permalink),
Title = Common.TrimString(p.Title, 50),
Excerpt = Common.TrimString(p.Excerpt, 80),
AverageRate = CalculateAverageRating((from pr in db.Ratings
where pr.ObjectType == Convert.ToInt32(ObjectTypeEnum.Post.Value) && pr.ObjectID == p.ID
select pr).SingleOrDefault()),
PublishedDate = new DateHelper().DateTimeInWords(p.PublishedDate.Value)
}).OrderByDescending(o => o.AverageRate).Take(5).ToList();
and the other method is
private Decimal CalculateAverageRating(Rating pr)
{
if (pr != null)
{
Decimal Average = ((1 * (Decimal)pr.Star1) + (2 * (Decimal)pr.Star2) + (3 * (Decimal)pr.Star3) + (4 * (Decimal)pr.Star4) + (5 * (Decimal)pr.Star5)) / ((Decimal)pr.Star1 + (Decimal)pr.Star2 + (Decimal)pr.Star3 + (Decimal)pr.Star4 + (Decimal)pr.Star5);
return Average;
}
else
{
return 0;
}
}
I get this run time error "no supported translation to SQL".
What i want is to get lists of the posts, and do a small quick calculations of the rating and then sort those from highest to low and take 5 posts only.
Thanks
I haven't tested this, but try defining the function as an Expression object.
Expression<Rating, Decimal> CalculateAverageRating =
pr => (Decimal)(
pr == null ? 0 :
(pr.Star1 + 2*pr.Star2 + 3*pr.Star3 + 4*pr.Star4 + 5*pr.Star5)
/(pr.Star1 + pr.Star2 + pr.Star3 + pr.Star4 + pr.Star5));