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
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.
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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With