onthewebfert.blogg.se

Create temp table in a for loop in php
Create temp table in a for loop in php











This will not harm other DB Connections since each Connection has its own id_list table. Whichever way you choose, the operation is still the same since TRUNCATE TABLE drops and recreates the table. I say no because another way to do it is: CREATE TEMPORARY TABLE IF NOT EXISTS id_list (iid CHAR(32) NOT NULL) ENGINE=memory I say Yes because it is one way to do it. If we employ persistent connections, will the MEMORY table persist through multiple REQUESTS, and it seems it will, so for performance sake, I'm assuming that using this method will *REQUIRE us to explicitly DROP the temporary MEMORY table.

create temp table in a for loop in php

Since you calling a recursive stored procedure, you can rest assured the temp table is fully accessible for the duration of the DB Connection. Since the mysql client is not a Stored Procedure, the results cannot be manipulated the mysql client level via DML other than doing a SELECT to see the results. Looking back at the StackOverflow question, I can see someone called the Stored Procedure from the mysql client. but if one stored program needs to supply another stored program with results, then a temporary table can be the best solution. 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. Unfortunately, the only way to pass a result set from one stored procedure to another is to pass the results via a temporary table. The fact is: The temp table exists inside and outside of the Stored Procedure, but you can do things with the temporary table only inside the scope of a running Stored Procedure.Ĭhapter 5 has a subheading Returning Result Sets to Another Stored Procedure. It has been over a year and nobody answered the question? Let me set the record straight. Someone asked this question on StackOverflow : Scope of temp tables created in MySQL stored procedure. What is funny about temporary tables in a stored procedure is not so much the transient existence of the table (which gets dropped upon the DB connection's termination), but the scope of the stored procedure. WHERE r.Group_ID = 'abc123' OR Thanks to the commentors for your input, and for giving me the reason I needed to try a little harder :) So the result of the stored procedure is now a comma separated list of ID's that is compatible with FIND_IN_SET, and so the final query was modified so that: WHERE r.Group_ID = 'abc123' OR r.Group_ID IN (SELECT * FROM id_list) SELECT GROUP_CONCAT(DISTINCT iid SEPARATOR ',') FROM id_list INTO inherited_set Rather than just calling the stored procedure and using the remaining TEMP table to gather the results in the actual query, I've changed the CALL format to use a third OUT variable like so: CALL then within the stored procedure, I added a second IF tier = 0 at the very end with the following: IF tier = 0 UPDATE: Based on the advice from the commenters, I've found a way of adjusting my stored procedure so that I can utilize the TEMP MEMORY table, but be able to explicitly DROP it at the end.

Create temp table in a for loop in php free#

The obvious answer might be to drop the temp table prior to the commit statement, but lets assume for a moment that I can't do that.*ĮDIT: To be a little more precise, what if persistent connections are employed, will the table persist through multiple requests? So far it seems that it will and that we would need to explicitly remove the temp table to free up that resource. So my question is: If I don't DROP the temporary MEMORY table at the end of the procedure, or within my transaction, how long will that table persist in memory? Is it automatically dropped once the session ends, or will it remain in memory as long as the connection is open? Prior to the recursive loop it checks if tier = 0 and if it is it runs: DROP TEMPORARY TABLE IF EXISTS id_list ĬREATE TEMPORARY TABLE IF NOT EXISTS id_list (iid CHAR(32) NOT NULL) ENGINE=memory When calling the procedure, the first value is the top ID of the branch I want, and the second is the tier which the procedure uses during recursions. WHERE r.Group_ID = 'abc123' OR r.Group_ID IN (SELECT * FROM id_list)

create temp table in a for loop in php

INNER JOIN usr_accts a ON a.User_ID = r.User_ID * uses the results of the stored procedure in the WHERE */ * generates the temporary table of ID's */ĬALL fetch_inheritance_groups('abc123',0)

create temp table in a for loop in php

I'm using a recursive stored procedure in MySQL to generate a temporary table called id_list, but I must use the results of that procedure in a follow up select query, so I can't DROP the temporary table within the procedure.











Create temp table in a for loop in php