@@ -261,6 +261,35 @@ static bool subst_spvars(THD *thd, sp_instr *instr, LEX_STRING *query_str)
261
261
// /////////////////////////////////////////////////////////////////////////
262
262
263
263
264
+ class SP_instr_error_handler : public Internal_error_handler
265
+ {
266
+ public:
267
+ SP_instr_error_handler ()
268
+ : cts_table_exists_error(false )
269
+ {}
270
+
271
+ virtual bool handle_condition (THD *thd,
272
+ uint sql_errno,
273
+ const char *,
274
+ Sql_condition::enum_severity_level*,
275
+ const char *)
276
+ {
277
+ /*
278
+ Check if the "table exists" error or warning reported for the
279
+ CREATE TABLE ... SELECT statement.
280
+ */
281
+ if (thd->lex && thd->lex ->sql_command == SQLCOM_CREATE_TABLE &&
282
+ thd->lex ->select_lex && thd->lex ->select_lex ->item_list .elements > 0 &&
283
+ sql_errno == ER_TABLE_EXISTS_ERROR)
284
+ cts_table_exists_error= true ;
285
+
286
+ return false ;
287
+ }
288
+
289
+ bool cts_table_exists_error;
290
+ };
291
+
292
+
264
293
bool sp_lex_instr::reset_lex_and_exec_core (THD *thd,
265
294
uint *nextp,
266
295
bool open_tables)
@@ -336,6 +365,9 @@ bool sp_lex_instr::reset_lex_and_exec_core(THD *thd,
336
365
thd->lex ->safe_to_cache_query = 0 ;
337
366
#endif
338
367
368
+ SP_instr_error_handler sp_instr_error_handler;
369
+ thd->push_internal_handler (&sp_instr_error_handler);
370
+
339
371
/* Open tables if needed. */
340
372
341
373
if (!error)
@@ -413,6 +445,9 @@ bool sp_lex_instr::reset_lex_and_exec_core(THD *thd,
413
445
}
414
446
}
415
447
448
+ // Pop SP_instr_error_handler error handler.
449
+ thd->pop_internal_handler ();
450
+
416
451
if (m_lex->query_tables_own_last )
417
452
{
418
453
/*
@@ -451,7 +486,8 @@ bool sp_lex_instr::reset_lex_and_exec_core(THD *thd,
451
486
See Query_arena->state definition for explanation.
452
487
453
488
Some special handling of CREATE TABLE .... SELECT in an SP is required. The
454
- state is always set to STMT_INITIALIZED_FOR_SP in such a case.
489
+ state is set to STMT_INITIALIZED_FOR_SP even in case of "table exists"
490
+ error situation.
455
491
456
492
Why is this necessary? A useful pointer would be to note how
457
493
PREPARE/EXECUTE uses functions like select_like_stmt_test to implement
@@ -467,12 +503,10 @@ bool sp_lex_instr::reset_lex_and_exec_core(THD *thd,
467
503
*/
468
504
469
505
bool reprepare_error=
470
- error && thd->get_stmt_da ()->mysql_errno () == ER_NEED_REPREPARE;
471
- bool is_create_table_select=
472
- thd->lex && thd->lex ->sql_command == SQLCOM_CREATE_TABLE &&
473
- thd->lex ->select_lex && thd->lex ->select_lex ->item_list .elements > 0 ;
506
+ error && thd->is_error () &&
507
+ thd->get_stmt_da ()->mysql_errno () == ER_NEED_REPREPARE;
474
508
475
- if (reprepare_error || is_create_table_select )
509
+ if (reprepare_error || sp_instr_error_handler. cts_table_exists_error )
476
510
thd->stmt_arena ->state = Query_arena::STMT_INITIALIZED_FOR_SP;
477
511
else if (!error || !thd->is_error () ||
478
512
(thd->get_stmt_da ()->mysql_errno () != ER_CANT_REOPEN_TABLE &&
0 commit comments