11

I'd like to execute something like this on my MySQL server:

SET @id=(SELECT id FROM lookupTable WHERE field=?);
(SELECT * FROM table2 WHERE id=@id)
UNION
(SELECT * FROM table3 WHERE id=@id)
UNION
(SELECT * FROM table4 WHERE id=@id);

This works fine from the console, but not from my Java PreparedStatement. It throws an exception with a syntax error at the ';' separating the statements. I like the variable because I don't have to repeat the lookup clause, but I could rewrite it if necessary. The equivalent JOIN is a little awkward with the UNION clauses too.

Thanks,

Joshua

Joshua Martell
  • 7,074
  • 2
  • 30
  • 37
  • As @ddimitrov says, this doesn't seem possible directly. I didn't try `PreparedStatement.addBatch()`. A stored procedure would likely work for this too. – Joshua Martell Aug 28 '10 at 03:52
  • If you're interested in other vendors, then CUBRID Database supports this feature. See this forum post http://www.cubrid.org/forum/534638. – esengineer Dec 31 '12 at 02:17

2 Answers2

5

JDBC has never supported parsing delimited queries. Each invocation is one trip to the database. Perhaps you can achieve what you meant to doing PreparedStatement.addBatch() for each separate query, then executing and retrieving the two resultsets ?

ddimitrov
  • 3,293
  • 3
  • 31
  • 46
2

Just running this as two separate queries (within one connection) should give you same results.

Mchl
  • 61,444
  • 9
  • 118
  • 120