Every it-codigo has 1 or more es-codigo. I'm trying to find all the es-codigo of the it-codigo input, but it's taking too long. Did I do anything wrong in my code? For what I have seen, it's all right, unless there's something I don't know about that I'm doing wrong. Is QUERY-PREPARE() the best option in this case?
DEF VAR qEstrutura AS CHAR.
IF pi-cod-emitente <> 0 THEN DO:
qEstrutura = " WHERE item-cli.cod-emitente = " + QUOTER(pi-cod-emitente).
END.
IF pc-it-codigo <> "" THEN DO:
IF qEstrutura = "" THEN
qEstrutura = " WHERE estrutura.it-codigo = " + QUOTER(pc-it-codigo).
ELSE
qEstrutura = qEstrutura + " AND estrutura.it-codigo = " + QUOTER(pc-it-codigo).
END.
IF pc-item-cli <> "" THEN DO:
IF qEstrutura = "" THEN
qEstrutura = " WHERE item-cli.item-do-cli = " + QUOTER(pc-item-cli).
ELSE
qEstrutura = qEstrutura + " AND item-cli.item-do-cli = " + QUOTER(pc-item-cli).
END.
cQuery = cQuery + " FOR EACH item-cli, ".
cQuery = cQuery + " EACH estrutura ".
cQuery = cQuery + qEstrutura + " BREAK BY estrutura.es-codigo".
QUERY qConsulta:QUERY-PREPARE(cQuery).
QUERY qConsulta:QUERY-OPEN().
GET FIRST qConsulta.
DO WHILE AVAILABLE item-cli:
IF QUERY qConsulta:FIRST-OF(1) THEN DO:
CREATE tt-estrutura.
ASSIGN
tt-estrutura.it-codigo = estrutura.it-codigo
tt-estrutura.es-codigo = estrutura.es-codigo
.
GET NEXT qConsulta.
END.
END.
QUERY qConsulta:QUERY-CLOSE().
FOR EACH tt-estrutura:
DISP tt-estrutura.
END.
I believe it's the QUERY-OPEN() that takes time. Not QUERY-PREPARE().
Your query is only performing selection (WHERE) and sort (BY) on the second table. That makes is difficult to utilize indizes. The OpenEdge ABL query engine does not support flipping the buffer-sequence. Try turning the query around:
FOR EACH estrutura WHERE ......, FIRST item-cli.
Related
I want to join pt_mstr and in_mstr to create my report screen for which I have written this code i want to sort my output screen based on either prod line or status so i have defined a variable lvc_sort sort by prod line if i give 1 and similarly 2 for status
IF lvc_sort = 1 THEN DO:
FOR EACH pt_mstr no-lock
WHERE pt_domain = global_domain
AND pt_part >= lvc_part
AND pt_part <= lvc_part1
AND pt_part_type >= lvc_part_type
AND pt_part_type <= lvc_part_type1
AND pt_prod_line >= lvc_prod_line
AND pt_prod_line <= lvc_prod_line1
AND pt_status >= lvc_status
AND pt_status <= lvc_status1,
EACH in_mstr
WHERE in_domain = pt_domain
AND in_part = pt_part
BREAK BY pt_prod_line:
FIND FIRST tt NO-LOCK
WHERE tt_part = pt_part
AND tt_site = in_site NO-ERROR.
Is this the correct approach or can it be done in some other way?
You can use a query then you have a section with a different by clause
DEFINE VARIABLE cQuery AS CHARACTER NO-UNDO.
DEFINE VARIABLE hQuery AS HANDLE NO-UNDO.
cQuery = "FOR EACH pt_mstr no-lock " +
" WHERE pt_domain = " + QUOTER(global_domain) +
" AND pt_part >= " + QUOTER(lvc_part) +
" AND pt_part <= " + QUOTER(lvc_part1) +
" AND pt_part_type >= " + QUOTER(lvc_part_type) +
" AND pt_part_type <= " + QUOTER(lvc_part_type1) +
" AND pt_prod_line >= " + QUOTER(lvc_prod_line) +
" AND pt_prod_line <= " + QUOTER(lvc_prod_line1) +
" AND pt_status >= " + QUOTER(lvc_status) +
" AND pt_status <= " + QUOTER(lvc_status1) +
", EACH in_mstr " +
" WHERE in_domain = " + QUOTER(pt_domain) +
" AND in_part = " + QUOTER(pt_part).
CREATE QUERY hQuery.
hQuery:SET-BUFFERS(BUFFER pt_mstr:HANDLE, BUFFER in_mstr:HANDLE).
IF lvc_sort = 1 THEN
cQuery = cQuery + " BREAK BY pt_prod_line ".
ELSE
cQuery = cQuery + " BREAK BY pt_status ".
hQuery:QUERY-PREPARE(cQuery).
hQuery:QUERY-OPEN.
// alle durch
DO WHILE hQuery:GET-NEXT() :
FIND FIRST tt NO-LOCK
WHERE tt_part = pt_part
AND tt_site = in_site NO-ERROR.
END.
FINALLY:
hQuery:QUERY-CLOSE () NO-ERROR.
DELETE OBJECT hQuery NO-ERROR.
END FINALLY.
In Mysql, FIND_IN_SET is used to find value in a set. I have tried FIND_IN_SET in SQLite, but it is not an SQL keyword. I have Googled, but I did not get an answer. If anybody knows, please tell me the alternative to FIND_IN_SET in SQLite.
If you need just a true / false value rather than index then you can use LIKE clause:
(',' || column_name || ',') LIKE '%,value,%'
we can write query like change into hibernate critearea
my old query
select * FROM game_detail WHERE content_id=176 and FIND_IN_SET(12,group_master_id)
New query
select *
FROM game_detail
WHERE content_id=176
and (group_master_id LIKE '%12,%'|| group_master_id LIKE '%,12,'|| group_master_id LIKE '%12')
This is my old query
String query = "SELECT a.content_id,a.content_name,a.image_name,a.image_path,a.rating,d.content_type_name"
+ "from content_master a, category_content_mapping b, "
+ "game_detail c, content_type_master d "
+ "where a.content_id=b.content_id "
+ "and c.content_id = a.content_id "
+ "and a.content_type_id = d.content_type_id "
+ "and b.category_id = '" + category_id + "' "
+ "and find_in_set('" + group_master_id + "',c.group_master_id) "
+ "and a.is_active='Y' and b.is_active = 'Y' and c.is_active = 'Y'"
+ "order by b.content_mapping_id DESC limit 0,3";
Criteria in hibernate use like alternate of find_in_set
Session session=new Configuration().configure().buildSessionFactory().openSession();
Criteria criteria=session.createCriteria(ContentMaster.class);
criteria.setFetchMode("CategoryContentMapping", FetchMode.JOIN);
criteria.setFetchMode("GameDetail", FetchMode.JOIN);
criteria.createAlias("categoryContentMappings","cat");
criteria.createAlias("contentTypeMaster", "ctm");
criteria.createAlias("gameDetails","game");
criteria.add(Restrictions.eq("cat.categoryMaster.categoryMasterId", 9));
criteria.add(Restrictions.disjunction()
.add(Restrictions.like("game.groupMasterId","%12,%"))
.add(Restrictions.like("game.groupMasterId","%,12,%"))
.add(Restrictions.like("game.groupMasterId","%12%")));
criteria.add(Restrictions.eq("isActive", "y"));
criteria.add(Restrictions.eq("cat.isActive", "y"));
criteria.add(Restrictions.eq("ctm.isActive", "y"));
criteria.addOrder(Order.desc("cat.contentMappingId"));
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.
I have this 2 SQLite scripts:
both tested by direct input into SQLite.
def getOutgoingLinks(self, hostname):
t = (hostname,)
result = self.__cursor.execute("SELECT url.id, hostname.name, url.path, linking_to.keyword, siteId.id " +
"FROM url, hostname, linking_to, " +
"(SELECT url.id FROM url, hostname " +
"WHERE hostname.name = (?) " +
"AND hostname.id = url.hostname_id " +
") AS siteId " +
"WHERE linking_to.from_id = siteId.id " +
"AND linking_to.to_id = url.id " +
"AND url.hostname_id = hostname.id", t)
result = result.fetchall()
return result
def getIncommingLinks(self, hostname):
t = (hostname,)
result = self.__cursor.execute("SELECT url.id, hostname.name, url.path, linking_to.keyword, siteId.id " +
"FROM url, hostname, linking_to, " +
"(SELECT url.id FROM url, hostname " +
"WHERE hostname.name = (?) " +
"AND hostname.id = url.hostname_id " +
") AS siteId " +
"WHERE linking_to.to_id = siteId.id " +
"AND linking_to.from_id = url.id " +
"AND url.hostname_id = hostname.id", t)
result = result.fetchall()
return result
The getIncommingLinks() methond works very well, but getOutgoingLinks() causes an infinite Loop when python trys to execute the SQL statement. Any ideas what went wrong?
Rewrite your Select statements without select ...( Select ...) - thats very bad style. The result may solve your problem.
If by infinite loop you mean that the function doesnt ever get to return a value, I had the same problem.
Found the solution in Why python+sqlite3 is extremely slow?. With large tables it becomes a performance issue with the version shipped with python 2.7. I solved it by upgrading sqlite3 as specified here: https://stackoverflow.com/a/3341117/3894804
how to put white space before and after a character in text field
eg Text="John"
so i need whit space before and after John
You could try this:
SomeField.Text = " " + SomeField.Text + " ";
or using string.Format:
SomeField.Text = string.Format(" {0} ", SomeField.Text);
string InputValue = " " + MyTextBox.Text + " ";
Using Jquery, you could call this method...
function changeTxtBox(controlID) {
var oldValue = $(controlID).val();
var newValue = ' ' + result + ' ';
$(controlID).val(newValue)
}