When I try to execute two SELECT statements at once as follows below, the logging console returns a runtime error:
java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT * FROM ...
The Java source code that generates the statements:
@Repository
public class VehicleObjectDbAccess {
@PersistenceContext
EntityManager entityManager;
public List<Object[]> getObjectById(long objectId, long year)
{
int limit = 10;
String tableName = ("i0i"+year)+objectId;
String queryText =
"START TRANSACTION;"
+ "SELECT t.created INTO @startTime FROM ObjectTable as t WHERE t.speed > 30 LIMIT 1;"
+ "SELECT * FROM ObjectTable WHERE created <= (CASE WHEN @startTime IS NULL THEN NOW() ELSE @startTime END) ORDER BY created DESC LIMIT 10;"
+ "COMMIT;";
Query query = this.entityManager.createNativeQuery(queryText);
return query.getResultList();
}
}
Eventually the java source code above translates to
START TRANSACTION;
SELECT t.created INTO @startTime FROM ObjectTable as t WHERE t.speed > 30 LIMIT 1;
SELECT * FROM ObjectTable WHERE created <= (CASE WHEN @startTime IS NULL THEN NOW() ELSE @startTime END) ORDER BY created DESC LIMIT 10;
COMMIT;
I verified that SQL code running it on a MySQL client and it works properly.
How can I execute these two SELECT statements in a single query?