Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scope of temp tables created in MySQL stored procedure

QUESTION WITHDRAWN! When I spell everything correctly, the problem goes away!

I have a MySQL stored procedure which creates a temporary table. When I call the procedure from the mysql prompt, it appears to run successfully, but if I then SELECT COUNT(*) from the temporary table, I get an error saying the table doesn't exist.

Does a temporary table created inside a stored procedure cease to exist when the stored procedure ends?

mysql> delimiter //
mysql> drop procedure if exists sp_temp_reciepts//
mysql> create procedure sp_temp_receipts ()
begin
  drop temporary table if exists receipts;
  create temporary table receipts
  (  ... snip ...
  );
  insert into receipts
  select ... snip ...
end//
mysql> delimiter ;
mysql> call sp_temp_reciepts();
Query OK, 46903 rows affected, 1 warning (2.15 sec)
mysql> select count(*) from receipts;
ERROR 1146 (42S02): Table 'receipts' doesn't exist
like image 627
user1956095 Avatar asked Jan 07 '13 20:01

user1956095


People also ask

What is the scope of temp tables in SQL?

Scope of the Local Temporary Table is the session in which it is created and they are dropped automatically once the session ends and we can also drop them explicitly. If a Temporary Table is created within a batch, then it can be accessed within the next batch of the same session.

Can you use temp tables in stored procedures?

You can create and use temporary tables in a stored procedure, but the temporary table exists only for the duration of the stored procedure that creates it. When the procedure completes, Adaptive Server automatically drops the temporary table.

Can we create temporary table in stored procedure in MySQL?

A temporary table exists within a session only; therefore, a table created by one session is not visible to another session. For example, Session ID 501 has created a temporary table then session ID 502 cannot access it or manipulate it. Two different sessions can have a temporary table with the same name.

Can we access a temp table of one stored procedure from another stored procedure?

This is because when creating a stored procedure you can access temporary tables from a connection other than the one that created the table. If you execute the above query, you will see that SQL Server will not throw any error.


1 Answers

This question is over a year old. It deserves an answer. Here it goes:

I just saw this question today in the DBA StackExchange : How long will a temporary MEMORY table persist if I don't drop it (MySQL). I just answered it. In part I said this:

According to the Book

kdsjx

Chapter 5 has a subheading Returning Result Sets to Another Stored Procedure.

It says in paragraph 2 on Page 117:

Unfortunately, the only way to pass a result set from one stored procedure to another is to pass the results via a temporary table. This is an awkward solution b, and -- because the temporary table has scope throughout the entire session -- it creates many of the same maintainability issues raised by the use of global variables. but if one stored program needs to supply another stored program with results, then a temporary table can be the best solution.

Although the question was dropped because of spelling the table wrong, this viewpoint needs to expressed and learned by all who call MySQL Stored Procedures and need their data available.

like image 77
RolandoMySQLDBA Avatar answered Sep 28 '22 12:09

RolandoMySQLDBA