In our database, we generate statistics by periodically calling a large number of stored procedures in the event.

Considering there are two SP

  1. auto_update_daily_users
  2. auto_update_daily_orders

Is there a conflict when using the same temporary table name (lets say temp1)in both stored procedures?

I checked Q&A on Stack Overflow, they say the temporary table's scope is the current session. I think it means when we connect the db server with clients.

But in my case all the sp are running on the server side. So could anyone explain this?

Will the temporary table be automatically destroyed after one SP is finished? What if two SPs are running at the same time? Aren't they running in the same session?


2 Answers 2


in my case all the sp are running on the server side

Any case the SP is executed using some client connection (from Event Scheduler, for example). If they are called one-by-one from some outer SP/UDF - they are executed in the same connection, and temp table created by 1st SP exists when 2nd starts. If they are executed independently (for example, as/from 2 separate event procedures) - they are executed in different connections, and each have its own independent (local) temp table.

Server only accept queries and returns its results. Anything that sends queries and receives its results is client. See SHOW PROCESSLIST - you will see all client connections, including Event Scheduler service connection and executed events connections, separate connection for each separate event.

Will the temporary table be automatically destroyed after one SP is finished?

Temporary tables have a connection scope. If you execute 2 SPs in two different connections, each have its own temp table with its own structure and data despite the fact that their names are the same.

If you execute one SP then another, but close connection after executing 1st and create new for executing 2nd - each SP will have its own temp table again.

But if you execute one SP then another in the same connection then temptable created by 1st will exist while 2nd start, with the data inserted by 1st. - akina

  • Thanks for Akina's answer, please note that the client mentioned here is not a GUI tool (navicat or mysql workbench).
    – mingchau
    Commented Jun 21, 2019 at 3:33

Plan A: Use different temp table names in different EVENTs.

Plan B: Fabricate the temp table name using connection_id():

mysql> SELECT @tmp := CONCAT("_T_", connection_id());
| @tmp := CONCAT("_T_", connection_id()) |
| _T_453                                 |

Then prepare & execute the queries in question.

Plan C: Use GET_LOCK(..) (etc) to prevent one Event from using another Event's temp table.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.