I have this select that works every time running in workbench but fails sometimes for the same arguments over jdbc. The problem is that sometimes, over JDBC, 'pos' value returns null. I Think that, for some reason, the #p as not started, but dont know how to fix.
SELECT t1.wId, t1.twId, t1.name, t1.timeout, t1.pos
FROM (
SELECT w.id AS wId, tw2.id AS twId, w.name AS name, tw2.timeout AS timeout, #p:=#p+1 AS pos
FROM timeout_workqueue tw1
INNER JOIN timeout_workqueue tw2
ON tw1.workqueue_id = tw2.workqueue_id
INNER JOIN workqueue w
ON tw1.workqueue_id = w.id
WHERE tw1.id = ?
ORDER BY tw2.id) t1, (SELECT #p:=1) c
WHERE t1.twId = ?;
The whole Java Code Are:
public TimeoutWorkqueueView getTimeoutWorkqueueView(Integer id) {
String sql = "SELECT t1.wId, t1.twId, t1.name, t1.timeout, t1.pos"
+ " FROM ("
+ " SELECT w.id AS wId, tw2.id AS twId, w.name AS name, tw2.timeout AS timeout, #p:=#p+1 AS pos"
+ " FROM timeout_workqueue tw1"
+ " INNER JOIN timeout_workqueue tw2"
+ " ON tw1.workqueue_id = tw2.workqueue_id"
+ " INNER JOIN workqueue w"
+ " ON tw1.workqueue_id = w.id"
+ " WHERE tw1.id = ?"
+ " ORDER BY tw2.id) t1, (SELECT #p:=1) c"
+ " WHERE t1.twId = ?";
return (TimeoutWorkqueueView) getJdbcTemplate().queryForObject(sql, new BeanPropertyRowMapper(TimeoutWorkqueueView.class), id, id);
}
Ok so the problem i can see here (please verify) is that you think you are running the query with parameter set to 1 to begin with.
However if you set #p = 1 -> this #p:=#p+1 will not evaluate to 1 any more.
Also assuming you have 20 rows, you run this query 20 times but on the last run it will return null because pos will be 21 and this does not exists.
Related
select new mvp.backend.finance.dto.projection.SplitShipDto(spl.id, spl.promoVolume, spl.baselineVolumeProportion) from SplitShipment spl inner join DateDay spd on spl.dateDay.id = spd.id " +
"where spl.sku.id =:sku_id and spd.day > cast(:date as string)
skuIdAndDates.parallelStream().forEach(spl -> {
float rcpPromo = priceFromSap.getBasePrice() * spl.getPromoVolume();
float rcpBaseline = priceFromSap.getBasePrice() * spl.getBaselineVolumeProportion();
RequestSplitShipmentRCP requestSplitShipmentRCP = RequestSplitShipmentRCP.builder().id(spl.getId()).rcpPromo(rcpPromo).rcpBaseline(rcpBaseline).build();
splitShipmentRCP.add(requestSplitShipmentRCP);
});
update split_shipments set " +
"rcp_promo =:rcpPromo, rcp_baseline =:rcpBaseline " +
"where id =:id
Guys help me. I must use this 2 queries in one query. In first query i select spl.id, spl.promoVolume, spl.baselineVolumeProportion from table split_shipments. How I can creat this? Sorry for my eng...
I have the following code
#Query(nativeQuery = true, value = "select e.* from event e"
+ " join league l on (l.id = e.league_id)"
+ " join sport s on (l.sport_id = s.id)"
+ " join team t1 on (t1.id = e.team_one_id)"
+ " join team t2 on (t2.id = e.team_two_id)"
+ " join country c on (c.id = l.country_id)"
+ " where l.name LIKE %?1%")
Page<Event> getAllEventsFiltered(final PageRequest of, final String filter);
If execute this SQL into database I receive a lot of results but when is executed from java I receive no one result in the same SQL.
I already tried:
where l.name LIKE %?#{escape([1])} escape ?#{escapeCharacter()}
but just works to begins and I needs for both begin and end.
JDBC parameters can only be included in very specific places in a SQL statement. They are not replaced as strings in the SQL statement but they are applied. The specific valid places change from database to database, and also depend on the JDBC driver variant/version.
The general rule is that a parameter can be applied where a single value would be present. String parameters should not be enclosed in single quotes as a VARCHAR would be.
One option to make it compliant, is to place the parameter isolated from other text, for example by using the CONCAT() function. Your query could be rephrased as:
#Query(nativeQuery = true, value = "select e.* from event e"
+ " join league l on (l.id = e.league_id)"
+ " join sport s on (l.sport_id = s.id)"
+ " join team t1 on (t1.id = e.team_one_id)"
+ " join team t2 on (t2.id = e.team_two_id)"
+ " join country c on (c.id = l.country_id)"
+ " where l.name LIKE concat('%', ?1, '%')") // change here
Nevertheless, the specific syntax change can be different depending on the database you are using (that you haven't mentioned).
Following is the logic in my java code :
con.setAutoCommit(false);
int starIndex = 0;
List Details = new ArrayList();
while (true) {
isRsEmpty = true;
String my_Query = "select * from x,y,z where"
+ " x.a = y.b"
+ " x.a = z.c"
+ "y.b = z.e"
+ " ORDER BY a ASC"
+ " limit 5"
+ " offset " + starIndex;
preparedStatement = con.prepareStatement(my_Query);
rs = preparedStatement.executeQuery();
while (rs.next()) {
isRsEmpty = false;
//Other processing steps
starIndex++;
Details.add(rs.getInt("id"));
}
if(isRsEmpty){
starIndex = 0;
}
Iterator itr = Details.iterator();
String updateTable = "UPDATE x SET status = ? where i = ? and j = ? and k = ?";
updatePreparedStatement = con.prepareStatement(updateTable);
while (itr.hasNext()) {
updatePreparedStatement.setInt(1, 1);
updatePreparedStatement.setString(2, "zzz");
updatePreparedStatement.setInt(3, 9);
updatePreparedStatement.setInt(4, 10);
updatePreparedStatement.addBatch();
}
updatePreparedStatement.executeBatch();
con.commit();
Details.clear();
}
The Problem :
I have 13 entries in my table which meets the select query.
When I first time run the query my Limit is 5 and Offset is 0 and I get
5 records correctly from the table and the selected 5 records are updated.
After doing the update to the table when the select query runs again it gives me 3 records.This is unexpected since I have more 8 records left in the table.Again the 3 records are updated.
Again when the select query runs it gives me 0 records.
Then again a select query runs which gives me 5 records and updates the 5 records.
I am not able to understand this behaviour. If I run my select query without update then it runs correctly which gives me 5,5,3 records , but with the above logic it gives me 5,3,0,5 records.
What is the problem here ?
Note : In the above program all the variables like PreparedStatement and other are declared correctly.
Thank you
Is there any chance you are missing "and" keyword between your conditions in the select query?
String my_Query = "select * from x,y,z where"
+ " x.a = y.b"
+ " AND x.a = z.c"
+ " AND y.b = z.e"
+ " ORDER BY a ASC"
+ " limit 5"
+ " offset " + starIndex;
I don't know if that would solve the problem but I find it weird that it works like this.
I have a problem with getting the alias of the columnnames for my ResultSet.
I made a subquery, where i use the alias function(MAX(...) in SQL) but everytime I execute the Statement, I get java.sql.SQLException because the column name is not valid. And I use the current alias where I call the getString - Function of my ResultSet.
This is my SQL-Statement in Eclipse:
String sql = "SELECT a.steelgrade, a.prod_order_id, a.prod_order_item_pos, "
+"a.prod_order_version, a.strip_thickn_aim, a.strip_width_aim, "
+"a.customer, a.order_weight_plan, b.grund_kommlos, b.coil_weight "
+"FROM (SELECT prod_order_id, prod_order_item_pos, "
+ "MAX (prod_order_version) AS max_version "
+ "FROM production_order "
And in the ResultSet while.next()-Loop:
prod_order_version = AuftraegeProduction.getString("max_version");
This is the whole SQL-Statement( in the Database it works fine!):
SELECT a.steelgrade, a.prod_order_id, a.prod_order_item_pos,
a.prod_order_version, a.strip_thickn_aim, a.strip_width_aim,
a.customer, a.order_weight_plan, b.grund_kommlos, b.coil_weight
FROM (SELECT prod_order_id, prod_order_item_pos,
MAX (prod_order_version) AS max_version
FROM production_order
GROUP BY prod_order_id, prod_order_item_pos) c
JOIN
production_order a
ON a.prod_order_id = c.prod_order_id
AND a.prod_order_item_pos = c.prod_order_item_pos
AND a.prod_order_version = c.max_version
JOIN pps_plan_slab b
ON b.prod_order_id = c.prod_order_id
AND b.prod_order_item_pos = c.prod_order_item_pos
AND b.prod_order_version = c.max_version
WHERE a.strip_thickn_aim > 1.78
AND a.strip_thickn_aim < 3.26
AND a.steelgrade = 'M4R51'
AND a.prod_order_id NOT BETWEEN '0999551' AND '0999599'
AND a.strip_width_aim BETWEEN 1126 AND 1166
AND NVL (a.order_weight_plan, 0) > 0
AND a.order_weight_plan >= b.coil_weight
ORDER BY prod_order_id ASC
Anyone have a suggestion?
Maurice
when using aggregate functions max(),min(),sum(),... you must use group by clause clause.
I'm using the h2 v1.3.176.
I have user-defined function which execute RECURSIVE query.
public static ResultSet getChildCategories(Connection connection, long categoryId) throws SQLException {
String sql =
"WITH RECURSIVE r(CATEGORY_ID, PARENT_ID) AS (\n" +
" SELECT CATEGORY_ID\n" +
" ,PARENT_ID\n" +
" FROM CATEGORY\n" +
" WHERE CATEGORY_ID = " + categoryId + "\n" +
" UNION ALL\n" +
" SELECT CATEGORY.CATEGORY_ID\n" +
" ,CATEGORY.PARENT_ID\n" +
" FROM CATEGORY, r\n" +
" WHERE CATEGORY.PARENT_ID = r.CATEGORY_ID\n" +
")\n" +
"SELECT CATEGORY_ID FROM r";
ResultSet resultSet = connection.createStatement().executeQuery(sql);
SimpleResultSet rs = new SimpleResultSet();
rs.addColumn("CATEGORY_ID", Types.INTEGER, 12, 0);
try {
while(resultSet.next()) {
rs.addRow(resultSet.getLong(1));
}
} finally {
resultSet.close();
}
return rs;
}
I have registered this function by following SQL.
create alias GET_CHILD_CATEGORIES for "com.myapp.db.function.Functions.getChildCategories";
My problem is the getChildCategories function will be called many times when I execute the following query.
SELECT DISTINCT B.BOOK_ID
,B.SERIES_ID
,B.TITLE
,B.ISBN
,B.VOLUME
,(
SELECT MAX(SAME_SERIES.VOLUME)
FROM BOOK SAME_SERIES
WHERE SAME_SERIES.SERIES_ID = B.SERIES_ID
AND SAME_SERIES.VOLUME IS NOT NULL
) AS VOLUME_COUNT
,B.PAGE_COUNT
,B.FILE_PATH
,B.SORTABLE_FILE_NAME
,B.SIZE
,B.HASH
,B.COVER_IMAGE_TYPE
,B.COVER_PAGE_NO
,B.COVER_LARGE_IMAGE_URL
,B.COVER_SMALL_IMAGE_URL
,B.COVER_CROP_COORD
,B.IS_ENCRYPT
,B.PUBLISHER_ID
,B.PUBLISHED_DATE
,B.CREATION_TIME
,B.LAST_MODIFIED_TIME
,B.NOTE
,B.IS_ISBN_SEARCH
,S.CATEGORY_ID
,S.TITLE
,BA.AUTHOR_ID
,BT.TAG_ID
FROM BOOK AS B
INNER JOIN SERIES AS S ON S.SERIES_ID = B.SERIES_ID
LEFT OUTER JOIN BOOK_TAG AS BT ON BT.BOOK_ID = B.BOOK_ID
LEFT OUTER JOIN BOOK_AUTHOR AS BA ON BA.BOOK_ID = B.BOOK_ID
WHERE
(
S.CATEGORY_ID IN (SELECT CATEGORY_ID FROM GET_CHILD_CATEGORIES(106))
And
S.IS_COMPLETION = 1
)
ORDER BY BA.AUTHOR_ID
Why do many times would be called this function?
Extracted from H2 documentation
A function that returns a result set can be used like a table.
However, in this case the function is called at least twice: first
while parsing the statement to collect the column names (with
parameters set to null where not known at compile time). And then,
while executing the statement to get the data (maybe multiple times if
this is a join). If the function is called just to get the column
list, the URL of the connection passed to the function is
jdbc:columnlist:connection. Otherwise, the URL of the connection is
jdbc:default:connection.
The first calls are only to retrieve the resultset column types. Then you have to check if the connection url is "jdbc:columnlist:connection". If true you have to return an empty result set with column list.
The connection url test is:
connection.getMetaData().getURL().equals("jdbc:columnlist:connection");